int _tmain(int argc, _TCHAR* argv[]) {
IplImage *src = NULL;
IplImage *tmp = NULL;
IplImage *src_bg = NULL;
IplImage *dst_gray = NULL;
IplImage *dst_bw = NULL;
IplImage *dst_contours = NULL;
IplConvKernel *element = NULL; //形态学结构指针
int Num_Obj = 0; //定义目标对象数量
int contour_area_tmp = 0; //定义目标对象面积临时寄存器
int contour_area_sum = 0; //定义目标所有对象面积的和
int contour_area_ave = 0; //定义目标对象面积平均值
int contour_area_max = 0; //定义目标对象面积最大值
CvMemStorage *memstor = NULL;
CvSeq *cont = NULL;
CvSeq *a_contour = NULL;
//read and display the img
src = cvLoadImage("rice.png", 0);
cvNamedWindow("src", CV_WINDOW_AUTOSIZE);
cvShowImage("src", src);
//cvSmooth(src, src, CV_MEDIAN, 3, 0, 0, 0);
//estimate background of the img
tmp = cvCreateImage(cvGetSize(src), src ->depth, src ->nChannels);
src_bg = cvCreateImage(cvGetSize(src), src ->depth, src ->nChannels);
//create structuring element
element = cvCreateStructuringElementEx(4, 4, 1, 1, CV_SHAPE_ELLIPSE, 0);
//用该结构对源图象进行数学形态学的开操作后,估计背景亮度
cvErode(src, tmp, element, 10);
cvDilate(tmp, src_bg, element, 10);
cvNamedWindow("src_bg", CV_WINDOW_AUTOSIZE);
cvShowImage("src_bg", src_bg);
//src - bg
dst_gray = cvCreateImage(cvGetSize(src), src ->depth, src ->nChannels);
cvSub(src, src_bg, dst_gray, 0);
cvNamedWindow("dst_gray", CV_WINDOW_AUTOSIZE);
cvShowImage("dst_gray", dst_gray);
//threshold
dst_bw = cvCreateImage(cvGetSize(src), src -> depth, src ->nChannels);
cvThreshold(dst_gray, dst_bw, 50, 255, CV_THRESH_BINARY);
//cvAdaptiveThreshold(dst_gray, dst_bw, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 3, 5);
cvNamedWindow("dst_bw", CV_WINDOW_AUTOSIZE);
cvShowImage("dst_bw", dst_bw);
//check the number of object in the img
memstor = cvCreateMemStorage(0);
cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint), memstor);
//find all of the contours
Num_Obj = cvFindContours(dst_bw, memstor, &cont, sizeof(CvContour),
CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
printf("Number_object:%d\n", Num_Obj);
//compute the property of object
dst_contours = cvCreateImage(cvGetSize(src), src ->depth, src ->nChannels);
cvThreshold(dst_contours, dst_contours, 0, 255, CV_THRESH_BINARY); //画轮廓前先把图像变成白色
for(; cont; cont = cont ->h_next) {
//获取当前轮廓
cvDrawContours(dst_contours, cont, CV_RGB(255, 0, 0), CV_RGB(255, 0, 0), 0, 1, 8, cvPoint(0, 0));
//获取当前轮廓面积
contour_area_tmp = fabs(cvContourArea(cont, CV_WHOLE_SEQ));
if (contour_area_tmp > contour_area_max) {
contour_area_max = contour_area_tmp;
}
contour_area_sum += contour_area_tmp;
}
contour_area_ave = contour_area_sum / Num_Obj;
printf("contour_area_ave:%d\n", contour_area_ave);
printf("contour_area_max:%d\n", contour_area_max);
cvNamedWindow("dst_contours", CV_WINDOW_AUTOSIZE);
cvShowImage("dst_contours", dst_contours);
cvWaitKey(0);
cvReleaseImage(&src);
cvReleaseImage(&tmp);
cvReleaseImage(&src_bg);
cvReleaseImage(&dst_gray);
cvReleaseImage(&dst_bw);
cvReleaseImage(&dst_contours);
cvReleaseMemStorage(&memstor);
cvDestroyAllWindows();
return 0;
}
simple image processing
最新推荐文章于 2018-11-28 00:40:05 发布