• 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;
    }
    

      

    效果:

  • 相关阅读:
    软工第1次阅读作业
    软工第0次作业
    第四次博客
    第三次博客
    第二次
    第一次博客
    提问回顾与个人总结
    软件工程结对作业
    软件工程第一次阅读作业
    软件工程第0次个人作业
  • 原文地址:https://www.cnblogs.com/Blackops/p/5866891.html
Copyright © 2020-2023  润新知