Skip to content

Commit 43af4a2

Browse files
committed
Added minimum size threshold for blimp objects
Included 20 pixel x and y minimum for blimp like objects so only those meeting these conditions will be 1) appended to centroid coordinate lists which are used to calculate 3D position, and 2) surrounding by the red bounding rectangle in the video being analyzed.
1 parent 93093c2 commit 43af4a2

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

BlimpDetect-9-22.py

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

0 commit comments

Comments
 (0)