OpenCV学习笔记——形态学梯度操作

Posted Blackops

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV学习笔记——形态学梯度操作相关的知识,希望对你有一定的参考价值。

代码:

#include<cv.h>
#include<highgui.h>
int main(void)
{
	cvNamedWindow("cmp");
	IplImage *temp = cvLoadImage("sample.jpg");//载入
	IplImage *src = cvCreateImage(CvSize(temp->width*0.5,temp->height*0.5), temp->depth,temp->nChannels);//缩放
	cvResize(temp, src);
	IplImage *srctemp = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);
	IplImage *total = cvCreateImage(CvSize(src->width *5, src->height ), src->depth, src->nChannels);

	IplImage *open = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);
	IplImage *close = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);
	IplImage *gradient = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);
	IplImage *tophat = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);
	IplImage *blackhat = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);

	cvZero(srctemp);
	cvZero(total);

	cvZero(open);
	cvZero(close);
	cvZero(gradient);
	cvZero(tophat);
	cvZero(blackhat);

	IplConvKernel *kernel = cvCreateStructuringElementEx(3,3,2,2,CV_SHAPE_ELLIPSE);

	cvMorphologyEx(src, open, srctemp, kernel, CV_MOP_OPEN);
	cvMorphologyEx(src, close, srctemp, kernel, CV_MOP_CLOSE);
	cvMorphologyEx(src, gradient, srctemp, kernel, CV_MOP_GRADIENT);
	cvZero(srctemp);
	cvMorphologyEx(src, tophat, srctemp, kernel, CV_MOP_TOPHAT);
	cvMorphologyEx(src, open, srctemp, kernel, CV_MOP_BLACKHAT);
	CvFont font;
	cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1, 1, 2, 2, CV_AA);
	cvPutText(open, "open", CvPoint(20, 40), &font, CV_RGB(169, 55, 55));
	cvPutText(close, "close", CvPoint(20, 40), &font, CV_RGB(169, 55, 55));
	cvPutText(gradient, "gradient", CvPoint(20, 40), &font, CV_RGB(169, 55, 55));
	cvPutText(tophat, "tophat", CvPoint(20, 40), &font, CV_RGB(169, 55, 55));
	cvPutText(blackhat, "blackhat", CvPoint(20, 40), &font, CV_RGB(169, 55, 55));
	int width = src->width;
	int height = src->height;

	cvSetImageROI(total, CvRect(0, 0, width, height));
	cvCopy(open, total);
	cvResetImageROI(total);

	cvSetImageROI(total, CvRect(width, 0, width, height));
	cvCopy(close, total);
	cvResetImageROI(total);

	cvSetImageROI(total, CvRect(width*2, 0, width, height));
	cvCopy(gradient, total);
	cvResetImageROI(total);

	cvSetImageROI(total, CvRect(width * 3, 0, width, height));
	cvCopy(tophat, total);
	cvResetImageROI(total);

	cvSetImageROI(total, CvRect(width * 4, 0, width, height));
	cvCopy(blackhat, total);
	cvResetImageROI(total);

	cvShowImage("cmp", total);
	cvWaitKey(0);
	cvSaveImage("out.png", total);
	cvDestroyAllWindows();
	cvReleaseImage(&temp);
	cvReleaseImage(&src);
	cvReleaseImage(&srctemp);
	cvReleaseImage(&total);
	cvReleaseImage(&open);
	cvReleaseImage(&close);
	cvReleaseImage(&gradient);
	cvReleaseImage(&tophat);
	cvReleaseImage(&blackhat);

	return 0;
}

  

效果:

以上是关于OpenCV学习笔记——形态学梯度操作的主要内容,如果未能解决你的问题,请参考以下文章

opencv学习之路(15)形态学其他操作(开闭顶帽黑帽形态学梯度)

学习 opencv---(10)形态学图像处理:开运算,闭运算,形态学梯度,顶帽,黒帽合辑

opencv的学习笔记4

youcans 的 OpenCV 例程 200 篇119. 图像的形态学梯度

OpenCV 形态学操作(开,闭,梯度,礼帽,黑帽)

OpenCV 形态学操作(开,闭,梯度,礼帽,黑帽)