/
cam.py
executable file
·91 lines (71 loc) · 2.79 KB
/
cam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#! /usr/bin/env python
import sys
# import the necessary things for OpenCV
from opencv import cv
from opencv import highgui
if __name__ == '__main__':
print "HOWDY, welcome to the webcam proggy"
# first, create the necessary windows
highgui.cvNamedWindow('Camera', highgui.CV_WINDOW_AUTOSIZE)
highgui.cvNamedWindow('HUE', highgui.CV_WINDOW_AUTOSIZE)
highgui.cvNamedWindow('SATURATION', highgui.CV_WINDOW_AUTOSIZE)
highgui.cvNamedWindow('VALUE', highgui.CV_WINDOW_AUTOSIZE)
highgui.cvNamedWindow('RED', highgui.CV_WINDOW_AUTOSIZE)
highgui.cvNamedWindow('GREEN', highgui.CV_WINDOW_AUTOSIZE)
highgui.cvNamedWindow('BLUE', highgui.CV_WINDOW_AUTOSIZE)
# move the new window to a better place
highgui.cvMoveWindow('Camera', 0, 40)
highgui.cvMoveWindow('HUE', 0, 400)
highgui.cvMoveWindow('SATURATION', 330, 40)
highgui.cvMoveWindow('VALUE', 330, 400)
highgui.cvMoveWindow('RED', 660, 40)
highgui.cvMoveWindow('GREEN', 660, 400)
highgui.cvMoveWindow('BLUE', 990, 40)
capture = highgui.cvCreateCameraCapture(0)
# set the wanted image size from the camera
highgui.cvSetCaptureProperty(capture, highgui.CV_CAP_PROP_FRAME_WIDTH, 320)
highgui.cvSetCaptureProperty(capture, highgui.CV_CAP_PROP_FRAME_HEIGHT, 240)
# check that capture device is OK
if not capture:
print "Error opening capture device"
sys.exit (1)
# capture the 1st frame to get some propertie on it
frame = highgui.cvQueryFrame(capture)
# get some properties of the frame
frame_size = cv.cvGetSize(frame)
# compute which selection of the frame we want to monitor
selection = cv.cvRect(0, 0, frame.width, frame.height)
hsv = cv.cvCreateImage(frame_size, 8, 3)
hue = cv.cvCreateImage(frame_size, 8, 1)
saturation = cv.cvCreateImage(frame_size, 8, 1)
val_scale = cv.cvCreateImage(frame_size, 8, 1)
value = cv.cvCreateImage(frame_size, 8, 1)
red = cv.cvCreateImage(frame_size, 8, 1)
green = cv.cvCreateImage(frame_size, 8, 1)
blue = cv.cvCreateImage(frame_size, 8, 1)
while 1:
# 1. capture the current image
frame = highgui.cvQueryFrame (capture)
if frame is None:
# no image captured... end the processing
break
# compute the hsv version of the image
cv.cvCvtColor(frame, hsv, cv.CV_BGR2HSV)
# compute the hue/sat/value from the hsv image
cv.cvSplit(hsv, hue, saturation, value, None)
cv.cvSplit(frame, blue, green, red, None);
cv.cvCvtScale(value, val_scale, 1/100.);
cv.cvCvtScale(val_scale, value, 1);
# handle events
k = highgui.cvWaitKey (10)
# we can now display the images
highgui.cvShowImage('Camera', frame)
highgui.cvShowImage('HUE', hue)
highgui.cvShowImage('SATURATION', saturation)
highgui.cvShowImage('VALUE', value)
highgui.cvShowImage('RED', red)
highgui.cvShowImage('GREEN', green)
highgui.cvShowImage('BLUE', blue)
if k == '\x1b':
# user has press the ESC key, so exit
break