opencv二值化函數(shù) threshold(src_gray,dst,threshold_value,max_BINARY_value,threshold_type),threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);這里二值化,即圖像像素值變成0或255好乐,THRESH_OTSU是確定閾值分割點,這個是庫函數(shù)確定的砌烁,下面介紹原理
對于圖像I(x,y),將要確定的分割閾值計做T催式,所以灰度點分成倆個區(qū)域函喉,ω0(所有低于T的)和ω1(高于T的),其對應(yīng)區(qū)域的平均灰度μ0和μ1荣月,圖像的總平均灰度記為μ管呵,類間方差記為g。圖像的大小為M×N喉童,圖像中像素的灰度值小于閾值T的像素個數(shù)記作N0撇寞,像素灰度大于閾值T的像素個數(shù)記作N1顿天,則有:
ω1=N1/ M×N (2)
N0+N1=M×N (3)
ω0+ω1=1 (4)
μ=ω0*μ0+ω1*μ1 (5)
g=ω0(μ0-μ)^2+ω1(μ1-μ)^2 (6) 將式(5)代入式(6),得到等價公式:
g=ω0ω1(μ0-μ1)^2 (7) 這就是類間方差
采用遍歷的方法得到使類間方差g最大的閾值T,即為所求堂氯。
int MyAutoFocusDll::otsuThreshold(IplImage *frame)
{
const int GrayScale = 256;
int width = frame->width;
int height = frame->height;
int pixelCount[GrayScale];
float pixelPro[GrayScale];
int i, j, pixelSum = width * height, threshold = 0;
uchar* data = (uchar*)frame->imageData; //指向像素數(shù)據(jù)的指針
for (i = 0; i < GrayScale; i++)
{
pixelCount[i] = 0;
pixelPro[i] = 0;
}
//統(tǒng)計灰度級中每個像素在整幅圖像中的個數(shù)
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
pixelCount[(int)data[i * width + j]]++; //將像素值作為計數(shù)數(shù)組的下標
}
}
//計算每個像素在整幅圖像中的比例
float maxPro = 0.0;
int kk = 0;
for (i = 0; i < GrayScale; i++)
{
pixelPro[i] = (float)pixelCount[i] / pixelSum;
if (pixelPro[i] > maxPro)
{
maxPro = pixelPro[i];
kk = i;
}
}
//遍歷灰度級[0,255]
float w0, w1, u0tmp, u1tmp, u0, u1, u, deltaTmp, deltaMax = 0;
for (i = 0; i < GrayScale; i++) // i作為閾值
{
w0 = w1 = u0tmp = u1tmp = u0 = u1 = u = deltaTmp = 0;
for (j = 0; j < GrayScale; j++)
{
if (j <= i) //背景部分
{
w0 += pixelPro[j];
u0tmp += j * pixelPro[j];
}
else //前景部分
{
w1 += pixelPro[j];
u1tmp += j * pixelPro[j];
}
}
u0 = u0tmp / w0;
u1 = u1tmp / w1;
u = u0tmp + u1tmp;
deltaTmp = w0 * pow((u0 - u), 2) + w1 * pow((u1 - u), 2);
if (deltaTmp > deltaMax)
{
deltaMax = deltaTmp;
threshold = i;
}
}
return threshold;
}