本文介绍了多 otsu(多阈值)与 openCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与 otsu 进行多阈值处理.我目前使用的方法实际上是通过最大化类之间的差异,我设法获得与 OpenCV 库给出的阈值相同的阈值.然而,那只是通过运行一次 otsu 方法.

关于如何进行多级阈值处理或递归阈值处理的文档相当有限.获得原始大津的价值后我该怎么做?希望得到一些提示,我一直在玩代码,添加一个外部 for 循环,但对于任何给定的图像,计算出的下一个值始终为 254:(

如果需要,我的代码是:

//先计算直方图简历::垫图像;//图像编辑为灰度直方图目的//图像=图像;//删除和取消下面的注释;cv::cvtColor(image, imageh, CV_BGR2GRAY);int histSize[1] = {256};//箱数浮动范围[2] = {0.0, 256.0};//最小和轴像素值const float* range[1] = {hranges};整数通道[1] = {0};//只使用了 1 个通道简历::MatND 历史;//计算直方图calcHist(&imageh, 1, 通道, cv::Mat(), hist, 1, histSize, range);IplImage* im = new IplImage(imageh);//将图像赋值给一个IplImage指针IplImage* finalIm = cvCreateImage(cvSize(im->width, im->height), IPL_DEPTH_8U, 1);double otsuThreshold= cvThreshold(im, finalIm, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU );cout<<"opencv otsu 给出"<<otsuThreshold<<endl;int totalNumberOfPixels= imageh.total();cout<<"总像素数是" <<totalNumberOfPixels<<结束;浮点数 = 0;对于 (int t=0 ; t(t);}cout<<"sum 是"<<sum<<endl;浮动 sumB = 0;//背景总和int wB = 0;//背景权重int wF = 0;//前景权重浮动 varMax = 0;int 阈值 = 0;//运行迭代以找到类间方差的最大值(如类间方差应最大化)对于 (int t=0 ; t
解决方案

为了将 Otsu 的阈值方法扩展到多级阈值,类间方差方程变为:

请查看 Deng-Yuan Huang, Ta-Wei Lin, Wu-Chih Hu, Automatic基于聚类的二阶段大津法的多级阈值化通过谷估计确定,Int.创新杂志Computing, 2011, 7:5631-5644 了解更多信息.

Otsu 的原论文:Nobuyuki Otsu, A Threshold Selection Method来自灰度直方图、IEEE Transactions on Systems、Man 和Cyber​​netics, 1979, 9:62-66" 还简要提到了对多阈值.

https://engineering.purdue.edu/kak/computervision/ECE661.08/OTSU_paper.pdf

希望这会有所帮助.

I am trying to carry out multi-thresholding with otsu. The method I am using currently is actually via maximising the between class variance, I have managed to get the same threshold value given as that by the OpenCV library. However, that is just via running otsu method once.

Documentation on how to do multi-level thresholding or rather recursive thresholding is rather limited. Where do I do after obtaining the original otsu's value? Would appreciate some hints, I been playing around with the code, adding one external for loop, but the next value calculated is always 254 for any given image:(

My code if need be:

//compute histogram first
cv::Mat imageh; //image edited to grayscale for histogram purpose
//imageh=image; //to delete and uncomment below;
cv::cvtColor(image, imageh, CV_BGR2GRAY);

int histSize[1] = {256}; // number of bins
float hranges[2] = {0.0, 256.0}; // min andax pixel value
const float* ranges[1] = {hranges};
int channels[1] = {0}; // only 1 channel used

cv::MatND hist;
// Compute histogram
calcHist(&imageh, 1, channels, cv::Mat(), hist, 1, histSize, ranges);

IplImage* im = new IplImage(imageh);//assign the image to an IplImage pointer
IplImage* finalIm = cvCreateImage(cvSize(im->width, im->height), IPL_DEPTH_8U, 1);
double otsuThreshold= cvThreshold(im, finalIm, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU );

cout<<"opencv otsu gives "<<otsuThreshold<<endl;

int totalNumberOfPixels= imageh.total();
cout<<"total number of Pixels is " <<totalNumberOfPixels<< endl;


float sum = 0;
for (int t=0 ; t<256 ; t++)
{
    sum += t * hist.at<float>(t);
}
cout<<"sum is "<<sum<<endl;

float sumB = 0; //sum of background
int wB = 0; // weight of background
int wF = 0; //weight of foreground

float varMax = 0;
int threshold = 0;

//run an iteration to find the maximum value of the between class variance(as between class variance shld be maximise)
for (int t=0 ; t<256 ; t++)
{
       wB += hist.at<float>(t);               // Weight Background
       if (wB == 0) continue;

       wF = totalNumberOfPixels - wB;                 // Weight Foreground
       if (wF == 0) break;

       sumB += (float) (t * hist.at<float>(t));

       float mB = sumB / wB;            // Mean Background
       float mF = (sum - sumB) / wF;    // Mean Foreground

       // Calculate Between Class Variance
       float varBetween = (float)wB * (float)wF * (mB - mF) * (mB - mF);

       // Check if new maximum found
       if (varBetween > varMax) {
          varMax = varBetween;
          threshold = t;
       }
}

       cout<<"threshold value is: "<<threshold;
解决方案

To extend Otsu's thresholding method to multi-level thresholding the between class variance equation becomes:

Here is my C# implementation of Otsu Multi for 2 thresholds:

/* Otsu (1979) - multi */

Tuple < int, int > otsuMulti(object sender, EventArgs e) {
    //image histogram
    int[] histogram = new int[256];

    //total number of pixels
    int N = 0;

    //accumulate image histogram and total number of pixels
    foreach(int intensity in image.Data) {
        if (intensity != 0) {
            histogram[intensity] += 1;
            N++;
        }
    }

    double W0K, W1K, W2K, M0, M1, M2, currVarB, optimalThresh1, optimalThresh2, maxBetweenVar, M0K, M1K, M2K, MT;

    optimalThresh1 = 0;
    optimalThresh2 = 0;

    W0K = 0;
    W1K = 0;

    M0K = 0;
    M1K = 0;

    MT = 0;
    maxBetweenVar = 0;
    for (int k = 0; k <= 255; k++) {
        MT += k * (histogram[k] / (double) N);
    }


    for (int t1 = 0; t1 <= 255; t1++) {
        W0K += histogram[t1] / (double) N; //Pi
        M0K += t1 * (histogram[t1] / (double) N); //i * Pi
        M0 = M0K / W0K; //(i * Pi)/Pi

        W1K = 0;
        M1K = 0;

        for (int t2 = t1 + 1; t2 <= 255; t2++) {
            W1K += histogram[t2] / (double) N; //Pi
            M1K += t2 * (histogram[t2] / (double) N); //i * Pi
            M1 = M1K / W1K; //(i * Pi)/Pi

            W2K = 1 - (W0K + W1K);
            M2K = MT - (M0K + M1K);

            if (W2K <= 0) break;

            M2 = M2K / W2K;

            currVarB = W0K * (M0 - MT) * (M0 - MT) + W1K * (M1 - MT) * (M1 - MT) + W2K * (M2 - MT) * (M2 - MT);

            if (maxBetweenVar < currVarB) {
                maxBetweenVar = currVarB;
                optimalThresh1 = t1;
                optimalThresh2 = t2;
            }
        }
    }

    return new Tuple(optimalThresh1, optimalThresh2);
}

And this is the result I got by thresholding an image scan of soil with the above code:

(T1 = 110, T2 = 147).

Hope this helps.

这篇关于多 otsu(多阈值)与 openCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 01:00