2 from opencv.cv import *
3 from opencv.highgui import *
6 file_name = "../c/baboon.jpg";
20 lut=cvCreateMat(256,1,CV_8U)
22 # brightness/contrast callback function
23 def update_brightness( val ):
24 global Gbrightness # global tag is required, or we get UnboundLocalError
28 def update_contrast( val ):
29 global Gcontrast # global tag is required, or we get UnboundLocalError
33 def update_brightcont():
34 # no global tag required for images ???
36 brightness = Gbrightness - 100;
37 contrast = Gcontrast - 100;
40 # The algorithm is by Werner D. Streidt
41 # (http://visca.com/ffactory/archives/5-99/msg00021.html)
43 delta = 127.*contrast/100;
44 a = 255./(255. - delta*2);
45 b = a*(brightness - delta);
47 delta = -128.*contrast/100;
48 a = (256.-delta*2)/255.;
49 b = a*brightness + delta;
59 cvLUT( src_image, dst_image, lut );
60 cvShowImage( "image", dst_image );
62 cvCalcHist( dst_image, hist, 0, None );
64 min_value, max_value = cvGetMinMaxHistValue( hist );
65 cvScale( hist.bins, hist.bins, float(hist_image.height)/max_value, 0 );
66 #cvNormalizeHist( hist, 1000 );
68 cvSet( hist_image, cvScalarAll(255));
69 bin_w = cvRound(float(hist_image.width)/hist_size);
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 );
76 cvShowImage( "histogram", hist_image );
79 if __name__ == "__main__":
80 # Load the source image. HighGUI use.
82 file_name = sys.argv[1]
84 src_image = cvLoadImage( file_name, 0 );
87 print "Image was not loaded.";
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);
95 cvNamedWindow("image", 0);
96 cvNamedWindow("histogram", 0);
98 cvCreateTrackbar("brightness", "image", _brightness, 200, update_brightness);
99 cvCreateTrackbar("contrast", "image", _contrast, 200, update_contrast);