原文:http://blog.youkuaiyun.com/qinjianganying/article/details/6648700
OTSU一维算法,我自己的理解是自适应阈值分割法,通过对灰度图的处理自行得到一个最佳的阈值,并最后用这个阈值二值化灰度图,参考了商丘师范学院的胡颖老师的<<OTSU算法的改进与纠正>>一文,但是我用的时候效果不是很理想,于是自己进行了稍微的变动,
OTSU算法:就是计算出灰度图最佳阈值的算法
1.先对灰度图进行直方图计算并归一化处理,得到0-255之间每个像素在灰度图中出现的概率,即表示为某个像素在灰度图中出现了n个,灰度图总的像素点为N个,则这个像素的出现概率为Pi=n/N
2.每个灰度图可以由阈值k将灰度图分为A,B两大类,很容易得到A,B类在灰度图中的出现概率以及灰度均值
3.计算灰度图A,B类得类间方差,在最佳阈值K处,求得的类间方差最大,也就是类间方差最大的那个时刻的阈值就为灰度图的最佳阈值
下面结合代码讲解
- int ThresholdOtsu(IplImage *src)
- {
- assert(src->nChannels == 1);
- float histogram[256] = {0};
- for(int h=0;h<src->height;h++)
- {
- unsigned char* p = (unsigned char *)src->imageData +h*src->height;
- for(int w=0;w<src->width ;w++)
- {
- histogram[*p++]++;//计算直方图
- }
- }
- int totalpixel = src->width *src->height;
- for(int i=0;i<256;i++)
- histogram[i] =(float) histogram[i]/(float)totalpixel;//归一化直方图
- float maxavgvalue = 0;
- for(int i=0;i<256;i++)
- maxavgvalue = i*histogram[i];//总的灰度均值,其实在这里可将其设为0
- float PA = 0;//A类出现概率
- float PB = 0;//B类出现概率
- float Agrayvalue = 0;//A类灰度均值
- float Bgrayvalue = 0;//B类灰度均值
- float maxvariance = 0;
- float variance ;
- int threshold;
- for(int i=0;i<256;i++)
- {
- PA += histogram[i];
- PB = 1-PA;
- Agrayvalue += i*histogram[i];//A类灰度均值
- Bgrayvalue += maxavgvalue - Agrayvalue;//B类灰度均值
- //Agrayvalue = Agrayvalue/PA;
- //Bgrayvalue = Bgrayvalue/PB;
- variance = PA*(Agrayvalue-maxavgvalue)*(Agrayvalue-maxavgvalue)+PB*(Bgrayvalue-maxavgvalue)*(Bgrayvalue-maxavgvalue);//计算类间方差
- if(variance>maxvariance)
- {
- maxvariance = variance;
- threshold = i;//求得最大类间方差时的像素值,即为最佳阈值
- }
- }
- return threshold;//返回最佳阈值二值化图像
- }
int ThresholdOtsu(IplImage *src)
{
assert(src->nChannels == 1);
float histogram[256] = {0};
for(int h=0;h<src->height;h++)
{
unsigned char* p = (unsigned char *)src->imageData +h*src->height;
for(int w=0;w<src->width ;w++)
{
histogram[*p++]++;//计算直方图
}
}
int totalpixel = src->width *src->height;
for(int i=0;i<256;i++)
histogram[i] =(float) histogram[i]/(float)totalpixel;//归一化直方图
float maxavgvalue = 0;
for(int i=0;i<256;i++)
maxavgvalue = i*histogram[i];//总的灰度均值,其实在这里可将其设为0
float PA = 0;//A类出现概率
float PB = 0;//B类出现概率
float Agrayvalue = 0;//A类灰度均值
float Bgrayvalue = 0;//B类灰度均值
float maxvariance = 0;
float variance ;
int threshold;
for(int i=0;i<256;i++)
{
PA += histogram[i];
PB = 1-PA;
Agrayvalue += i*histogram[i];//A类灰度均值
Bgrayvalue += maxavgvalue - Agrayvalue;//B类灰度均值
//Agrayvalue = Agrayvalue/PA;
//Bgrayvalue = Bgrayvalue/PB;
variance = PA*(Agrayvalue-maxavgvalue)*(Agrayvalue-maxavgvalue)+PB*(Bgrayvalue-maxavgvalue)*(Bgrayvalue-maxavgvalue);//计算类间方差
if(variance>maxvariance)
{
maxvariance = variance;
threshold = i;//求得最大类间方差时的像素值,即为最佳阈值
}
}
return threshold;//返回最佳阈值二值化图像
}
将这个算法放入代码中,实现的效果很理想,并且进行形态学运算后效果如下: