c++ - CImg : Image binarization result fails

标签 c++ image algorithm image-processing

因此,我以下代码中的问题是图像二值化的结果变得太暗。 (我什至有一个示例图像,其二进制图像变成全黑。)

很长一段时间以来,我一直在寻找我的代码中的任何错误,但没有发现任何对我来说看起来有问题的错误。

下面是我要二值化的图像:

Image before binarized - in my code is named: "hildebrantmed.bmp"

下面是生成的二进制图像:

Image after binarized

在我向您展示我的源代码之前,这里是图像二值化中的“规则”(因为这是我最近收到的作业):

  1. 除 CImg 外,我不得使用任何其他库。
  2. 要使用的编程语言是 C/C++。没有别的。
  3. 通常情况下,Otsu 的方法是首选。但是,如果更好的话,我可能会被允许使用其他算法。

最后,这是我的源代码:

#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;
}

如果您发现另一种算法效果更好,请在您的回答中提供算法和源代码。感谢您的关注。

最佳答案

第一个错误:您试图返回 一个数组的指针,该指针实际上在generate_histogram 函数结束后立即被销毁。 为了使其正常工作,您应该从调用函数提供指向数组的指针,例如:

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

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

关于c++ - CImg : Image binarization result fails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33693378/

相关文章:

c++ - 按指定的顺序迭代元组的元素

C# 和 C++,从 C# 调用 C++ dll 时出现运行时错误

c++ - 在没有库的情况下用 C 解析 XML。

algorithm - AVL树是邪恶的吗?

c++ - 我应该创建多少个线程?

c++ - 扩展类

image - 数学 : Transparent background with PNG

c# - 将大量数据传递到 aspx 站点并返回图像

php - 如何检查文件是否为图像?

algorithm - 5GB文件可供读取