OpenCV学习笔记——图像的腐蚀与膨胀

顺便又复习了一下cvcopy如何进行图像拼接(最近觉得打开多幅图像分别看不如缩小掉放拼接到一幅图像上对比来的好)

首先把拼接的目标图像设置兴趣区域ROI,比如我有一个total,要把a、b、c分别从左到右拼接到total上,那就分三次对total设置敢兴趣区域ROI(注意不是对a、b、c设置),然后再用cvcopy复制过去,如果要加文字可以在复制之前预先把文字加到a、b、c上

然后就是图像的腐蚀和膨胀,可以自定义一个kernel

代码:

#include<cv.h>
#include<highgui.h>
int main(void)
{
cvNamedWindow("Compare");
IplImage *temp = cvLoadImage("zhiwen.jpg"); IplImage *src = cvCreateImage(CvSize(temp->width / 2, temp->height / 2), temp->depth, temp->nChannels);
cvResize(temp, src); IplImage *total = cvCreateImage(CvSize(src->width * 3, src->height),src->depth,src->nChannels); IplImage *erode = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);
IplImage *dilate= cvCreateImage(cvGetSize(src), src->depth, src->nChannels); cvZero(erode);
cvZero(dilate);
IplConvKernel *kernel = cvCreateStructuringElementEx(3,3,2,2,CV_SHAPE_RECT);
cvErode(src, erode,kernel,1);
cvDilate(src, dilate,kernel,1); CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1, 1, 2, 2, CV_AA);
char *a = "Original", *b = "Erode",*c="Dilate";
//放置字体
cvPutText(src, a,CvPoint(0, 40),&font,CV_RGB(255,0,0));
cvPutText(erode, b, CvPoint(0, 40), &font, CV_RGB(255, 0, 0));
cvPutText(dilate, c, CvPoint(0, 40), &font, CV_RGB(250, 0, 0)); cvSetImageROI(total, cvRect(0, 0, src->width, src->height));//放入原图像
cvCopy(src, total);
cvResetImageROI(total);//记得取消ROI cvSetImageROI(total, cvRect(src->width, 0, src->width, src->height));//放入腐蚀结果
cvCopy(erode, total);
cvResetImageROI(total); cvSetImageROI(total, CvRect(src->width * 2, 0, src->width, src->height));//放入膨胀结果
cvCopy(dilate, total);
cvResetImageROI(total); cvShowImage("Compare", total);//对比图
cvWaitKey(0);
cvSaveImage("result.png", total);
cvDestroyAllWindows();
cvReleaseImage(&temp);
cvReleaseImage(&src);
cvReleaseImage(&total);
cvReleaseImage(&dilate);
cvReleaseImage(&erode);
cvReleaseStructuringElement(&kernel);
return 0;
}

  

效果图:

OpenCV学习笔记——图像的腐蚀与膨胀

上一篇:DEMO: springboot 与 mybatis 集成


下一篇:浅谈前端中的mvvm与mvc