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");
7 debug_on_warning(true);
8 crash_dumps_octave_core (0)
12 #############################################################################
13 ## definition of some constants
15 ## how many bins we want for the histogram, and their ranges
19 ## ranges for the limitation of the histogram
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);
28 #############################################################################
29 ## some useful functions
31 function rgb = hsv2rgb (hue)
33 ## convert the hue value to the corresponding rgb value
35 sector_data = [0, 2, 1; 1, 2, 0; 1, 0, 2; 2, 0, 1; 2, 1, 0; 0, 1, 2]+1;
37 sector = cv.cvFloor (hue);
38 p = cv.cvRound (255 * (hue - sector));
44 rgb (sector_data (sector+1, 1)) = 255;
45 rgb (sector_data (sector+1, 2)) = 0;
46 rgb (sector_data (sector+1, 3)) = p;
48 rgb = cv.cvScalar (rgb (3), rgb (2), rgb (1), 0);
51 #############################################################################
52 ## so, here is the main part of the program
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);
60 ## first, create the necessary windows
61 highgui.cvNamedWindow ('Camera', highgui.CV_WINDOW_AUTOSIZE);
62 highgui.cvNamedWindow ('Histogram', highgui.CV_WINDOW_AUTOSIZE);
64 ## move the new window to a better place
65 #highgui.cvMoveWindow ('Camera', 10, 40);
66 #highgui.cvMoveWindow ('Histogram', 10, 270);
69 ## try to get the device number from the command line
70 device = int32 (argv(){1});
73 ## no device number on the command line, assume we want the 1st device
77 ## no argument on the command line, try to use the camera
78 capture = highgui.cvCreateCameraCapture (device);
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);
86 ## create an image to put in the histogram
87 histimg = cv.cvCreateImage (cv.cvSize (320,240), 8, 3);
89 ## init the image of the histogram to black
90 cv.cvSetZero (histimg);
92 ## capture the 1st frame to get some propertie on it
93 frame = highgui.cvQueryFrame (capture);
95 ## get some properties of the frame
96 frame_size = cv.cvGetSize (frame);
98 ## compute which selection of the frame we want to monitor
99 selection = cv.cvRect (0, 0, frame.width, frame.height);
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 );
106 ## create the histogram
107 hist = cv.cvCreateHist ({hdims}, cv.CV_HIST_ARRAY, {hranges}, 1);
109 while (1) ## do forever
111 ## 1. capture the current image
112 frame = highgui.cvQueryFrame (capture);
113 if (swig_this(frame)==0);
114 ## no image captured... end the processing
118 ## mirror the captured image
119 cv.cvFlip (frame, [], 1);
121 ## compute the hsv version of the image
122 cv.cvCvtColor (frame, hsv, cv.CV_BGR2HSV);
124 ## compute which pixels are in the wanted range
125 cv.cvInRangeS (hsv, hsv_min, hsv_max, mask);
127 ## extract the hue from the hsv array
128 cv.cvSplit (hsv, hue, [], [], []);
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);
134 ## it's time to compute the histogram
135 cv.cvCalcHist (hue_roi, hist, 0, mask_roi);
137 ## extract the min and max value of the histogram
138 [min_val, max_val, min_idx, max_idx] = cv.cvGetMinMaxHistValue (hist);
140 ## compute the scale factor
142 scale = 255. / max_val;
147 ## scale the histograms
148 cv.cvConvertScale (hist.bins, hist.bins, scale, 0);
150 ## clear the histogram image
151 cv.cvSetZero (histimg);
153 ## compute the width for each bin do display
154 bin_w = histimg.width / hdims;
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);
164 color = hsv2rgb (i * 180. / hdims);
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),
172 ## we can now display the images
173 highgui.cvShowImage ('Camera', frame);
174 highgui.cvShowImage ('Histogram', histimg);
178 k = highgui.cvWaitKey (5);
181 ## user has press the ESC key, so exit