Created
September 29, 2013 18:03
-
-
Save BloodAxe/6754956 to your computer and use it in GitHub Desktop.
Color image histogram in OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void drawHistogram(cv::Mat src, cv::Mat& histImage) | |
{ | |
using namespace cv; | |
using namespace std; | |
/// Separate the image in 3 places ( B, G and R ) | |
vector<Mat> bgr_planes; | |
split( src, bgr_planes ); | |
/// Establish the number of bins | |
int histSize = 256; | |
/// Set the ranges ( for B,G,R) ) | |
float range[] = { 0, 256 } ; | |
const float* histRange = { range }; | |
bool uniform = true; bool accumulate = false; | |
Mat b_hist, g_hist, r_hist; | |
/// Compute the histograms: | |
calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate ); | |
calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate ); | |
calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate ); | |
// Draw the histograms for B, G and R | |
int hist_w = 512; int hist_h = 200; | |
int bin_w = cvRound( (double) hist_w/histSize ); | |
histImage.create( hist_h, hist_w, CV_8UC4); | |
histImage = Scalar::all(0); | |
/// Normalize the result to [ 0, histImage.rows ] | |
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); | |
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); | |
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); | |
/// Draw for each channel | |
for( int i = 1; i < histSize; i++ ) | |
{ | |
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) , | |
Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ), | |
Scalar( 255, 0, 0, 255), 2, CV_AA, 0 ); | |
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) , | |
Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ), | |
Scalar( 0, 255, 0, 255), 2, CV_AA, 0 ); | |
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) , | |
Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ), | |
Scalar( 0, 0, 255, 255), 2, CV_AA, 0 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment