-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (61 loc) · 2.28 KB
/
main.py
File metadata and controls
66 lines (61 loc) · 2.28 KB
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
from cv2 import cv
import math
from time import sleep
posx=0
posy=0
def getthresholdedimg(im):
'''this function take RGB image.Then convert it into HSV for easy colour detection and threshold it with yellow part as white and all other regions as black.Then return that image'''
imghsv=cv.CreateImage(cv.GetSize(im),8,3)
cv.CvtColor(im,imghsv,cv.CV_BGR2HSV) # Convert image from RGB to HSV
imgthreshold=cv.CreateImage(cv.GetSize(im),8,1)
cv.InRangeS(imghsv,cv.Scalar(20,100,100),cv.Scalar(30,255,255),imgthreshold) # Select a range of yellow color
return imgthreshold
capture=cv.CaptureFromCAM(0)
sleep(5)
frame = cv.QueryFrame(capture)
frame_size = cv.GetSize(frame)
grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)
test=cv.CreateImage(cv.GetSize(frame),8,3)
cv.NamedWindow("Real")
cv.NamedWindow("Threshold")
while(1):
color_image = cv.QueryFrame(capture)
imdraw=cv.CreateImage(cv.GetSize(frame),8,3)
cv.Flip(color_image,color_image,1)
cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0)
imgyellowthresh=getthresholdedimg(color_image)
cv.Erode(imgyellowthresh,imgyellowthresh,None,3)
cv.Dilate(imgyellowthresh,imgyellowthresh,None,10)
storage = cv.CreateMemStorage(0)
contour = cv.FindContours(imgyellowthresh, storage, cv.CV_RETR_EXTERNAL, cv.CV_CHAIN_APPROX_SIMPLE)
points = []
# This is the new part here. ie Use of cv.BoundingRect()
while contour:
# Draw bounding rectangles
bound_rect = cv.BoundingRect(list(contour))
contour=contour.h_next()
'''if contour!=None and contour.h_next()!=None:
contour=contour.h_next()[0]
print contour.h_next()[0]'''
# for more details about cv.BoundingRect,see documentation
pt1 = (bound_rect[0], bound_rect[1])
print pt1
pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
print pt2
points.append(pt1)
points.append(pt2)
cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255,0,0), 1)
lastx=posx
lasty=posy
posx=cv.Round((pt1[0]+pt2[0])/2)
posy=cv.Round((pt1[1]+pt2[1])/2)
if lastx!=0 and lasty!=0:
cv.Line(imdraw,(posx,posy),(lastx,lasty),(0,255,255))
cv.Circle(imdraw,(posx,posy),5,(0,255,255),-1)
cv.Add(test,imdraw,test)
cv.ShowImage("Real",color_image)
cv.ShowImage("Threshold",test)
if cv.WaitKey(33)==1048603:
cv.DestroyWindow("Real")
cv.DestroyWindow("Threshold")
break