Skip to content

Commit eb4b960

Browse files
committed
Track multiple yellow objects
1 parent 4e29b5b commit eb4b960

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'''single_color_multiple_points_track.py
2+
3+
Before reading this code make sure you are thorough with track_yellow_draw_line.py and example_8_3.py. It is a combination of both plus use of new function and new ideas. This code shows how you can use contours to track multiple object. A new function cv.BoundingRect also used here to draw bounding rectangles of contours.
4+
5+
In track_yellow_draw_line.py, we tracked a single yellow object. And we didn't use contours there. Here we track multiple yellow objects. ie the name, single_color_multiple_points_track.py. That is the main function.
6+
7+
Plus, as earlier, i have kept drawing function also there, to see a nice output. And i have uploaded a picture scmpt.png to show you what output i got.
8+
9+
Always remember, i am following an hierarchy from simple to complex, and i always put path above. Follow it.
10+
11+
Written by Abid.K --mail me at [email protected] '''
12+
13+
###############################################################################################################################
14+
15+
import cv
16+
posx=0
17+
posy=0
18+
def getthresholdedimg(im):
19+
'''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'''
20+
imghsv=cv.CreateImage(cv.GetSize(im),8,3)
21+
cv.CvtColor(im,imghsv,cv.CV_BGR2HSV) # Convert image from RGB to HSV
22+
imgthreshold=cv.CreateImage(cv.GetSize(im),8,1)
23+
cv.InRangeS(imghsv,cv.Scalar(20,100,100),cv.Scalar(30,255,255),imgthreshold) # Select a range of yellow color
24+
return imgthreshold
25+
26+
27+
capture=cv.CaptureFromCAM(0)
28+
frame = cv.QueryFrame(capture)
29+
frame_size = cv.GetSize(frame)
30+
grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)
31+
test=cv.CreateImage(cv.GetSize(frame),8,3)
32+
cv.NamedWindow("Real")
33+
cv.NamedWindow("Threshold")
34+
while(1):
35+
color_image = cv.QueryFrame(capture)
36+
imdraw=cv.CreateImage(cv.GetSize(frame),8,3)
37+
cv.Flip(color_image,color_image,1)
38+
cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0)
39+
imgyellowthresh=getthresholdedimg(color_image)
40+
cv.Erode(imgyellowthresh,imgyellowthresh,None,3)
41+
cv.Dilate(imgyellowthresh,imgyellowthresh,None,10)
42+
43+
storage = cv.CreateMemStorage(0)
44+
contour = cv.FindContours(imgyellowthresh, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
45+
points = []
46+
47+
# This is the new part here. ie Use of cv.BoundingRect()
48+
while contour:
49+
# Draw bounding rectangles
50+
bound_rect = cv.BoundingRect(list(contour))
51+
contour = contour.h_next()
52+
53+
# for more details about cv.BoundingRect,see documentation
54+
pt1 = (bound_rect[0], bound_rect[1])
55+
pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
56+
points.append(pt1)
57+
points.append(pt2)
58+
cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255,0,0), 1)
59+
lastx=posx
60+
lasty=posy
61+
posx=cv.Round((pt1[0]+pt2[0])/2)
62+
posy=cv.Round((pt1[1]+pt2[1])/2)
63+
if lastx!=0 and lasty!=0:
64+
cv.Line(imdraw,(posx,posy),(lastx,lasty),(0,255,255))
65+
cv.Circle(imdraw,(posx,posy),5,(0,255,255),-1)
66+
cv.Add(test,imdraw,test)
67+
68+
cv.ShowImage("Real",color_image)
69+
cv.ShowImage("Threshold",test)
70+
if cv.WaitKey(33)==1048603:
71+
cv.DestroyWindow("Real")
72+
cv.DestroyWindow("Threshold")
73+
break

0 commit comments

Comments
 (0)