Drawing histogram of hue channel of picture

asked 2015-03-17 03:11:23 -0600

215 gravatar image

updated 2015-03-17 11:27:24 -0600

Hi guys... This question might be marked as noob question.. I have been struggling with this problem.. I manages to calculate Histogram of a picture, but cannot show/display it ... How do i do that..

This is How i calculated it

void track(Rect face ){
Mat video,deepRoiExplorer;
MatND hist_base;
VideoCapture cam(0);
int h_bins = 50;
int histSize[] = {h_bins};
float h_ranges[] = { 0, 180 };
const float* ranges[] = { h_ranges};

if (cam.read(video)) {
    cvtColor(video, video, CV_BGR2HSV);
    Mat ROI(video,face);
    flip(ROI, ROI, 1);
    deepRoiExplorer =ROI.clone();
}
calcHist( &deepRoiExplorer, 1, 0, Mat(), hist_base, 1, histSize, ranges, true, false);
cout << "hello" << endl;

double total;
total = deepRoiExplorer.rows*deepRoiExplorer.cols;
for (int h = 0; h<h_bins; h++) {
    float binVal = hist_base.at<float>(h);
    cout<<" "<<binVal;
}
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/h_bins );
Mat histImage( hist_h, hist_w, CV_8UC1, Scalar( 0,0,0) );
normalize(hist_base, hist_base, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

for( int i = 1; i < h_bins; i++ )
{
    line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist_base.at<float>(i-1)) ) ,
         Point( bin_w*(i), hist_h - cvRound(hist_base.at<float>(i)) ),
         Scalar( 255, 0, 0), 2, 8, 0  );
}

while (cam.read(video)) {
    rectangle(video, face, CV_RGB(128,128,128));
    flip(video,video, 1);
    imshow("video", video);
    imshow("ROI", deepRoiExplorer);
    imshow( "Result", histImage );
}

}

edit retag flag offensive close merge delete

Comments

Have you seen this page? Your histogram shall be calculated the same way.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-17 03:16:22 -0600 )edit

I was able to do RGB but the HSV is causing the problem..

215 gravatar image215 ( 2015-03-17 03:32:56 -0600 )edit

Maybe CV_BGR2HSV_FULL is the problem?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-17 03:50:52 -0600 )edit

The problem is i cannot imshow the histogram..
Imshow("hist",hist_base) gives out the error message No matching function for call to imshow.

215 gravatar image215 ( 2015-03-17 10:46:58 -0600 )edit

shouldn't be ranges {0, 256}? histSize and ranges shouldn't be given as ref (&histSize and &ranges)? &ROI shouldn't be &ROI[0]? And more, you have just the values in hist_base.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-17 11:14:58 -0600 )edit

I changed it above.. But now i am not able to calc. the histogram...

215 gravatar image215 ( 2015-03-17 11:28:19 -0600 )edit