-
Notifications
You must be signed in to change notification settings - Fork 0
/
vermell.py
122 lines (97 loc) · 3.66 KB
/
vermell.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#############################################################################
import sys
from opencv import cv
from opencv import highgui
#############################################################################
# definition of some constants
cam_width = 640
cam_height = 480
#############################################################################
r_img = cv.cvCreateImage (cv.cvSize (cam_width,cam_height), 8, 1)
g_img = cv.cvCreateImage (cv.cvSize (cam_width,cam_height), 8, 1)
b_img = cv.cvCreateImage (cv.cvSize (cam_width,cam_height), 8, 1)
laser_img = cv.cvCreateImage (cv.cvSize (cam_width,cam_height), 8, 1)
storage = cv.cvCreateMemStorage(0)
capture = None
def create_and_position_window(name, xpos, ypos):
''' a function to created a named widow (from name),
and place it on the screen at (xpos, ypos) '''
highgui.cvNamedWindow(name, highgui.CV_WINDOW_AUTOSIZE) # create the window
highgui.cvResizeWindow(name, cam_width, cam_height) # resize it
highgui.cvMoveWindow(name, xpos, ypos) # move it to (xpos,ypos) on the screen
def setup_camera_capture(device_num=0):
''' perform camera setup for the device number (default device = 0) i
return a reference to the camera Capture
'''
try:
# try to get the device number from the command line
device = int(device_num)
except (IndexError, ValueError):
# no device number on the command line, assume we want the 1st device
device = 0
print 'Using Camera device %d'%device
global capture
# no argument on the command line, try to use the camera
capture = highgui.cvCreateCameraCapture (device)
# set the wanted image size from the camera
highgui.cvSetCaptureProperty (capture,highgui.CV_CAP_PROP_FRAME_WIDTH, cam_width)
highgui.cvSetCaptureProperty (capture,highgui.CV_CAP_PROP_FRAME_HEIGHT, cam_height)
# check that capture device is OK
if not capture:
print "Error opening capture device"
sys.exit (1)
return capture
def returnEllipses(contours):
ellipses = []
for c in contours.hrange():
count = c.total;
if( count < 6 ):
continue;
PointArray = cv.cvCreateMat(1, count, cv.CV_32SC2)
PointArray2D32f= cv.cvCreateMat( 1, count, cv.CV_32FC2)
cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX));
cv.cvConvert( PointArray, PointArray2D32f )
box = cv.CvBox2D()
box = cv.cvFitEllipse2(PointArray2D32f);
#cv.cvDrawContours(frame, c, cv.CV_RGB(255,255,255), cv.CV_RGB(255,255,255),0,1,8,cv.cvPoint(0,0));
center = cv.CvPoint()
size = cv.CvSize()
center.x = cv.cvRound(box.center.x);
center.y = cv.cvRound(box.center.y);
size.width = cv.cvRound(box.size.width*0.5);
size.height = cv.cvRound(box.size.height*0.5);
box.angle = -box.angle;
ellipses.append({'center':center, 'size':size, 'angle':box.angle})
return ellipses
def initCapture():
setup_camera_capture()
def getData():
frame = highgui.cvQueryFrame(capture)
if frame is None:
return None
cv.cvSplit(frame, b_img, g_img, r_img, None)
cv.cvInRangeS(r_img, 150, 255, r_img)
cv.cvInRangeS(g_img, 0, 100, g_img)
cv.cvInRangeS(b_img, 0, 100, b_img)
cv.cvAnd(r_img, g_img, laser_img)
cv.cvAnd(laser_img, b_img, laser_img)
cv.cvErode(laser_img,laser_img) #,0,2)
cv.cvDilate(laser_img,laser_img)
c_count, contours = cv.cvFindContours (laser_img,
storage,
cv.sizeof_CvContour,
cv.CV_RETR_LIST,
cv.CV_CHAIN_APPROX_NONE,
cv.cvPoint (0,0))
if c_count:
return returnEllipses(contours)
else:
return None
#if c_count:
#ellipses = returnEllipses(contours)
#for e in ellipses:
#cv.cvEllipse(frame, e['center'], e['size'],
#e['angle'], 0, 360,
#cv.CV_RGB(0,0,255), 1, cv.CV_AA, 0);