图像处理——二值化(大律法)

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

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


你可能感兴趣的:(图像处理)