【问题标题】:compare histograms of grayscale images in opencv比较opencv中灰度图像的直方图
【发布时间】:2013-03-24 04:25:19
【问题描述】:

嗨,任何人都可以为我提供一个简单的开放式 cv 程序来加载两个 RGB 图像,将其转换为灰度,计算直方图,然后比较它们的直方图。我在 open cv 网站上看到了一个类似的程序,但他们使用 HSV 而不是灰度,它是一个 c++ 程序。我可以查看流程和所有内容......我不知道要使用哪些函数以及它们的参数意味着什么...... 问候, 基兰

【问题讨论】:

  • “比较直方图”是什么意思?

标签: c opencv histogram


【解决方案1】:

这里是做这件事的简单代码 sn-p。由于您没有告诉您要如何比较直方图,我建议您直观地进行。

#include <opencv2/opencv.hpp>

void show_histogram(std::string const& name, cv::Mat1b const& image)
{
    // Set histogram bins count
    int bins = 256;
    int histSize[] = {bins};
    // Set ranges for histogram bins
    float lranges[] = {0, 256};
    const float* ranges[] = {lranges};
    // create matrix for histogram
    cv::Mat hist;
    int channels[] = {0};

    // create matrix for histogram visualization
    int const hist_height = 256;
    cv::Mat3b hist_image = cv::Mat3b::zeros(hist_height, bins);

    cv::calcHist(&image, 1, channels, cv::Mat(), hist, 1, histSize, ranges, true, false);

    double max_val=0;
    minMaxLoc(hist, 0, &max_val);

    // visualize each bin
    for(int b = 0; b < bins; b++) {
        float const binVal = hist.at<float>(b);
        int   const height = cvRound(binVal*hist_height/max_val);
        cv::line
            ( hist_image
            , cv::Point(b, hist_height-height), cv::Point(b, hist_height)
            , cv::Scalar::all(255)
            );
    }
    cv::imshow(name, hist_image);
}

int main (int argc, const char* argv[])
{
    // here you can use cv::IMREAD_GRAYSCALE to load grayscale image, see image2
    cv::Mat3b const image1 = cv::imread("C:\\workspace\\horse.png", cv::IMREAD_COLOR);
    cv::Mat1b image1_gray;
    cv::cvtColor(image1, image1_gray, cv::COLOR_BGR2GRAY);
    cv::imshow("image1", image1_gray);
    show_histogram("image1 hist", image1_gray);

    cv::Mat1b const image2 = cv::imread("C:\\workspace\\bunny.jpg", cv::IMREAD_GRAYSCALE);
    cv::imshow("image2", image2);
    show_histogram("image2 hist", image2);

    cv::waitKey();
    return 0;
}

结果:

【讨论】:

  • 非常感谢哥们...对不起,如果我含糊其辞,但您给了我想要的东西...再次感谢。
  • 请接受答案。谢谢。
【解决方案2】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2015-05-01
    • 1970-01-01
    • 2021-06-01
    • 2014-09-15
    • 2019-08-28
    • 1970-01-01
    相关资源
    最近更新 更多