Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / c / demhist.c
1 #ifdef _CH_
2 #pragma package <opencv>
3 #endif
4
5 #define CV_NO_BACKWARD_COMPATIBILITY
6
7 #ifndef _EiC
8 #include "cv.h"
9 #include "highgui.h"
10 #include <stdio.h>
11 #endif
12
13 char file_name[] = "baboon.jpg";
14
15 int _brightness = 100;
16 int _contrast = 100;
17
18 int hist_size = 64;
19 float range_0[]={0,256};
20 float* ranges[] = { range_0 };
21 IplImage *src_image = 0, *dst_image = 0, *hist_image = 0;
22 CvHistogram *hist;
23 uchar lut[256];
24 CvMat* lut_mat;
25
26 /* brightness/contrast callback function */
27 void update_brightcont( int arg )
28 {
29     int brightness = _brightness - 100;
30     int contrast = _contrast - 100;
31     int i, bin_w;
32     float max_value = 0;
33
34     /*
35      * The algorithm is by Werner D. Streidt
36      * (http://visca.com/ffactory/archives/5-99/msg00021.html)
37      */
38     if( contrast > 0 )
39     {
40         double delta = 127.*contrast/100;
41         double a = 255./(255. - delta*2);
42         double b = a*(brightness - delta);
43         for( i = 0; i < 256; i++ )
44         {
45             int v = cvRound(a*i + b);
46             if( v < 0 )
47                 v = 0;
48             if( v > 255 )
49                 v = 255;
50             lut[i] = (uchar)v;
51         }
52     }
53     else
54     {
55         double delta = -128.*contrast/100;
56         double a = (256.-delta*2)/255.;
57         double b = a*brightness + delta;
58         for( i = 0; i < 256; i++ )
59         {
60             int v = cvRound(a*i + b);
61             if( v < 0 )
62                 v = 0;
63             if( v > 255 )
64                 v = 255;
65             lut[i] = (uchar)v;
66         }
67     }
68
69     cvLUT( src_image, dst_image, lut_mat );
70     cvShowImage( "image", dst_image );
71
72     cvCalcHist( &dst_image, hist, 0, NULL );
73     cvZero( dst_image );
74     cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
75     cvScale( hist->bins, hist->bins, ((double)hist_image->height)/max_value, 0 );
76     /*cvNormalizeHist( hist, 1000 );*/
77
78     cvSet( hist_image, cvScalarAll(255), 0 );
79     bin_w = cvRound((double)hist_image->width/hist_size);
80
81     for( i = 0; i < hist_size; i++ )
82         cvRectangle( hist_image, cvPoint(i*bin_w, hist_image->height),
83                      cvPoint((i+1)*bin_w, hist_image->height - cvRound(cvGetReal1D(hist->bins,i))),
84                      cvScalarAll(0), -1, 8, 0 );
85
86     cvShowImage( "histogram", hist_image );
87 }
88
89
90 int main( int argc, char** argv )
91 {
92     // Load the source image. HighGUI use.
93     src_image = cvLoadImage( argc == 2 ? argv[1] : file_name, 0 );
94
95     if( !src_image )
96     {
97         printf("Image was not loaded.\n");
98         return -1;
99     }
100
101     dst_image = cvCloneImage(src_image);
102     hist_image = cvCreateImage(cvSize(320,200), 8, 1);
103     hist = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
104     lut_mat = cvCreateMatHeader( 1, 256, CV_8UC1 );
105     cvSetData( lut_mat, lut, 0 );
106
107     cvNamedWindow("image", 0);
108     cvNamedWindow("histogram", 0);
109
110     cvCreateTrackbar("brightness", "image", &_brightness, 200, update_brightcont);
111     cvCreateTrackbar("contrast", "image", &_contrast, 200, update_brightcont);
112
113     update_brightcont(0);
114     cvWaitKey(0);
115
116     cvReleaseImage(&src_image);
117     cvReleaseImage(&dst_image);
118
119     cvReleaseHist(&hist);
120
121     return 0;
122 }
123
124 #ifdef _EiC
125 main(1,"demhist.c");
126 #endif
127