5 # import the necessary things for OpenCV
7 from opencv import highgui
9 #############################################################################
10 # definition of some constants
12 # how many bins we want for the histogram, and their ranges
16 # ranges for the limitation of the histogram
21 # the range we want to monitor
22 hsv_min = cv.cvScalar (0, smin, vmin, 0)
23 hsv_max = cv.cvScalar (180, 256, vmax, 0)
25 #############################################################################
26 # some useful functions
29 # convert the hue value to the corresponding rgb value
31 sector_data = [[0, 2, 1],
38 sector = cv.cvFloor (hue)
39 p = cv.cvRound (255 * (hue - sector))
44 rgb [sector_data [sector][0]] = 255
45 rgb [sector_data [sector][1]] = 0
46 rgb [sector_data [sector][2]] = p
48 return cv.cvScalar (rgb [2], rgb [1], rgb [0], 0)
50 #############################################################################
51 # so, here is the main part of the program
53 if __name__ == '__main__':
56 print "OpenCV Python wrapper test"
57 print "OpenCV version: %s (%d, %d, %d)" % (cv.CV_VERSION,
60 cv.CV_SUBMINOR_VERSION)
62 # first, create the necessary windows
63 highgui.cvNamedWindow ('Camera', highgui.CV_WINDOW_AUTOSIZE)
64 highgui.cvNamedWindow ('Histogram', highgui.CV_WINDOW_AUTOSIZE)
66 # move the new window to a better place
67 highgui.cvMoveWindow ('Camera', 10, 40)
68 highgui.cvMoveWindow ('Histogram', 10, 270)
71 # try to get the device number from the command line
72 device = int (sys.argv [1])
74 # got it ! so remove it from the arguments
76 except (IndexError, ValueError):
77 # no device number on the command line, assume we want the 1st device
80 if len (sys.argv) == 1:
81 # no argument on the command line, try to use the camera
82 capture = highgui.cvCreateCameraCapture (device)
84 # set the wanted image size from the camera
85 highgui.cvSetCaptureProperty (capture,
86 highgui.CV_CAP_PROP_FRAME_WIDTH, 320)
87 highgui.cvSetCaptureProperty (capture,
88 highgui.CV_CAP_PROP_FRAME_HEIGHT, 240)
90 # we have an argument on the command line,
91 # we can assume this is a file name, so open it
92 capture = highgui.cvCreateFileCapture (sys.argv [1])
94 # check that capture device is OK
96 print "Error opening capture device"
99 # create an image to put in the histogram
100 histimg = cv.cvCreateImage (cv.cvSize (320,240), 8, 3)
102 # init the image of the histogram to black
103 cv.cvSetZero (histimg)
105 # capture the 1st frame to get some propertie on it
106 frame = highgui.cvQueryFrame (capture)
108 # get some properties of the frame
109 frame_size = cv.cvGetSize (frame)
111 # compute which selection of the frame we want to monitor
112 selection = cv.cvRect (0, 0, frame.width, frame.height)
114 # create some images usefull later
115 hue = cv.cvCreateImage (frame_size, 8, 1)
116 mask = cv.cvCreateImage (frame_size, 8, 1)
117 hsv = cv.cvCreateImage (frame_size, 8, 3 )
119 # create the histogram
120 hist = cv.cvCreateHist ([hdims], cv.CV_HIST_ARRAY, hranges, 1)
125 # 1. capture the current image
126 frame = highgui.cvQueryFrame (capture)
128 # no image captured... end the processing
131 # mirror the captured image
132 cv.cvFlip (frame, None, 1)
134 # compute the hsv version of the image
135 cv.cvCvtColor (frame, hsv, cv.CV_BGR2HSV)
137 # compute which pixels are in the wanted range
138 cv.cvInRangeS (hsv, hsv_min, hsv_max, mask)
140 # extract the hue from the hsv array
141 cv.cvSplit (hsv, hue, None, None, None)
143 # select the rectangle of interest in the hue/mask arrays
144 hue_roi = cv.cvGetSubRect (hue, selection)
145 mask_roi = cv.cvGetSubRect (mask, selection)
147 # it's time to compute the histogram
148 cv.cvCalcHist (hue_roi, hist, 0, mask_roi)
150 # extract the min and max value of the histogram
151 min_val, max_val, min_idx, max_idx = cv.cvGetMinMaxHistValue (hist)
153 # compute the scale factor
155 scale = 255. / max_val
159 # scale the histograms
160 cv.cvConvertScale (hist.bins, hist.bins, scale, 0)
162 # clear the histogram image
163 cv.cvSetZero (histimg)
165 # compute the width for each bin do display
166 bin_w = histimg.width / hdims
168 for i in range (hdims):
171 # get the value, and scale to the size of the hist image
172 val = cv.cvRound (cv.cvGetReal1D (hist.bins, i)
173 * histimg.height / 255)
176 color = hsv2rgb (i * 180. / hdims)
178 # draw the rectangle in the wanted color
179 cv.cvRectangle (histimg,
180 cv.cvPoint (i * bin_w, histimg.height),
181 cv.cvPoint ((i + 1) * bin_w, histimg.height - val),
184 # we can now display the images
185 highgui.cvShowImage ('Camera', frame)
186 highgui.cvShowImage ('Histogram', histimg)
189 k = highgui.cvWaitKey (10)
192 # user has press the ESC key, so exit