1 /************************************************************************************
\r
4 * This program is demonstration for histogram and functions: cvCreateHist(),
\r
5 * cvSetHistThresh(), cvCalcHist(), cvNormalizeHist(), cvQueryHistValue_1D(),
\r
6 * cvReleaseHist() and cvCopyHist().
\r
9 * Trackbar specify normalize factor for histogram value.
\r
13 * Autor: Denis Burenkov
\r
16 ************************************************************************************/
\r
18 #define DIMENSION_SIZE 50
\r
20 char file_name[] = "EXPO0002.bmp";
\r
22 char wndname01[] = "Source image";
\r
23 char wndname02[] = "Histogram";
\r
24 char barname01[] = "Normalize factor";
\r
25 int dims[1] = {DIMENSION_SIZE};
\r
26 float thresh[1][2] = { {0, 255} };
\r
27 float* pthresh[1] = { thresh[0] };
\r
28 int slider_pos[1] = {2000};
\r
30 // Load the source image. HighGUI use.
\r
31 IPLIMAGE image01 = load_iplimage( file_name );
\r
35 printf("File '%s' not found.\n", file_name);
\r
39 // Create the destination images. HighGUI use.
\r
40 IplImage * image02 = cvCreateImage( cvSize(image01->width,image01->height),
\r
43 IplImage * image03 = cvCreateImage( cvSize(image01->width,image01->height),
\r
47 // Make onechannel image.
\r
48 cvCvtColor(image01,image02, CV_BGR2GRAY);
\r
51 // Created histogram headers. OpenCV use.
\r
52 CvHistogram* hist01 = cvCreateHist(1, // Histogram dimension number.
\r
53 dims, // Dimension size array.
\r
54 CV_HIST_ARRAY, // Histogram type.
\r
57 CvHistogram* hist02 = cvCreateHist(1, dims, CV_HIST_ARRAY, 0, 1);
\r
60 // Set bounds of histogram bins. The histogram or back project are calculated
\r
61 // if within the specified boundaries.
\r
63 cvSetHistBinRanges( hist01, // Destination histogram.
\r
64 pthresh, 1); // Pointer to the array of threshold values.
\r
68 // Create windows. HighGUI use.
\r
69 named_window( wndname01, 0 );
\r
70 named_window( wndname02, 0 );
\r
72 // Show the image. HighGUI use.
\r
73 show_iplimage( wndname01, image02 );
\r
76 // Calculated the histogram. OpenCV use.
\r
77 cvCalcHist( &image02, // Source images.
\r
78 hist01, // Pointer to the histogram.
\r
79 0, 0 ); // Clear flag.
\r
82 // Define trackbar callback functon. This function normalize and draw histogram.
\r
83 void ShowHist(int h)
\r
88 pt2.y=image01->height;
\r
90 // Clear image. OpenCV use.
\r
91 //cvFillImage(image03,0); //old code
\r
95 // Copy histogram to save data. OpenCV use.
\r
96 cvCopyHist(hist01, // Source histogram.
\r
97 &hist02); // Pointer to destination histogram.
\r
99 // Normalize histogram.
\r
100 // The sum of histogram bins becomes equal to factor.
\r
102 cvNormalizeHist( hist02, //Pointer to the histogram.
\r
103 slider_pos[0] ); //Normalize factor.
\r
106 for(i=0; i<DIMENSION_SIZE; i++)
\r
109 pt1.y=image01->height -
\r
110 cvQueryHistValue_1D(hist02, // Pointer to the source histogram.
\r
111 i); // Address of a required element.
\r
112 // The function cvQueryHistValue_1D() returns the
\r
113 // value of the specified histogram bin.
\r
117 cvLine(image03, pt1, pt2, CV_RGB(200,0,0), 2, 8);
\r
120 // Show the image. HighGUI use.
\r
121 show_iplimage( wndname02, image03 );
\r
128 // Create toolbars. HighGUI use.
\r
129 create_trackbar(barname01,
\r
135 // Wait for a key stroke; the same function arranges events processing
\r
137 cvReleaseImage(&image01);
\r
138 cvReleaseImage(&image02);
\r
139 cvReleaseImage(&image03);
\r
141 destroy_window(wndname01);
\r
142 destroy_window(wndname02);
\r
144 cvReleaseHist( &hist01 );
\r
145 cvReleaseHist( &hist02 );
\r