Skip to content

Commit d6b667f

Browse files
committed
Segmentation improvements
Tinkered with segmentation code a fair bit
1 parent 4f7d6fb commit d6b667f

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

BlimpDetect.py

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def triang_3D(col_1, row_1, col_2, row_2) :
6262
#P2 = np.array([[-49.3179, -518.1547, -4126.6037, 847220.0489], [-1776.8193, 738.4249, -127.1965, 963513.3797], [0.2075, 0.9387, -0.2753, 1589.9759]])
6363
#P2 = np.array([[2.1, 0.0, -3.64, -34048.4],[0.0, -4.2, 0.0, 18186.0],[-0.866, 0.0, -0.5, 44521.3]])
6464
#P2 = np.array([[2.100000, 1.651303, -3.240864, -46414.721975],[0.000000, -3.742227, -1.906760, 43391.754855],[-0.866025, 0.226995, -0.445503, 42821.420363]])
65-
P1 = np.array([[-1.173428,-0.688810,-3.973488,47750.246983],[-0.169082,4.143045,-0.668271,-12800.579085],{0.959334,-0.006367,-0.282201,3183.078361]])
66-
P2 = np.array([[2.655326,1.495096,-2.890317,-51294.190875],[0.815836,-3.917179,-1.276762,34865.251439],{-0.750044,0.058514,-0.658795,33903.280179]])
65+
P1 = np.array([[-1.173428,-0.688810,-3.973488,47750.246983],[-0.169082,4.143045,-0.668271,-12800.579085],[0.959334,-0.006367,-0.282201,3183.078361]])
66+
P2 = np.array([[2.655326,1.495096,-2.890317,-51294.190875],[0.815836,-3.917179,-1.276762,34865.251439],[-0.750044,0.058514,-0.658795,33903.280179]])
6767

6868
#blimp position from camera 1
6969
#col_1 = 396
@@ -164,32 +164,44 @@ def getthresholdedimg(im):
164164

165165
# this function take RGB image.Then converts it into HSV for easy colour detection
166166
# Threshold it with yellow and blue part as white and all other regions as black.Then return that image
167-
RED_MIN = cv.Scalar(0,140,140)
168-
RED_MAX = cv.Scalar(5,255,255)
167+
RED_MIN1 = cv.Scalar(0,100,150)
168+
RED_MAX1 = cv.Scalar(2,255,255)
169+
RED_MIN2 = cv.Scalar(170,100,150)
170+
RED_MAX2 = cv.Scalar(180,255,255)
171+
YEL_MIN = cv.Scalar(22,90,90)
172+
YEL_MAX = cv.Scalar(25,255,255)
173+
169174

170175
global imghsv
171176
imghsv = cv.CreateImage(cv.GetSize(im),8,3)
172177

173178
# Convert image from RGB to HSV
174179
cv.CvtColor(im,imghsv,cv.CV_BGR2HSV)
175180

176-
# creates images for blue
177-
imgblue = cv.CreateImage(cv.GetSize(im),8,1)
181+
# creates images for red
182+
imgred1 = cv.CreateImage(cv.GetSize(im),8,1)
183+
imgred2 = cv.CreateImage(cv.GetSize(im),8,1)
184+
185+
# image for yellow
186+
imgyellow = cv.CreateImage(cv.GetSize(im),8,1)
178187

179188
# creates blank image to which color images are added
180189
imgthreshold = cv.CreateImage(cv.GetSize(im),8,1)
181190

182191
# determine HSV color thresholds for yellow, blue, and green
183192
# cv.InRange(src, lowerbound, upperbound, dst)
184-
# for imgblue, lowerbound is 95, and upperbound is 115
185-
# for red, 0 to 60
186-
cv.InRangeS(imghsv, RED_MIN, RED_MAX, imgblue)
193+
cv.InRangeS(imghsv, RED_MIN1, RED_MAX1, imgred1)
194+
cv.InRangeS(imghsv, RED_MIN2, RED_MAX2, imgred2)
195+
cv.InRangeS(imghsv, YEL_MIN, YEL_MAX, imgyellow)
196+
187197

188198
# add color thresholds to blank 'threshold' image
189-
cv.Add(imgthreshold, imgblue, imgthreshold)
199+
cv.Add(imgthreshold, imgred1, imgthreshold)
200+
cv.Add(imgthreshold, imgred2, imgthreshold)
201+
cv.Add(imgthreshold, imgyellow, imgthreshold)
190202

