Skip to content

Commit aef9c53

Browse files
committed
Update to the segmentation algorithm for new blimp
1 parent de1a54c commit aef9c53

File tree

1 file changed

+62
-46
lines changed

1 file changed

+62
-46
lines changed

BlimpDetect.py

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ def triang_3D(col_1, row_1, col_2, row_2) :
7373
row_1 = (row_1-360)*0.00475
7474
row_2 = (row_2-360)*0.00475
7575

76+
print("west side centroid x: " + str(col_1) + " y: " + str(row_1) + " mm")
77+
print("east side centroid x: " + str(col_2) + " y: " + str(row_2) + " mm")
78+
79+
#col_1 = 0.8117
80+
#row_1 = -0.9851
81+
#col_2 = -1.0373
82+
#row_2 = 0.42232
83+
7684

7785
#translated from matlab:
7886

@@ -150,31 +158,34 @@ def triang_3D(col_1, row_1, col_2, row_2) :
150158
#---------------------------------------------------------
151159
def getthresholdedimg(im):
152160

153-
# this function take RGB image.Then convert it into HSV for easy colour detection
154-
# and threshold it with yellow and blue part as white and all other regions as black.Then return that image
155-
156-
global imghsv
157-
imghsv = cv.CreateImage(cv.GetSize(im),8,3)
158-
159-
# Convert image from RGB to HSV
160-
cv.CvtColor(im,imghsv,cv.CV_BGR2HSV)
161-
162-
# creates images for blue
163-
imgblue = cv.CreateImage(cv.GetSize(im),8,1)
164-
165-
# creates blank image to which color images are added
166-
imgthreshold = cv.CreateImage(cv.GetSize(im),8,1)
167-
168-
# determine HSV color thresholds for yellow, blue, and green
169-
# cv.InRange(src, lowerbound, upperbound, dst)
170-
# for imgblue, lowerbound is 95, and upperbound is 115
171-
cv.InRangeS(imghsv, cv.Scalar(55,110,110), cv.Scalar(155,255,255), imgblue )
172-
173-
# add color thresholds to blank 'threshold' image
174-
cv.Add(imgthreshold, imgblue, imgthreshold)
175-
176-
#return imgthreshold
177-
return imgblue
161+
# this function take RGB image.Then converts it into HSV for easy colour detection
162+
# Threshold it with yellow and blue part as white and all other regions as black.Then return that image
163+
RED_MIN = cv.Scalar(0,140,140)
164+
RED_MAX = cv.Scalar(5,255,255)
165+
166+
global imghsv
167+
imghsv = cv.CreateImage(cv.GetSize(im),8,3)
168+
169+
# Convert image from RGB to HSV
170+
cv.CvtColor(im,imghsv,cv.CV_BGR2HSV)
171+
172+
# creates images for blue
173+
imgblue = cv.CreateImage(cv.GetSize(im),8,1)
174+
175+
# creates blank image to which color images are added
176+
imgthreshold = cv.CreateImage(cv.GetSize(im),8,1)
177+
178+
# determine HSV color thresholds for yellow, blue, and green
179+
# cv.InRange(src, lowerbound, upperbound, dst)
180+
# for imgblue, lowerbound is 95, and upperbound is 115
181+
# for red, 0 to 60
182+
cv.InRangeS(imghsv, RED_MIN, RED_MAX, imgblue)
183+
184+
# add color thresholds to blank 'threshold' image
185+
cv.Add(imgthreshold, imgblue, imgthreshold)
186+
187+
#return imgthreshold
188+
return imgblue
178189
#---------------------------------------------------------
179190
#img is an image (passed in by reference)
180191
#sideName is for output printing purposes
@@ -189,11 +200,14 @@ def procImg(img,sideName,dispFlag):
189200
imgMask = cv.CreateImage(cv.GetSize(img), 8, 1)
190201

