实验环境: VS2010 + OpenCV2.4.9.0
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
void showHistogram(Mat &src, int bins,char* wndName)
{
int histSize[] = {bins};
float range[] = {0,bins};
const float* ranges[] = {range};
MatND hist;
int channels[] = {0};
calcHist(&src, 1, channels, Mat(), hist, 1, histSize, ranges, true, false);
double maxVal;
minMaxLoc(hist, 0, &maxVal, 0, 0);
int scale = 2; //histogram size(width)
int histHeight = 256;
Mat histImage = Mat::zeros(histHeight,bins * scale,CV_8UC1);
for(int i = 0; i < bins; i++)
{
float binVal = hist.at<float>(i);
int intensity = cvRound(binVal * histHeight/maxVal);
rectangle(histImage,Point(i * scale,histHeight - 1),
Point((i + 1)*scale - 1,histHeight - intensity),CV_RGB(255,255,255));
}
namedWindow(wndName,CV_WINDOW_AUTOSIZE);
i