大津法---OTSU算法--- THRESH_OTSU

opencv二值化函数 threshold(src_gray,dst,threshold_value,max_BINARY_value,threshold_type),threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);这里二值化,即图像像素值变成0或255,THRESH_OTSU是确定阈值分割点,这个是库函数确定的,下面介绍原理

对于图像I(x,y),将要确定的分割阈值计做T,所以灰度点分成俩个区域,ω0(所有低于T的)和ω1(高于T的),其对应区域的平均灰度μ0和μ1,图像的总平均灰度记为μ,类间方差记为g。图像的大小为M×N,图像中像素的灰度值小于阈值T的像素个数记作N0,像素灰度大于阈值T的像素个数记作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;  //指向像素数据的指针

    for (i = 0; i < GrayScale; i++)

    {

        pixelCount[i] = 0;

        pixelPro[i] = 0;

    }

    //统计灰度级中每个像素在整幅图像中的个数 

    for (i = 0; i < height; i++)

    {

        for (j = 0; j < width; j++)

        {

            pixelCount[(int)data[i * width + j]]++;  //将像素值作为计数数组的下标

        }

    }

    //计算每个像素在整幅图像中的比例 

    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;

}

本文参考了:https://www.cnblogs.com/ranjiewen/p/6385564.html

你可能感兴趣的:(大津法---OTSU算法--- THRESH_OTSU)