191202
cv.SetZero(imdraw)
192-
cv.Smooth(img, imgSmooth, cv.CV_GAUSSIAN, 3, 0) #Gaussian filter the image
193-
imgbluethresh = getthresholdedimg(img) #Get a color thresholed binary image
194-
imgMask = imgbluethresh
203+
cv.Smooth(img, imgSmooth, cv.CV_GAUSSIAN, 5, 0) #Gaussian filter the image
204+
imgbluethresh = getthresholdedimg(imgSmooth) #Get a color thresholed binary image
205+
cv.Smooth(imgbluethresh, imgbluethresh, cv.CV_GAUSSIAN, 5, 0) #Gaussian filter the image
206+
#imgMask = imgbluethresh
207+
#imgbluethresh = imgMask
195208
cv.Erode(imgbluethresh, imgbluethresh, None, 3)
196209
cv.Dilate(imgbluethresh, imgbluethresh, None, 10)
210+
imgMask = imgbluethresh
197211
#img2 = cv.CloneImage(imgbluethresh)
198212
storage = cv.CreateMemStorage(0)
199213
contour = cv.FindContours(imgbluethresh, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
@@ -213,11 +227,12 @@ def procImg(img,sideName,dispFlag):
213227
area = bound_rect[2]*bound_rect[3];
214228

215229
#if dispFlag:
216-
# print("Area= " + str(area))
230+
#print("Area= " + str(area))
217231

218-
if (area > prevArea and area > 3000):
232+
if (area > 5000) and (area > prevArea):
219233
pt1 = (bound_rect[0], bound_rect[1])
220-
pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
234+
pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
235+
prevArea = area
221236

222237
# Draw bounding rectangle
223238
cv.Rectangle(img, pt1, pt2, cv.CV_RGB(255,0,0), 3)
@@ -228,9 +243,7 @@ def procImg(img,sideName,dispFlag):
228243

229244
if (centroidx == 0 or centroidy == 0):
230245
print ("no blimp detected from " + sideName)
231-
else:
232-
print(sideName + " centroid x:" + str(centroidx))
233-
print(sideName + " centroid y:" + str(centroidy))
246+
234247

235248
print("")
236249

@@ -282,15 +295,17 @@ def procImg(img,sideName,dispFlag):
282295
#Live images from the IP cameras
283296
#(if not live, then this file needs to be in folder with a bunch of images)
284297
#(if live, then need to be local at WID)
285-
liveIP = 1
298+
liveIP = 0
286299
if liveIP == 0:
287300
#need to be in directory with a bunch of images
288-
dirList = os.listdir(os.getcwd())
301+
#dirList = os.listdir(os.getcwd())
302+
dirName = 'E:\\Google Drive\\Medical_Physics\\Blimp\\2013-09-19\\15,10,7.32\\'
303+
dirList = os.listdir(dirName)
289304
num_base = 0
290-
both_offset = 10
291-
num_imgs = 145 - both_offset
305+
both_offset = 0
306+
num_imgs = 5 - both_offset
292307
east_offset = 0 + both_offset
293-
west_offset = 147 + both_offset
308+
west_offset = num_imgs + both_offset
294309

295310

296311
#address of the control server
@@ -310,12 +325,13 @@ def procImg(img,sideName,dispFlag):
310325
#capture images from cameras, store images to file
311326
urllib.urlretrieve(url_west,fname_west)
312327
urllib.urlretrieve(url_east,fname_east)
313-
else:
314-
num_base = (num_base + 1)%(num_imgs)
328+
else:
329+
print("image # = " + str(num_base))
315330
west_num = west_offset + num_base
316331
east_num = east_offset + num_base
317-
fname_west = dirList[west_num]
318-
fname_east = dirList[east_num]
332+
fname_west = dirName + dirList[west_num]
333+
fname_east = dirName + dirList[east_num]
334+
num_base = (num_base + 1)%(num_imgs)
319335
cv.WaitKey(2000) #wait for 2 seconds so I can see the output
320336

321337
#open the images from file
@@ -349,9 +365,9 @@ def procImg(img,sideName,dispFlag):
349365
#get the 3D location of the blimp
350366
coord3D = triang_3D(centx_west, centy_west, centx_east, centy_east)
351367

352-
print("x_3d: " + str(coord3D[0]))
353-
print("y_3d: " + str(coord3D[1]))
354-
print("z_3d: " + str(coord3D[2]))
368+
print("x_3d: " + str(coord3D[0]) + "mm")
369+
print("y_3d: " + str(coord3D[1]) + "mm")
370+
print("z_3d: " + str(coord3D[2]) + "mm")
355371
print("err1: " + str(coord3D[3]))
356372
print("err2: " + str(coord3D[4]))
357373

0 commit comments

Comments
 (0)