191203
#return imgthreshold
192-
return imgblue
204+
return imgthreshold
193205
#---------------------------------------------------------
194206
#img is an image (passed in by reference)
195207
#sideName is for output printing purposes
@@ -201,20 +213,20 @@ def procImg(img,sideName,dispFlag):
201213
#put the smoothed image here
202214
imgSmooth = cv.CreateImage(cv.GetSize(img), 8, 3)
203215
#put thresholded image here
204-
imgMask = cv.CreateImage(cv.GetSize(img), 8, 1)
216+
imgMask = cv.CreateImage(cv.GetSize(img), 1, 1)
205217

206218
cv.SetZero(imdraw)
207-
cv.Smooth(img, imgSmooth, cv.CV_GAUSSIAN, 5, 0) #Gaussian filter the image
208-
imgbluethresh = getthresholdedimg(imgSmooth) #Get a color thresholed binary image
209-
cv.Smooth(imgbluethresh, imgbluethresh, cv.CV_GAUSSIAN, 5, 0) #Gaussian filter the image
210-
#imgMask = imgbluethresh
219+
#cv.Smooth(img, imgSmooth, cv.CV_GAUSSIAN, 13, 0) #Gaussian filter the image
220+
imgthresh = getthresholdedimg(img) #Get a color thresholed binary image
221+
#cv.Smooth(imgbluethresh, imgbluethresh, cv.CV_GAUSSIAN, 5, 0) #Gaussian filter the image
222+
imgMask = imgthresh
211223
#imgbluethresh = imgMask
212-
cv.Erode(imgbluethresh, imgbluethresh, None, 3)
213-
cv.Dilate(imgbluethresh, imgbluethresh, None, 10)
214-
imgMask = imgbluethresh
224+
cv.Erode(imgMask, imgMask, None, 6)
225+
cv.Dilate(imgMask, imgMask, None, 10)
226+
215227
#img2 = cv.CloneImage(imgbluethresh)
216228
storage = cv.CreateMemStorage(0)
217-
contour = cv.FindContours(imgbluethresh, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
229+
contour = cv.FindContours(imgMask, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
218230

219231
centroidx = 0
220232
centroidy = 0
@@ -231,13 +243,13 @@ def procImg(img,sideName,dispFlag):
231243
area = bound_rect[2]*bound_rect[3];
232244

233245
#if dispFlag:
234-
#print("Area= " + str(area))
246+
print("Area= " + str(area))
235247

236248
#Largest area over 5000 pixels
237-
if (area > 5000) and (area > prevArea):
249+
if (area > 2000) and (area > prevArea):
238250
pt1 = (bound_rect[0], bound_rect[1])
239251
pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
240-
prevArea = area
252+
prevArea = area
241253

242254
# Draw bounding rectangle
243255
cv.Rectangle(img, pt1, pt2, cv.CV_RGB(255,0,0), 3)
@@ -259,7 +271,7 @@ def procImg(img,sideName,dispFlag):
259271
print("")
260272

261273
if dispFlag:
262-
small_thresh = cv.CreateImage((int(dispScale1*cv.GetSize(imgbluethresh)[0]), int(dispScale1*cv.GetSize(imgbluethresh)[1])), 8, 1)
274+
small_thresh = cv.CreateImage((int(dispScale1*cv.GetSize(imgMask)[0]), int(dispScale1*cv.GetSize(imgMask)[1])), 8, 1)
263275
cv.Resize(imgMask, small_thresh)
264276
cv.Threshold(small_thresh, small_thresh, 0, 255, cv.CV_THRESH_BINARY)
265277
cv.ShowImage(sideName + "_threshold", small_thresh)
@@ -369,7 +381,7 @@ def procImg(img,sideName,dispFlag):
369381
fname_west = westList[imgIdx]
370382
fname_east = eastList[imgIdx]
371383
#imgIdx = (imgIdx + 1)%(totImg) -> Update below
372-
#cv.WaitKey(2000) #wait for 2 seconds so I can see the output
384+
cv.WaitKey(3000) #wait for 2 seconds so I can see the output
373385

374386
#open the images from file
375387
frame_west = cv.LoadImage(fname_west,cv.CV_LOAD_IMAGE_COLOR);

0 commit comments

Comments
 (0)