Skip to content

Commit 06f811b

Browse files
committed
Latest Python blimp detection script
Tested with blimp movie, outputs blip centroids in command line. Color tracking needs to be improved in different lighting conditions.
1 parent 370d857 commit 06f811b

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

BlimpDetect-8-28.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/usr/bin/python
2+
3+
# import system modules
4+
import cv2.cv as cv
5+
#import cv
6+
global imghsv
7+
8+
# purpose: using HSV thresholds, detects blue, yellow and purple objects in a video stream in three new windows
9+
# 1) a black/white stream showing objects matching threshold values (window "threshold")
10+
# 2) a black/color stream tracking the locations of the objects in their respective colors (window "final")
11+
# 3) a full-color stream showing the original video and the bounding boxes of detected objects (window "real")
12+
13+
# things that would make this script more useful for future tests:
14+
# 1) GUI HSV threshold and minimum pixel size sliders like Kevin has added to the Canny Edge Detection program
15+
# 2) Limit the number of blue/yellow/purple objects that can be detected at one time to one
16+
17+
# source from:
18+
# http://stackoverflow.com/questions/8152504/tracking-two-different-colors-using-opencv-2-3-and-python
19+
20+
# definitely works with Mac OSX, Python 2.7, and OpenCV library
21+
22+
# to modify color thresholds, change the cv.Scalar values in the InRange method in the gettresholdedimg function below
23+
24+
def getthresholdedimg(im):
25+
26+
# this function take RGB image.Then convert it into HSV for easy colour detection
27+
# and threshold it with yellow and blue part as white and all other regions as black.Then return that image
28+
29+
global imghsv
30+
imghsv = cv.CreateImage(cv.GetSize(im),8,3)
31+
32+
# Convert image from RGB to HSV
33+
cv.CvtColor(im,imghsv,cv.CV_BGR2HSV)
34+
35+
# creates images for blue
36+
imgblue = cv.CreateImage(cv.GetSize(im),8,1)
37+
38+
# creates blank image to which color images are added
39+
imgthreshold = cv.CreateImage(cv.GetSize(im),8,1)
40+
41+
# determine HSV color thresholds for yellow, blue, and green
42+
# cv.InRange(src, lowerbound, upperbound, dst)
43+
# for imgblue, lowerbound is 95, and upperbound is 115
44+
cv.InRangeS(imghsv, cv.Scalar(55,100,100), cv.Scalar(155,255,255), imgblue )
45+
46+
# add color thresholds to blank 'threshold' image
47+
cv.Add(imgthreshold, imgblue, imgthreshold)
48+
49+
return imgthreshold
50+
51+
# capture from m4v
52+
capture = cv.CaptureFromFile('/Users/glenaronson/Desktop/blimp_large.m4v') #cv.CaptureFromCAM(0)
53+
frame = cv.QueryFrame(capture)
54+
frame_size = cv.GetSize(frame)
55+
56+
# blank images to which images are added later
57+
test = cv.CreateImage(cv.GetSize(frame),8,3)
58+
img2 = cv.CreateImage(cv.GetSize(frame),8,3)
59+
60+
# three windows that will open upon execution
61+
cv.NamedWindow("Real",0)
62+
63+
# blank lists to store coordinates of blue blob
64+
blue = []
65+
66+
67+
while(1):
68+
# captures feed from video in color
69+
color_image = cv.QueryFrame(capture)
70+
71+
# ??
72+
imdraw = cv.CreateImage(cv.GetSize(frame), 8, 3)
73+
74+
# ??
75+
cv.SetZero(imdraw)
76+
cv.Flip(color_image,color_image, 1)
77+
cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0)
78+
# ??
79+
imgbluethresh = getthresholdedimg(color_image)
80+
cv.Erode(imgbluethresh, imgbluethresh, None, 3)
81+
cv.Dilate(imgbluethresh, imgbluethresh, None, 10)
82+
# ??
83+
img2 = cv.CloneImage(imgbluethresh)
84+
# ??
85+
storage = cv.CreateMemStorage(0)
86+
contour = cv.FindContours(imgbluethresh, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
87+
88+
# blank list into which points for bounding rectangles around blobs are appended
89+
points = []
90+
91+
# this is the new part here. ie use of cv.BoundingRect()
92+
while contour:
93+
94+
# Draw bounding rectangles
95+
bound_rect = cv.BoundingRect(list(contour))
96+
contour = contour.h_next()
97+
#print contour # not sure why print contour
98+
99+
# for more details about cv.BoundingRect,see documentation
100+
pt1 = (bound_rect[0], bound_rect[1])
101+
pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
102+
points.append(pt1)
103+
points.append(pt2)
104+
cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255,0,0), 1)
105+
106+
# calculating centroids
107+
centroidx = cv.Round((pt1[0]+pt2[0])/2)
108+
centroidy = cv.Round((pt1[1]+pt2[1])/2)
109+
110+
# identifying if blue blobs exist and adding centroids to corresponding lists.
111+
# note that the lower and upper bounds correspond to the the lower and upper bounds
112+
# in the getthresholdedimg(im): function earlier in the script.
113+
# e.g., yellow has a lowerbound of 95 and upper bound of 115 in both sections of code
114+
if (55 < cv.Get2D(imghsv,centroidy,centroidx)[0] < 155):
115+
blue.append((centroidx,centroidy))
116+
117+
# draw colors in windows; exception handling is used to avoid IndexError.
118+
# after drawing is over, centroid from previous part is removed from list by pop.
119+
# so in next frame, centroids in this frame become initial points of line to draw
120+
121+
# draw blue box around blue blimp blob
122+
try:
123+
cv.Circle(imdraw, blue[1], 5, (255,0,0))
124+
cv.Line(imdraw, blue[0], blue[1], (255,0,0), 3, 8, 0)
125+
blue.pop(0)
126+
print("centroid x:" + str(centroidx))
127+
print("centroid y:" + str(centroidy))
128+
print("")
129+
except IndexError:
130+
print "no blimp detected"
131+
132+
133+
# adds
134+
cv.Add(test,imdraw,test)
135+
136+
# display windows previously created
137+
cv.ShowImage("Real", color_image)
138+
139+
if cv.WaitKey(33) == 1048603:
140+
cv.DestroyWindow("Real")
141+
break
142+
######################################################

0 commit comments

Comments
 (0)