码迷,mamicode.com
首页 > 其他好文 > 详细

图像处理——二值化

时间:2015-05-08 13:03:17      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:图像处理

二值化,利用大律法实现自适应二值化,自动求出二值化阈值

int BinarizeImageByOTSU (IplImage * src)
{ 
	assert(src != NULL);

	//get the ROI
	CvRect rect = cvGetImageROI(src);

	//information of the source image
	int x = rect.x;
	int y = rect.y;
	int width = rect.width; 
	int height = rect.height;
	int ws = src->widthStep;

	int thresholdValue=1;//阈值
	int ihist [256] ; // 图像直方图, 256个点
	int i, j, k,n, n1, n2, Color=0;
	double m1, m2, sum, csum, fmax, sb;
	memset (ihist, 0, sizeof (ihist)) ; // 对直方图置 零...

	for (i=y;i< y+height;i++) // 生成直方图
	{ 
		int mul =  i*ws;
		for (j=x;j<x+width;j++)
		{ 
			//Color=Point (i,j) ;
			Color = (int)(unsigned char)*(src->imageData + mul+ j);
			ihist [Color] +=1;
		}
	}
	sum=csum=0.0;
	n=0;
	for (k = 0; k <= 255; k++)
	{ 
		sum+= (double) k* (double) ihist [k] ; // x*f (x) 质量矩
		n +=ihist [k]; //f (x) 质量
	}
	// do the otsu global thresholding method
	fmax = - 1.0;
	n1 = 0;
	for (k=0;k<255;k++) 
	{
		n1+=ihist [k] ;
		if (! n1)
		{ 
			continue; 
		}
		n2=n- n1;
		if (n2==0) 
		{
			break;
		}
		csum+= (double) k*ihist [k] ;
		m1=csum/ n1;
		m2= (sum- csum) /n2;
		sb = ( double) n1* ( double) n2* ( m1 - m2) * (m1- m2) ;

		if (sb>fmax) 
		{
			fmax=sb;
			thresholdValue=k;
		}
	}

	//binarize the image 
	cvThreshold( src, src ,thresholdValue, 255, CV_THRESH_BINARY ); 
	return 0;
} 


图像处理——二值化

标签:图像处理

原文地址:http://blog.csdn.net/xiao_lxl/article/details/45577357

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!