C++ 用 Opencv 绘制彩色图像的直方图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5050612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw Histogram of a colored Image with Opencv
提问by BlackShadow
anyone can link me or give an example of code in C / C + + that creates histograms for a colored image (RGB) and displays them?
任何人都可以链接我或给出 C/C++ 中的代码示例,它为彩色图像(RGB)创建直方图并显示它们?
采纳答案by all_by_grace
The following is a very nice tutorials on drawing Histogram with Open CV:
http://aishack.in/tutorials/drawing-histograms-opencv/
以下是使用 Open CV 绘制直方图的非常好的教程:http:
//aishack.in/tutorials/drawing-histograms-opencv/
The codes is easy to follow too !
这些代码也很容易遵循!
回答by SSteve
The OpenCV 2.3 documentation has a new tutorial on plotting a histogram for a color image.
OpenCV 2.3 文档有一个关于为彩色图像绘制直方图的新教程。
回答by Falco
The best link Ive found so far:
到目前为止我找到的最好的链接:
http://opencv-code.com/tutorials/drawing-histograms-in-opencv/
http://opencv-code.com/tutorials/drawing-histograms-in-opencv/
回答by Stéphane
See vz::plot_histogram(), written in C++. https://www.ccoderun.ca/programming/doxygen/vz_imagination/namespacevz.html#a4cf3b5cd26b8cc8ac28fdbb8355f56a5
请参阅用 C++ 编写的 vz::plot_histogram()。 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"
回答by Damilola
I learnt from these, they might help ->
我从这些中学到了,它们可能会有所帮助->
and
和
回答by mschneider
opencv has builtin histograms. the documentation includes an example: http://opencv.willowgarage.com/documentation/cpp/histograms.html
opencv 有内置的直方图。该文档包括一个示例:http: //opencv.willowgarage.com/documentation/cpp/histograms.html