OpenCV只获得像Matlab函数graythresh的Otsu阈值

时间:2015-02-09 09:02:17

标签: opencv image-processing

如何使用OpenCV获取Otsu Threshold值,如Matlab函数graythresh。

例如,如果我编写这段代码,我会得到二进制图像,就像使用Matlab函数im2bw:

一样
Mat bw;
uint8_t data[14] = { 10, 10, 10, 10, 10, 10, 20, 20, 54, 54, 54, 54, 55, 55 };
Mat M(1, 14, CV_8UC1, &data);
threshold(M, bw, 0, 1, CV_THRESH_BINARY | CV_THRESH_OTSU);

2 个答案:

答案 0 :(得分:4)

如果设置cv::threshold()标志,函数THRESH_OTSU将返回计算的阈值。

double thres_val = cv::threshold(M, bw, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

有关详细信息,请参阅cv::threshold()

答案 1 :(得分:2)

如果你想避免执行阈值而只想要值,那就去OpenCV里面做什么:

double getThreshVal_Otsu_8u( const cv::Mat& _src )
{
    cv::Size size = _src.size();
    if ( _src.isContinuous() )
    {
        size.width *= size.height;
        size.height = 1;
    }
    const int N = 256;
    int i, j, h[N] = {0};
    for ( i = 0; i < size.height; i++ )
    {
        const uchar* src = _src.data + _src.step*i;
        for ( j = 0; j <= size.width - 4; j += 4 )
        {
            int v0 = src[j], v1 = src[j+1];
            h[v0]++; h[v1]++;
            v0 = src[j+2]; v1 = src[j+3];
            h[v0]++; h[v1]++;
        }
        for ( ; j < size.width; j++ )
            h[src[j]]++;
    }

    double mu = 0, scale = 1./(size.width*size.height);
    for ( i = 0; i < N; i++ )
        mu += i*h[i];

    mu *= scale;
    double mu1 = 0, q1 = 0;
    double max_sigma = 0, max_val = 0;

    for ( i = 0; i < N; i++ )
    {
        double p_i, q2, mu2, sigma;

        p_i = h[i]*scale;
        mu1 *= q1;
        q1 += p_i;
        q2 = 1. - q1;

        if ( std::min(q1,q2) < FLT_EPSILON || std::max(q1,q2) > 1. - FLT_EPSILON )
            continue;

        mu1 = (mu1 + i*p_i)/q1;
        mu2 = (mu - q1*mu1)/q2;
        sigma = q1*q2*(mu1 - mu2)*(mu1 - mu2);
        if ( sigma > max_sigma )
        {
            max_sigma = sigma;
            max_val = i;
        }
    }

    return max_val;
}

将其放在您自己的命名空间或匿名命名空间中,以避免可能的冲突。