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