码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-09-13 01:32:03      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

代码:

#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学习笔记——形态学梯度操作

标签:

原文地址:http://www.cnblogs.com/Blackops/p/5866891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!