为图像绘制直方图,效果图如下:
代码如下:
// Separate image in BRg
vector<Mat> bgr;
split(img, bgr);
// Create the histogram for 256 bins
// The number of possibles values [0..255]
int numbins = 256;
/// Set the ranges ( for B,g,R) ), last is not included
float range[] = { 0, 256 };
const float* histRange = { range };
Mat b_hist, g_hist, r_hist;
calcHist(&bgr[0], 1, 0, Mat(), b_hist, 1, &numbins,
&histRange);
calcHist(&bgr[1], 1, 0, Mat(), g_hist, 1, &numbins,
&histRange);
calcHist(&bgr[2], 1, 0, Mat(), r_hist, 1, &numbins,
&histRange);
// Draw the histogram
// We go to draw lines for each channel
int width = 512;
int height = 300;
// Create image with gray base
Mat histImage(height, width, CV_8UC3, Scalar(20, 20, 20));
cout << histImage.size();
// normalize the histograms to height of image
normalize(b_hist, b_hist, 0, height, NORM_MINMAX);
normalize(g_hist, g_hist, 0, height, NORM_MINMAX);
normalize(r_hist, r_hist, 0, height, NORM_MINMAX);
int binStep = cvRound((float)width / (float)numbins);
for (int i = 1; i< numbins; i++)
{
line(histImage,
Point(binStep*(i - 1), height - cvRound(b_hist.at<float>(i - 1))),
Point(binStep*(i), height - cvRound(b_hist.at<float>(i))),
Scalar(255, 0, 0));
line(histImage,
Point(binStep*(i - 1), height - cvRound(g_hist.at<float>(i - 1))),
Point(binStep*(i), height - cvRound(g_hist.at<float>(i))),
Scalar(0, 255, 0));
line(histImage,
Point(binStep*(i - 1), height - cvRound(r_hist.at<float>(i - 1))),
Point(binStep*(i), height - cvRound(r_hist.at<float>(i))),
Scalar(0, 0, 255));
}
imshow("Histogram", histImage);