使用 Opencv 绘制彩色图像的直方图

发布于 2024-10-18 02:22:10 字数 1703 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攀登最高峰 2024-10-25 02:22:10

请参阅用 C++ 编写的 vz::plot_histogram()。 https://www.ccoderun.ca/programming/doxygen/vz_imagination/ namespacevz.html#a4cf3b5cd26b8cc8ac28fdbb8355f56a5

如果链接失效,相关代码如下所示:

// "src" is the cv::Mat input image
const size_t number_of_channels = src.channels();
const cv::Scalar background_colour(0,0,0);

std::vector<cv::Mat> split;
cv::split(src, split);

const int       height          = 480;
const int       width           = 640;
const int       histogram_size  = 256; // the number of "bins"
const float     range[]         = {0, 256}; // upper bound is exclusive, meaning 0-255
const float *   ranges          = {range};
const bool      uniform         = true;
const bool      accumulate      = false;
cv::Mat         mask;

// prepare the destination image
const int margin        = 3;
const int min_y         = margin;
const int max_y         = height - margin;
const int thickness     = 1;
const int line_type     = cv::LINE_AA;
const float bin_width   = static_cast<float>(width) / static_cast<float>(histogram_size);
cv::Mat dst(height, width, CV_8UC3, background_colour); // create the output image, starting with a pure colour

cv::Scalar colours[] =
{
    {255, 0, 0},    // blue
    {0, 255, 0},    // green
    {0, 0, 255}     // red
};
if (number_of_channels == 1)
{
    // for greyscale images, we only have a single colour channel, so ignore
    // the RGB colour definitions and use either black or white for the histogram

    colours[0] = (background_colour == cv::Scalar(0, 0, 0)) ?
            cv::Scalar(255, 255, 255) :
            cv::Scalar(0, 0, 0);
}

// iterate through all the channels in this image
for (size_t idx=0; idx < split.size(); idx++)
{
    const cv::Scalar colour = colours[idx % 3];

    cv::Mat & m = split[idx];

    cv::Mat histogram;
    cv::calcHist(&m, 1, 0, mask, histogram, 1, &histogram_size, &ranges, uniform, accumulate);

    cv::normalize(histogram, histogram, 0, dst.rows, cv::NORM_MINMAX);

    for (int i = 1; i < histogram_size; i++)
    {
        const int x1 = std::round(bin_width * (i - 1));
        const int x2 = std::round(bin_width * (i - 0));

        const int y1 = std::clamp(height - static_cast<int>(std::round(histogram.at<float>(i - 1))), min_y, max_y);
        const int y2 = std::clamp(height - static_cast<int>(std::round(histogram.at<float>(i - 0))), min_y, max_y);

        cv::line(dst, cv::Point(x1, y1), cv::Point(x2, y2), colour, thickness, line_type);
    }
}

// histogram is in "dst"

生成的直方图应如下所示: 颜色直方图

See vz::plot_histogram(), written in C++. https://www.ccoderun.ca/programming/doxygen/vz_imagination/namespacevz.html#a4cf3b5cd26b8cc8ac28fdbb8355f56a5

In case the link goes dead, the relevant code looks like this:

// "src" is the cv::Mat input image
const size_t number_of_channels = src.channels();
const cv::Scalar background_colour(0,0,0);

std::vector<cv::Mat> split;
cv::split(src, split);

const int       height          = 480;
const int       width           = 640;
const int       histogram_size  = 256; // the number of "bins"
const float     range[]         = {0, 256}; // upper bound is exclusive, meaning 0-255
const float *   ranges          = {range};
const bool      uniform         = true;
const bool      accumulate      = false;
cv::Mat         mask;

// prepare the destination image
const int margin        = 3;
const int min_y         = margin;
const int max_y         = height - margin;
const int thickness     = 1;
const int line_type     = cv::LINE_AA;
const float bin_width   = static_cast<float>(width) / static_cast<float>(histogram_size);
cv::Mat dst(height, width, CV_8UC3, background_colour); // create the output image, starting with a pure colour

cv::Scalar colours[] =
{
    {255, 0, 0},    // blue
    {0, 255, 0},    // green
    {0, 0, 255}     // red
};
if (number_of_channels == 1)
{
    // for greyscale images, we only have a single colour channel, so ignore
    // the RGB colour definitions and use either black or white for the histogram

    colours[0] = (background_colour == cv::Scalar(0, 0, 0)) ?
            cv::Scalar(255, 255, 255) :
            cv::Scalar(0, 0, 0);
}

// iterate through all the channels in this image
for (size_t idx=0; idx < split.size(); idx++)
{
    const cv::Scalar colour = colours[idx % 3];

    cv::Mat & m = split[idx];

    cv::Mat histogram;
    cv::calcHist(&m, 1, 0, mask, histogram, 1, &histogram_size, &ranges, uniform, accumulate);

    cv::normalize(histogram, histogram, 0, dst.rows, cv::NORM_MINMAX);

    for (int i = 1; i < histogram_size; i++)
    {
        const int x1 = std::round(bin_width * (i - 1));
        const int x2 = std::round(bin_width * (i - 0));

        const int y1 = std::clamp(height - static_cast<int>(std::round(histogram.at<float>(i - 1))), min_y, max_y);
        const int y2 = std::clamp(height - static_cast<int>(std::round(histogram.at<float>(i - 0))), min_y, max_y);

        cv::line(dst, cv::Point(x1, y1), cv::Point(x2, y2), colour, thickness, line_type);
    }
}

// histogram is in "dst"

Resulting histograms should look something like this: colour histogram

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文