4bb1047a359f65758cbf5b2bece7549e27d6659b
[opencv] / samples / octave / demhist.m
1 #! /usr/bin/env octave
2 cv;
3 highgui;
4
5 file_name = "../c/baboon.jpg";
6
7 global Gbrightness;
8 global Gcontrast;
9 global hist_size;
10 global ranges;
11 global src_image;
12 global dst_image;
13 global hist_image;
14 global hist;
15 global lut;
16
17 _brightness = 100;
18 _contrast = 100;
19 Gbrightness = 100;
20 Gcontrast = 100;
21
22 hist_size = 64;
23 range_0={0,256};
24 ranges = { range_0 };
25 src_image=[];
26 dst_image=[];
27 hist_image=[];
28 hist=[];
29 lut=cvCreateMat(256,1,CV_8U);
30
31 ## brightness/contrast callback function
32 function update_brightness( val )
33   global Gbrightness    # global tag is required, or we get UnboundLocalError
34   Gbrightness = val;
35   update_brightcont( );
36 endfunction
37
38 function update_contrast( val )
39   global Gcontrast;     # global tag is required, or we get UnboundLocalError
40   Gcontrast = val;
41   update_brightcont( );
42 endfunction
43
44 function update_brightcont()
45   global Gbrightness;
46   global Gcontrast;
47   global hist_size;
48   global ranges;
49   global src_image;
50   global dst_image;
51   global hist_image;
52   global hist;
53   global lut;
54   global cvCalcHist; # use cv namespace for these instead
55   global cvZero;
56   global cvScale;
57
58   brightness = Gbrightness - 100;
59   contrast = Gcontrast - 100;
60   max_value = 0;
61
62   ## The algorithm is by Werner D. Streidt
63   ## (http://visca.com/ffactory/archives/5-99/msg00021.html)
64   if( contrast > 0 )
65     delta = 127.*contrast/100;
66     a = 255./(255. - delta*2);
67     b = a*(brightness - delta);
68   else
69     delta = -128.*contrast/100;
70     a = (256.-delta*2)/255.;
71     b = a*brightness + delta;
72   endif
73
74   for i=0:256-1,
75     v = cvRound(a*i + b);
76     if( v < 0 )
77       v = 0;
78     endif
79     if( v > 255 )
80       v = 255;
81     endif
82     lut(i) = v;
83   endfor
84   
85   cvLUT( src_image, dst_image, lut );
86   cvShowImage( "image", dst_image );
87
88   cvCalcHist( dst_image, hist, 0, [] );
89   cvZero( dst_image );
90   [min_value, max_value] = cvGetMinMaxHistValue( hist );
91   cvScale( hist.bins, hist.bins, double(hist_image.height)/max_value, 0 );
92   ##cvNormalizeHist( hist, 1000 );
93
94   cvSet( hist_image, cvScalarAll(255));
95   bin_w = cvRound(double(hist_image.width)/hist_size);
96
97   for i=0:hist_size-1,
98     cvRectangle( hist_image, cvPoint(i*bin_w, hist_image.height), cvPoint((i+1)*bin_w, hist_image.height - cvRound(cvGetReal1D(hist.bins,i))), cvScalarAll(0), -1, 8, 0 );
99   endfor
100   
101   cvShowImage( "histogram", hist_image );
102 endfunction
103
104
105 ## Load the source image. HighGUI use.
106 if size(argv, 1)>1
107   file_name = argv(){1}
108 endif
109
110 src_image = cvLoadImage( file_name, 0 );
111
112 if (!swig_this(src_image))
113   printf("Image was not loaded.\n");
114   exit(-1);
115 endif
116
117
118 dst_image = cvCloneImage(src_image);
119 hist_image = cvCreateImage(cvSize(320,200), 8, 1);
120 hist = cvCreateHist({hist_size}, CV_HIST_ARRAY, ranges, 1);
121
122 cvNamedWindow("image", 0);
123 cvNamedWindow("histogram", 0);
124
125 cvCreateTrackbar("brightness", "image", _brightness, 200, @update_brightness);
126 cvCreateTrackbar("contrast", "image", _contrast, 200, @update_contrast);
127
128 update_brightcont();
129 cvWaitKey(0);