【问题标题】:Segmentation with Automatic Thresholding自动阈值分割
【发布时间】:2017-08-28 17:16:54
【问题描述】:

我想通过 C++ 使用自动阈值算法进行图像分割,但我的代码没有处理我预期的图像。我使用用户定义的类来加载和保存 BMP,它对我来说效果很好。它对某些图像有好处,但对某些图像也不起作用。你建议我开发我的代码。这是我的代码:

int Segmentation::getThreshold() {

    int distance[256] = { 0 };
    int t1 = 10;
    int t2 = 200;
    int t1O = 0;
    int t2O = 0;
    int threshold = 0;
    int meanT1 = 1000;
    int meanT2 = 2000;
    double sd1 = 1;
    double sd2 = 1;

    while (t1 != t1O || t2 != t2O) {

        int weighT1 = 0;
        int weighT2 = 0;
        meanT1 = 0;
        meanT2 = 0;

        for (int i = 0; i < 256; i++) {
            if (abs(histogram[t1] - histogram[i]) < abs(histogram[t2] - histogram[i]))
                distance[i] = 1;
            if (abs(histogram[t1] - histogram[i]) >= abs(histogram[t2] - histogram[i]))
                distance[i] = 2;
        }

        for (int j = 0; j < 256; j++) {
            if (distance[j] == 1) {
                meanT1 += histogram[j] * j;
                weighT1 += histogram[j];
            }
            if (distance[j] == 2) {
                meanT2 += histogram[j] * j;
                weighT2 += histogram[j];
            }
        }

        meanT1 = meanT1 / weighT1;
        meanT2 = meanT2 / weighT2;

        if (histogram[meanT1] != histogram[t1O] || histogram[meanT2] != histogram[t2O]) {
            t1O = t1;
            t1 = meanT1;
            t2O = t2;
            t2 = meanT2;
        }
        else {
            t1 = meanT1;
            t2 = meanT2;
            break;
        }


    }
    threshold = (t1 + t2) / 2;
    cout << "Threshold is: " << threshold << endl;
    return threshold;

    void Segmentation::getSegmentation() {

        int threshold;
        threshold = getThreshold();

        for (int i = 0; i<width; i++)
            for (int j = 0; j < height; j++) {
                if (histogram[img[width*i + j]] > histogram[threshold])
                    img[width*i + j] = 0;
                else
                    img[width*i + j] = 255;
            }
    }

【问题讨论】:

    标签: c++ image-processing image-segmentation


    【解决方案1】:

    替换

    histogram[img[width*i + j]] > histogram[threshold]
    

    通过

    img[width*i + j] > threshold
    

    【讨论】:

    • 谢谢,伙计。它提供了更好的结果。
    猜你喜欢
    • 1970-01-01
    • 2019-10-16
    • 2014-06-09
    • 2018-01-12
    • 1970-01-01
    • 2015-05-09
    • 2020-02-28
    • 2023-02-10
    • 1970-01-01
    相关资源
    最近更新 更多