首先放上测试图片:
基于OpenCV的直方图显示代码:
Mat draw1DHistgoram(Mat &image, int channel){
float channel_range[] = { 0.0, 255.0 };
const float* channel_ranges = channel_range;
bool uniform = true; bool accumulate = false;
int histSize = 255;
// Draw the histograms
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat display_image(hist_h, hist_w, CV_8UC3, Scalar(0));
if (channel == 1){
CV_Assert(image.channels() == 1);
Mat hist;
calcHist(&image, 1, 0, Mat(), hist,
1, &histSize, &channel_ranges, uniform, accumulate);
/// Normalize the result to [ 0, histImage.rows ]
normalize(hist, hist, 0, display_image.rows, NORM_MINMAX, -1, Mat());
for (int i = 1; i < histSize; ++i)
{
line(display_image, Point(bin_w*(i - 1), hist_h - cvRound(hist.