simple image processing

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值