Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / samples / python / demhist.py
1 #!/usr/bin/python
2 from opencv.cv import *
3 from opencv.highgui import *
4 import sys
5
6 file_name = "../c/baboon.jpg";
7
8 _brightness = 100
9 _contrast = 100
10 Gbrightness = 100
11 Gcontrast = 100
12
13 hist_size = 64
14 range_0=[0,256]
15 ranges = [ range_0 ]
16 src_image=None
17 dst_image=None
18 hist_image=None
19 hist=None
20 lut=cvCreateMat(256,1,CV_8U)
21
22 # brightness/contrast callback function
23 def update_brightness( val ):
24     global Gbrightness    # global tag is required, or we get UnboundLocalError
25     Gbrightness = val
26     update_brightcont( )
27
28 def update_contrast( val ):
29     global Gcontrast     # global tag is required, or we get UnboundLocalError
30     Gcontrast = val
31     update_brightcont( )
32
33 def update_brightcont():
34     # no global tag required for images ???
35
36     brightness = Gbrightness - 100;
37     contrast = Gcontrast - 100;
38     max_value = 0;
39
40      # The algorithm is by Werner D. Streidt
41      # (http://visca.com/ffactory/archives/5-99/msg00021.html)
42     if( contrast > 0 ):
43         delta = 127.*contrast/100;
44         a = 255./(255. - delta*2);
45         b = a*(brightness - delta);
46     else:
47         delta = -128.*contrast/100;
48         a = (256.-delta*2)/255.;
49         b = a*brightness + delta;
50
51     for i in range(256):
52         v = cvRound(a*i + b);
53         if( v < 0 ):
54             v = 0;
55         if( v > 255 ):
56             v = 255;
57         lut[i] = v;
58     
59     cvLUT( src_image, dst_image, lut );
60     cvShowImage( "image", dst_image );
61
62     cvCalcHist( dst_image, hist, 0, None );
63     cvZero( dst_image );
64     min_value, max_value = cvGetMinMaxHistValue( hist );
65     cvScale( hist.bins, hist.bins, float(hist_image.height)/max_value, 0 );
66     #cvNormalizeHist( hist, 1000 );
67
68     cvSet( hist_image, cvScalarAll(255));
69     bin_w = cvRound(float(hist_image.width)/hist_size);
70
71     for i in range(hist_size):
72         cvRectangle( hist_image, cvPoint(i*bin_w, hist_image.height),
73                      cvPoint((i+1)*bin_w, hist_image.height - cvRound(cvGetReal1D(hist.bins,i))),
74                      cvScalarAll(0), -1, 8, 0 );
75    
76     cvShowImage( "histogram", hist_image );
77
78
79 if __name__ == "__main__":
80     # Load the source image. HighGUI use.
81     if len(sys.argv)>1:
82         file_name = sys.argv[1]
83
84     src_image = cvLoadImage( file_name, 0 );
85
86     if not src_image:
87         print "Image was not loaded.";
88         sys.exit(-1)
89
90
91     dst_image = cvCloneImage(src_image);
92     hist_image = cvCreateImage(cvSize(320,200), 8, 1);
93     hist = cvCreateHist([hist_size], CV_HIST_ARRAY, ranges, 1);
94
95     cvNamedWindow("image", 0);
96     cvNamedWindow("histogram", 0);
97
98     cvCreateTrackbar("brightness", "image", _brightness, 200, update_brightness);
99     cvCreateTrackbar("contrast", "image", _contrast, 200, update_contrast);
100
101     update_brightcont();
102     cvWaitKey(0);