a. 二值化
b. 得到轮廓的个数
c. 将面积小于100的轮廓删除
d. 将宽、高 比例小于1的轮廓删除
e. 把面积最大的米粒用红色框框画出来
#include <stdio.h>#include <cv.h>#include <cxcore.h>#include <highgui.h>#pragma comment(lib, "ml.lib")#pragma comment(lib, "cv.lib")#pragma comment(lib, "cvaux.lib")#pragma comment(lib, "cvcam.lib")#pragma comment(lib, "cxcore.lib")#pragma comment(lib, "cxts.lib")#pragma comment(lib, "highgui.lib")#pragma comment(lib, "cvhaartraining.lib")int main( int argc, char** argv ) { IplImage* src; src=cvLoadImage("black.jpg",CV_LOAD_IMAGE_GRAYSCALE); IplImage* dst = cvCreateImage( cvGetSize(src), 8, 3 ); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* contour = 0; cvThreshold( src, src,120, 255, CV_THRESH_BINARY );//二值化 cvNamedWindow( "Source", 1 ); cvShowImage( "Source", src ); //提取轮廓 cvFindContours( src, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); cvZero( dst );//清空数组 CvSeq* _contour =contour; double maxarea=0; double minarea=100; int n=-1,m=0;//n为面积最大轮廓索引,m为迭代索引 for( ; contour != 0; contour = contour->h_next ) { double tmparea=fabs(cvContourArea(contour)); if(tmparea < minarea) { cvSeqRemove(contour,0); //删除面积小于设定值的轮廓 continue; } CvRect aRect = cvBoundingRect( contour, 0 ); if ((aRect.width/aRect.height)<1) { cvSeqRemove(contour,0); //删除宽高比例小于设定值的轮廓 continue; } if(tmparea > maxarea) { maxarea = tmparea; n=m; } m++; // CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );//创建一个色彩值 CvScalar color = CV_RGB( 0, 255,255 ); //max_level 绘制轮廓的最大等级。如果等级为0,绘制单独的轮廓。如果为1,绘制轮廓及在其后的相同的级别下轮廓。 //如果值为2,所有的轮廓。如果等级为2,绘制所有同级轮廓及所有低一级轮廓,诸此种种。 //如果值为负数,函数不绘制同级轮廓,但会升序绘制直到级别为abs(max_level)-1的子轮廓。 cvDrawContours( dst, contour, color, color, -1, 1, 8 );//绘制外部和内部的轮廓 } contour =_contour; /*int k=0;*/ int count=0; for( ; contour != 0; contour = contour->h_next ) { count++; double tmparea=fabs(cvContourArea(contour)); if (tmparea==maxarea /*k==n*/) { CvScalar color = CV_RGB( 255, 0, 0); cvDrawContours( dst, contour, color, color, -1, 1, 8 ); } /*k++;*/ } printf("The total number of contours is:%d",count); cvNamedWindow( "Components", 1 ); cvShowImage( "Components", dst ); cvWaitKey(0); cvDestroyWindow( "Source" ); cvReleaseImage(&src); cvDestroyWindow( "Components" ); cvReleaseImage(&dst); return 0; }