/** * Code sample for displaying image histogram in OpenCV. */ #include #include #include using namespace cv; using namespace std; void showHistogram(Mat& img) { int bins = 256; // number of bins int nc = img.channels(); // number of channels vector hist(nc); // histogram arrays // Initalize histogram arrays for (int i = 0; i < hist.size(); i++) hist[i] = Mat::zeros(1, bins, CV_32SC1); // Calculate the histogram of the image for (int i = 0; i < img.rows; i++) { for (int j = 0; j < img.cols; j++) { for (int k = 0; k < nc; k++) { uchar val = nc == 1 ? img.at(i,j) : img.at(i,j)[k]; hist[k].at(val) += 1; } } } // For each histogram arrays, obtain the maximum (peak) value // Needed to normalize the display later int hmax[3] = {0,0,0}; for (int i = 0; i < nc; i++) { for (int j = 0; j < bins-1; j++) hmax[i] = hist[i].at(j) > hmax[i] ? hist[i].at(j) : hmax[i]; } const char* wname[3] = { "blue", "green", "red" }; Scalar colors[3] = { Scalar(255,0,0), Scalar(0,255,0), Scalar(0,0,255) }; vector canvas(nc); // Display each histogram in a canvas for (int i = 0; i < nc; i++) { canvas[i] = Mat::ones(125, bins, CV_8UC3); for (int j = 0, rows = canvas[i].rows; j < bins-1; j++) { line( canvas[i], Point(j, rows), Point(j, rows - (hist[i].at(j) * rows/hmax[i])), nc == 1 ? Scalar(200,200,200) : colors[i], 1, 8, 0 ); } imshow(nc == 1 ? "value" : wname[i], canvas[i]); } } // Test the `showHistogram()` function above int main() { Mat src = imread("c:/users/nash/desktop/assets/lena.jpg"); if (src.empty()) return -1; showHistogram(src); imshow("src", src); waitKey(0); return 0; }