int main()
{
Mat src;
src = imread("lenna.bmp", IMREAD_GRAYSCALE);
if (src.empty()) {
cerr << "Image load failed!" << endl;
return -1;
}
int hist[256] = {};
for(int j = 0; j < src.rows; j++) {
for(int i = 0; i < src.cols; i++) {
hist[src.at<uchar>(j,i)]++;
}
}
int histMax = 0;
for(int i = 0; i < 256; i++) {
if(hist[i] > histMax) histMax = hist[i];
}
Mat imgHist(100, 256, CV_8UC1, Scalar(255));
for(int i=0; i < 256; i++) {
line(imgHist, Point(i, 100), Point(i, 100-cvRound(hist[i]*100/histMax)), Scalar(0));
}
imshow("src", src);
imshow("histogram", imgHist);
waitKey();
}
void calcHist(const Mat* images, int nimages, const int* channels,
InputArray mask, OutputArray hist, int dims, const int* histSize,
const float** ranges, bool uniform = true, bool accumulate = false);