由于项目需要,要对图像中的最大连通域进行标定,并且存储。首先需要使用cvFindCountour对边缘进行标定,其实它的原理就是连通域的边缘提取;其次就是对连通域进行大小判断找出最大的连通域;最后当然就是进行Rect并且ROI了。如果有需要可以进行存储。直接上源码吧。
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
int main( int argc, char** argv )
{
//声明IplImage指针
IplImage* pImg =cvLoadImage("e:/black.jpg",0);
IplImage* pContourImg = NULL;
CvMemStorage * storage =cvCreateMemStorage(0);
CvSeq * contour = 0;
CvSeq *contmax = 0;
int mode = CV_RETR_EXTERNAL;
cvShowImage( "src", pImg );
//为轮廓显示图像申请空间
//3通道图像,以便用彩色显示
pContourImg =cvCreateImage(cvGetSize(pImg),
IPL_DEPTH_8U,
3);
//copy source image and convert it to BGRimage
cvCvtColor(pImg, pContourImg, CV_GRAY2BGR);
//查找contour
cvFindContours( pImg, storage,&contour, sizeof(CvContour),
mode, CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));
//将轮廓画出
cvDrawContours(pContourImg, contour,
CV_RGB(255,0,0), CV_RGB(255, 0,0),
2, 2, 8, cvPoint(0,0));
int area,maxArea = 10;//设面积最大值大于10Pixel
for(;contour;contour =contour->h_next)
{
area = fabs(cvContourArea(contour, CV_WHOLE_SEQ )); //获取当前轮廓面积
printf(" area == %lfn",area);
if(area >maxArea)
{
contmax =contour;
maxArea =area;
}
}
CvRect aRect = cvBoundingRect( contmax, 0);
cvSetImageROI( pContourImg,aRect);
//显示图像
cvShowImage( "contour", pContourImg );
cvSaveImage("e:/contour.jpg",pContourImg);
cvWaitKey(0);
//销毁窗口
cvDestroyWindow( "src" );
cvDestroyWindow( "contour" );
//释放图像
cvReleaseImage( &pImg );
cvReleaseImage( &pContourImg);
cvReleaseMemStorage(&storage);
return 0;
}
处理前的连通域
处理后的连通域