simple image processing

来源:互联网 发布:好易网络电视apk下载 编辑:程序博客网 时间:2024/05/19 02:25
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 imgsrc = 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 imgtmp = cvCreateImage(cvGetSize(src), src ->depth, src ->nChannels);src_bg = cvCreateImage(cvGetSize(src), src ->depth, src ->nChannels);//create structuring elementelement = 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 - bgdst_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);//thresholddst_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 imgmemstor = cvCreateMemStorage(0);cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint), memstor);//find all of the contoursNum_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 objectdst_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;}

原创粉丝点击