簡體   English   中英

CImg:圖像二值化結果失敗

[英]CImg: Image binarization result fails

因此,我下面的代碼中的問題是圖像二值化的結果變得太暗。 (我什至有一個示例圖像,其二進制圖像變成全黑。)

很長時間以來,我一直在代碼中搜索任何錯誤,但沒有發現任何看起來對我有問題的錯誤。

以下是我要二值化的圖像:

二進制化之前的圖像-在我的代碼中名為:“ hildebrantmed.bmp”

下面是生成的二進制圖像:

二值化后的圖像

在向您展示我的源代碼之前,這是圖像二值化的“規則”(因為這是我最近得到的一項工作):

  1. 除了CImg,我不允許使用任何其他庫。
  2. 使用的編程語言是C / C ++。 沒有別的了。
  3. 通常,選擇大津的方法。 但是,如果更好的話,可以允許我使用其他算法。

最后,這是我的源代碼:

#include <iostream>
#include <CImg.h>

using namespace std;
using namespace cimg_library;

/**
 * Generate histogram of the grayscale image
 */
int * generate_histogram(CImg<unsigned char> img)
{   
    int histogram[256];

    // initialize default values for histogram
    for (int i = 0; i < 256; i++) 
    {
        histogram[i] = 0;
    }

    // increment intensity for histogram
    for (int i = 0; i < img.height(); i++)
    {
        for (int j = 0; j < img.width(); j++)
        {
            int gray_value = img(j, i, 0, 0);
            histogram[gray_value]++;
        }
    }

    return histogram;
}

/**
 * Find threshold value from the grayscale image's histogram
 */
int otsu_threshold(CImg<unsigned char> img)
{
    int * histogram = generate_histogram(img); // image histogram

    int total = img.width() * img.height(); // total pixels

    double sum = 0;

    int i;
    for (i = 0; i < 256; i++)
    {
        sum += i * histogram[i];
    }

    double sumB = 0;
    int wB = 0;
    int wF = 0;

    double var_max = 0;
    int threshold = 0;

    for (i = 0; i < 256; i++)
    {
        wB += histogram[i];
        if (wB == 0) continue;

        wF = total - wB;
        if (wF == 0) continue;

        sumB += (double)(i * histogram[i]);

        double mB = sumB / wB;
        double mF = (sum - sumB) / wF;

        double var_between = (double)wB * (double)wF * (mB - mF) * (mB - mF);

        if (var_between > var_max)
        {
            var_max = var_between;
            threshold = i;
        }
    }

    return threshold;
}

/**
 * Main function
 */
int main(int argc, char * argv[])
{
    // retrieve image from its path
    CImg<unsigned char> img("hildebrantmed.bmp");

    const int width = img.width();
    const int height = img.height();

    // initialize a new image for img's grayscale
    CImg<unsigned char> gray_img(width, height, 1, 1, 0);

    // from RGB divided into three separate channels
    CImg<unsigned char> imgR(width, height, 1, 3, 0);
    CImg<unsigned char> imgG(width, height, 1, 3, 0);
    CImg<unsigned char> imgB(width, height, 1, 3, 0);

    // for all (x, y) pixels in image
    cimg_forXY(img, x, y)
    {
        imgR(x, y, 0, 0) = img(x, y, 0, 0),
        imgG(x, y, 0, 1) = img(x, y, 0, 1),
        imgB(x, y, 0, 2) = img(x, y, 0, 2);

        // separate the channels
        int R = (int)img(x, y, 0, 0);
        int G = (int)img(x, y, 0, 1);
        int B = (int)img(x, y, 0, 2);

        // obtain gray value from different weights of RGB channels
        int gray_value = (int)(0.299 * R + 0.587 * G + 0.114 * B);
        gray_img(x, y, 0, 0) = gray_value;
    }

    // find threshold of grayscale image
    int threshold = otsu_threshold(gray_img);

    // initialize a binary image version of img
    CImg<unsigned char> binary_img(width, height, 1, 1, 0);

    // for every (x, y) pixel in gray_img
    cimg_forXY(img, x, y)
    {
        int gray_value = gray_img(x, y, 0, 0);

        // COMPARE gray_value with threshold
        int binary_value;

        // gray_value > threshold: 255 (white)
        if (gray_value > threshold) binary_value = 255;
        // gray_value < threshold: 0 (black)
        else binary_value = 0;

        // assign binary_value to each of binary_img's pixels
        binary_img(x, y, 0, 0) = binary_value;
    }

    // display the images
    CImgDisplay src_disp(img, "Source image");
    CImgDisplay gray_disp(gray_img, "Grayscale image");
    CImgDisplay binary_disp(binary_img, "Binary image");

    while (!src_disp.is_closed() && !gray_disp.is_closed() && !binary_disp.is_closed())
    {
        src_disp.wait();
        gray_disp.wait();
    }

    return 0;
}

如果您發現其他算法會更好,請在答案中提供該算法和源代碼。 感謝您的關注。

第一個錯誤:您嘗試return的數組指針實際上在generate_histogram函數結束后立即被銷毀。 為了使其正常工作,您應該從調用函數中提供指向數組的指針,例如:

{
//[....]
int histogram[256];
generate_histogram(img, histogram);
//[....]
}

int * generate_histogram(CImg<unsigned char> img, int* arHistogram)
{
//[....]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM