Key points:
1. Read the camera
2. The R, G, and B channels (cvSplit) are separated from a colored image)
3. Create a histogram for each channel image and display
The source code is as follows:
// Camera_Capture_Histogram.cpp: Defines the entry point for the console application. // author: JarvisChu // date: 2012-1-14 # include "stdafx. h "# include" opencv2/opencv. hpp "# include <iostream> using namespace std; const char * pstrSrcWnd =" Src "; const char * pstrBHistWnd =" B plane "; const char * pstrGHistWnd =" g plane "; const char * pstrRHistWnd = "r plane"; // calculate and plot the histogram (R, G, B)/* img channel image * hist_img: The plotting image of the Histogram * PstrWndName: Drawing window */void draw_histogram (IplImage * img, IplImage * hist_img, const char * pstrWndName) {CvHistogram * hist = NULL; int bin_count = 256; float range [] = {0,255}; float * ranges [] = {range}; hist = cvCreateHist (1, // One-dimensional & bin_count, // The number of bin (straight square column) on each dimension. Here it is 256. [by the two parameters above, the function creates a 1*256 matrix.] CV_HIST_ARRAY, ranges, 1 ); cvClearHist (hist); // prevent other data from initialization. Clear cvCalcHist (& img, hist,) first; // obtain the maximum value and label float min and max of the histogram; Int min_idx, max_idx; cvGetMinMaxHistValue (hist, & min, & max, & min_idx, & max_idx); // cout <"min:" <min <"max: "<max <endl; if (max = 0) {cout <" max = 0 err! "<Endl; max = 100;} // scale the histogram to adapt to cvScale (hist-> bins, hist-> bins, (double) hist_img-> height)/max, 0); // set the values of all histograms to 255 cvSet (hist_img, cvScalarAll (255), 0 ); // average width of each direct column int bin_w = cvRound (double) hist_img-> width/bin_count); // draw a histogram for (int I = 0; I <bin_count; I ++) {cvRectangle (hist_img, cvPoint (I * bin_w, hist_img-> height), // the point in the lower left corner (I * bin_w, height) cvPoint (I + 1) * bin_w, hist_img-> height-cvRound (cvGetReal1D (hist-> bins, I), // point in the upper right corner (I + 1) * bin_w, Image Height-height of the straight square column) cvScalarAll (0),-, 0 );} // display the histogram cvShowImage (pstrWndName, hist_img);} int _ tmain (int argc, _ TCHAR * argv []) {// create a window cvNamedWindow (pstrSrcWnd, 1 ); cvNamedWindow (pstrBHistWnd, 1); cvNamedWindow (pstrGHistWnd, 1); cvNamedWindow (pstrRHistWnd, 1); // read the camera CvCapture * capture = camera (-1); if (! Capture) cout <"unable to enable camera" <endl; // read a frame of data to obtain the parameter IplImage * frame = NULL; frame = cvQueryFrame (capture ); // B g r channel CvSize img_size; img_size.width = frame-> width; img_size.height = frame-> height; IplImage * B = cvCreateImage (img_size, 8, 1 ); iplImage * g = cvCreateImage (img_size, 8, 1); IplImage * r = cvCreateImage (img_size, 8, 1); // histogram CvSize size; size. width = 320; size. height = 200; IplImage * B _hist_img = cvCreateImage (s Ize, 8, 1); IplImage * g_hist_img = cvCreateImage (size, 8, 1); IplImage * r_hist_img = cvCreateImage (size, 8, 1); while (1) {frame = cvQueryFrame (capture ); if (! Frame) break; // split the BGR channel cvSplit (frame, B, g, r, 0); // display the camera image cvShowImage (pstrSrcWnd, frame ); // draw the histogram draw_histogram (B, B _hist_img, histogram); draw_histogram (g, g_hist_img, histogram); draw_histogram (r, r_hist_img, histogram); char c = waitcvkey (330 ); if (c = 27) break;} cvReleaseImage (& frame); cvReleaseImage (& B); cvReleaseImage (& g); cvReleaseImage (& r); cvReleaseImage (& B ); cvReleaseImage (& g); cvReleaseImage (& r); cvDestroyAllWindows (); return 0 ;}
The effect is as follows: