Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / octave / cam-histo.m
1 #! /usr/bin/env octave
2
3 ## import the necessary things for OpenCV
4 addpath("/home/x/opencv2/interfaces/swig/octave");
5 source("/home/x/opencv2/interfaces/swig/octave/PKG_ADD_template");
6 debug_on_error(true);
7 debug_on_warning(true);
8 crash_dumps_octave_core (0)
9 cv;
10 highgui;
11
12 #############################################################################
13 ## definition of some constants
14
15 ## how many bins we want for the histogram, and their ranges
16 hdims = 16;
17 hranges = {0, 180};
18
19 ## ranges for the limitation of the histogram
20 vmin = 10;
21 vmax = 256;
22 smin = 30;
23
24 ## the range we want to monitor
25 hsv_min = cv.cvScalar (0, smin, vmin, 0);
26 hsv_max = cv.cvScalar (180, 256, vmax, 0);
27
28 #############################################################################
29 ## some useful functions
30
31 function rgb = hsv2rgb (hue)
32   global cv;
33   ## convert the hue value to the corresponding rgb value
34
35   sector_data = [0, 2, 1; 1, 2, 0; 1, 0, 2; 2, 0, 1; 2, 1, 0; 0, 1, 2]+1;
36   hue *= 0.1 / 3;
37   sector = cv.cvFloor (hue);
38   p = cv.cvRound (255 * (hue - sector));
39   if (bitand(sector,1))
40     p = bitxor(p,255);
41   endif
42
43   rgb = zeros(1,3);
44   rgb (sector_data (sector+1, 1)) = 255;
45   rgb (sector_data (sector+1, 2)) = 0;
46   rgb (sector_data (sector+1, 3)) = p;
47   
48   rgb = cv.cvScalar (rgb (3), rgb (2), rgb (1), 0);
49 endfunction
50
51 #############################################################################
52 ## so, here is the main part of the program
53
54 ## a small welcome
55 printf("OpenCV Octave wrapper test\n");
56 printf("OpenCV version: %s (%d, %d, %d)\n",
57        cv.CV_VERSION,cv.CV_MAJOR_VERSION,
58        cv.CV_MINOR_VERSION,cv.CV_SUBMINOR_VERSION);
59
60 ## first, create the necessary windows
61 highgui.cvNamedWindow ('Camera', highgui.CV_WINDOW_AUTOSIZE);
62 highgui.cvNamedWindow ('Histogram', highgui.CV_WINDOW_AUTOSIZE);
63
64 ## move the new window to a better place
65 #highgui.cvMoveWindow ('Camera', 10, 40);
66 #highgui.cvMoveWindow ('Histogram', 10, 270);
67
68 try
69   ## try to get the device number from the command line
70   device = int32 (argv(){1});
71   have_device = true;
72 catch
73   ## no device number on the command line, assume we want the 1st device
74   device = -1;
75 end_try_catch
76
77 ## no argument on the command line, try to use the camera
78 capture = highgui.cvCreateCameraCapture (device);
79
80 ## set the wanted image size from the camera
81 highgui.cvSetCaptureProperty (capture, \
82                               highgui.CV_CAP_PROP_FRAME_WIDTH, 320);
83 highgui.cvSetCaptureProperty (capture, \
84                               highgui.CV_CAP_PROP_FRAME_HEIGHT, 240);
85
86 ## create an image to put in the histogram
87 histimg = cv.cvCreateImage (cv.cvSize (320,240), 8, 3);
88
89 ## init the image of the histogram to black
90 cv.cvSetZero (histimg);
91
92 ## capture the 1st frame to get some propertie on it
93 frame = highgui.cvQueryFrame (capture);
94
95 ## get some properties of the frame
96 frame_size = cv.cvGetSize (frame);
97
98 ## compute which selection of the frame we want to monitor
99 selection = cv.cvRect (0, 0, frame.width, frame.height);
100
101 ## create some images usefull later
102 hue = cv.cvCreateImage (frame_size, 8, 1);
103 mask = cv.cvCreateImage (frame_size, 8, 1);
104 hsv = cv.cvCreateImage (frame_size, 8, 3 );
105
106 ## create the histogram
107 hist = cv.cvCreateHist ({hdims}, cv.CV_HIST_ARRAY, {hranges}, 1);
108
109 while (1)  ## do forever
110   
111   ## 1. capture the current image
112   frame = highgui.cvQueryFrame (capture);
113   if (swig_this(frame)==0);
114     ## no image captured... end the processing
115     break
116   endif
117   
118   ## mirror the captured image
119   cv.cvFlip (frame, [], 1);
120
121   ## compute the hsv version of the image 
122   cv.cvCvtColor (frame, hsv, cv.CV_BGR2HSV);
123
124   ## compute which pixels are in the wanted range
125   cv.cvInRangeS (hsv, hsv_min, hsv_max, mask);
126
127   ## extract the hue from the hsv array
128   cv.cvSplit (hsv, hue, [], [], []);
129
130   ## select the rectangle of interest in the hue/mask arrays
131   hue_roi = cv.cvGetSubRect (hue, selection);
132   mask_roi = cv.cvGetSubRect (mask, selection);
133
134   ## it's time to compute the histogram
135   cv.cvCalcHist (hue_roi, hist, 0, mask_roi);
136
137   ## extract the min and max value of the histogram
138   [min_val, max_val, min_idx, max_idx] = cv.cvGetMinMaxHistValue (hist);
139
140   ## compute the scale factor
141   if (max_val > 0)
142     scale = 255. / max_val;
143   else
144     scale = 0.;
145   endif
146
147   ## scale the histograms
148   cv.cvConvertScale (hist.bins, hist.bins, scale, 0);
149
150   ## clear the histogram image
151   cv.cvSetZero (histimg);
152
153   ## compute the width for each bin do display
154   bin_w = histimg.width / hdims;
155   
156   for  (i=0:hdims-1)
157     ## for all the bins
158
159     ## get the value, and scale to the size of the hist image
160     val = cv.cvRound (cv.cvGetReal1D (hist.bins, i)
161                       * histimg.height / 255);
162
163     ## compute the color
164     color = hsv2rgb (i * 180. / hdims);
165
166     ## draw the rectangle in the wanted color
167     cv.cvRectangle (histimg,
168                     cv.cvPoint (i * bin_w, histimg.height),
169                     cv.cvPoint ((i + 1) * bin_w, histimg.height - val),
170                     color, -1, 8, 0);
171
172     ## we can now display the images
173     highgui.cvShowImage ('Camera', frame);
174     highgui.cvShowImage ('Histogram', histimg);
175   endfor
176
177   ## handle events
178   k = highgui.cvWaitKey (5);
179
180   if (k == 27)
181     ## user has press the ESC key, so exit
182     break;
183   endif
184 endwhile