3
3
# import system modules
4
4
import cv2 .cv as cv
5
5
import urllib
6
- import msvcrt
7
6
import numpy as np
8
7
from numpy import linalg as LA
9
8
@@ -46,6 +45,7 @@ def connect(ip,port):
46
45
#print("connected")
47
46
return s
48
47
48
+ #---------------------------------------------------------
49
49
#The following function takes coordinates from the images and convertes them to 3D spatial positions
50
50
#The calibration constants are in R1, T1, R2, and T2 for cameras 1 (west) and 2 (east)
51
51
#These constants are produced by matlab code that is available here:
@@ -125,7 +125,7 @@ def triang_3D(col_1, row_1, col_2, row_2) :
125
125
y_3d = y_coord
126
126
z_3d = z_coord
127
127
128
-
128
+ #---------------------------------------------------------
129
129
def getthresholdedimg (im ):
130
130
131
131
# this function take RGB image.Then convert it into HSV for easy colour detection
@@ -152,40 +152,82 @@ def getthresholdedimg(im):
152
152
cv .Add (imgthreshold , imgblue , imgthreshold )
153
153
154
154
return imgthreshold
155
+ #---------------------------------------------------------
156
+ #img is an image (passed in by reference I'm pretty sure)
157
+ #sideName is for output printing purposes
158
+ #this returns an x and y coordinate of the blimp (x = col, y = row)
159
+ def procImg (img ,sideName ):
160
+
161
+ #creates empty images of the same size
162
+ imdraw = cv .CreateImage (cv .GetSize (img ), 8 , 3 )
163
+
164
+ cv .SetZero (imdraw )
165
+ cv .Smooth (img , img , cv .CV_GAUSSIAN , 3 , 0 )
166
+ imgbluethresh = getthresholdedimg (img )
167
+ cv .Erode (imgbluethresh , imgbluethresh , None , 3 )
168
+ cv .Dilate (imgbluethresh , imgbluethresh , None , 10 )
169
+ img2 = cv .CloneImage (imgbluethresh )
170
+ storage = cv .CreateMemStorage (0 )
171
+ contour = cv .FindContours (imgbluethresh , storage , cv .CV_RETR_CCOMP , cv .CV_CHAIN_APPROX_SIMPLE )
172
+
173
+ centroidx = 0
174
+ centroidy = 0
175
+ prevArea = 0
176
+ pt1 = (0 , 0 )
177
+ pt2 = (0 , 0 )
178
+
179
+ while contour :
180
+ #find the area of each collection of contiguous points (contour)
181
+ bound_rect = cv .BoundingRect (list (contour ))
182
+ contour = contour .h_next ()
155
183
184
+ #get the largest contour
185
+ area = bound_rect [2 ]* bound_rect [3 ];
186
+ if area > prevArea :
187
+ pt1 = (bound_rect [0 ], bound_rect [1 ])
188
+ pt2 = (bound_rect [0 ] + bound_rect [2 ], bound_rect [1 ] + bound_rect [3 ])
156
189
190
+ # Draw bounding rectangle
191
+ cv .Rectangle (img , pt1 , pt2 , cv .CV_RGB (255 ,0 ,0 ), 1 )
192
+
193
+ # calculating centroid
194
+ centroidx = cv .Round ((pt1 [0 ]+ pt2 [0 ])/ 2 )
195
+ centroidy = cv .Round ((pt1 [1 ]+ pt2 [1 ])/ 2 )
196
+
197
+ if (centroidx == 0 or centroidy == 0 ):
198
+ print ("no blimp detected from " + sideName )
199
+ else :
200
+ print (sideName + " centroid x:" + str (centroidx ))
201
+ print (sideName + " centroid y:" + str (centroidy ))
202
+
203
+ print ("" )
204
+
205
+ return (centroidx , centroidy )
206
+
207
+
208
+
209
+ #---------------------------------------------------------
157
210
#!!Need to be on the local WID network to be able to grab images from the cameras
158
211
#grab a frame from the east camera, store it to disk
159
- fname_east = 'C://Users//Jeremy//Documents//blimp //east.jpg'
212
+ fname_east = '. //east.jpg'
160
213
url_east = 'http://10.129.20.11/snapshot/view0.jpg'
161
- urllib .urlretrieve (url_east ,fname_east )
162
214
163
215
#grab a frame from the west camera, store it to disk
164
- fname_west = 'C://Users//Jeremy//Documents//blimp //west.jpg'
216
+ fname_west = '. //west.jpg'
165
217
url_west = 'http://10.129.20.12/snapshot/view0.jpg'
166
- urllib .urlretrieve (url_west ,fname_west )
167
-
168
- frame_west = cv .LoadImageM (fname_west ,cv .CV_LOAD_IMAGE_COLOR );
169
- frame_size = cv .GetSize (frame_west )
170
-
171
- # blank images to which images are added later
172
- test = cv .CreateImage (cv .GetSize (frame_west ),8 ,3 )
173
- img2 = cv .CreateImage (cv .GetSize (frame_west ),8 ,3 )
174
218
175
219
# three windows that will open upon execution
176
220
cv .NamedWindow ("west" ,cv .CV_WINDOW_AUTOSIZE )
177
221
cv .NamedWindow ("east" ,cv .CV_WINDOW_AUTOSIZE )
178
222
179
- # blank lists to store coordinates of blue blob
180
- blue = []
181
-
182
223
#address of the control server
183
224
ip = "md-red5.discovery.wisc.edu"
184
225
port = 7779
185
226
size = 1024
186
227
187
228
#first get a connection to the server
188
- s = connect (ip ,port )
229
+ #s = connect(ip,port)
230
+
189
231
190
232
while (1 ):
191
233
#capture images from cameras, store images to file
@@ -196,153 +238,40 @@ def getthresholdedimg(im):
196
238
frame_west = cv .LoadImageM (fname_west ,cv .CV_LOAD_IMAGE_COLOR );
197
239
frame_east = cv .LoadImageM (fname_east ,cv .CV_LOAD_IMAGE_COLOR );
198
240
199
- #creates empty images of the same size
200
- imdraw_west = cv .CreateImage (cv .GetSize (frame_west ), 8 , 3 )
201
- imdraw_east = cv .CreateImage (cv .GetSize (frame_east ), 8 , 3 )
202
-
203
- #process west image
204
- cv .SetZero (imdraw_west )
205
- cv .Smooth (frame_west , frame_west , cv .CV_GAUSSIAN , 3 , 0 )
206
- imgbluethresh = getthresholdedimg (frame_west )
207
- cv .Erode (imgbluethresh , imgbluethresh , None , 3 )
208
- cv .Dilate (imgbluethresh , imgbluethresh , None , 10 )
209
- img2 = cv .CloneImage (imgbluethresh )
210
- storage = cv .CreateMemStorage (0 )
211
- contour = cv .FindContours (imgbluethresh , storage , cv .CV_RETR_CCOMP , cv .CV_CHAIN_APPROX_SIMPLE )
212
- # blank list into which points for bounding rectangles around blobs are appended
213
- points = []
214
-
215
- centroidx = 0
216
- centroidy = 0
217
-
218
- while contour :
219
-
220
- # Draw bounding rectangles
221
- bound_rect = cv .BoundingRect (list (contour ))
222
- contour = contour .h_next ()
223
- #print contour # not sure why print contour
224
-
225
- # for more details about cv.BoundingRect,see documentation
226
- pt1 = (bound_rect [0 ], bound_rect [1 ])
227
- pt2 = (bound_rect [0 ] + bound_rect [2 ], bound_rect [1 ] + bound_rect [3 ])
228
- points .append (pt1 )
229
- points .append (pt2 )
230
- cv .Rectangle (frame_west , pt1 , pt2 , cv .CV_RGB (255 ,0 ,0 ), 1 )
231
-
232
- # calculating centroids
233
- centroidx = cv .Round ((pt1 [0 ]+ pt2 [0 ])/ 2 )
234
- centroidy = cv .Round ((pt1 [1 ]+ pt2 [1 ])/ 2 )
235
-
236
- # identifying if blue blobs exist and adding centroids to corresponding lists.
237
- # note that the lower and upper bounds correspond to the the lower and upper bounds
238
- # in the getthresholdedimg(im): function earlier in the script.
239
- # e.g., yellow has a lowerbound of 95 and upper bound of 115 in both sections of code
240
- if (55 < cv .Get2D (imghsv ,centroidy ,centroidx )[0 ] < 155 ):
241
- blue .append ((centroidx ,centroidy ))
242
-
243
- # draw colors in windows; exception handling is used to avoid IndexError.
244
- # after drawing is over, centroid from previous part is removed from list by pop.
245
- # so in next frame, centroids in this frame become initial points of line to draw
246
-
247
- # draw blue box around blue blimp blob
248
- try :
249
- cv .Circle (imdraw_west , blue [1 ], 5 , (255 ,0 ,0 ))
250
- cv .Line (imdraw_west , blue [0 ], blue [1 ], (255 ,0 ,0 ), 3 , 8 , 0 )
251
- blue .pop (0 )
252
- print ("west centroid x:" + str (centroidx ))
253
- print ("west centroid y:" + str (centroidy ))
254
- print ("" )
255
- except IndexError :
256
- print "no blimp detected"
257
-
258
-
259
- # adds
260
- cv .Add (test ,imdraw_west ,test )
261
-
262
- centx_west = centroidx
263
- centy_west = centroidy
264
-
265
-
266
- #process east image
267
- cv .SetZero (imdraw_east )
268
- cv .Smooth (frame_east , frame_east , cv .CV_GAUSSIAN , 3 , 0 )
269
- imgbluethresh = getthresholdedimg (frame_east )
270
- cv .Erode (imgbluethresh , imgbluethresh , None , 3 )
271
- cv .Dilate (imgbluethresh , imgbluethresh , None , 10 )
272
- img2 = cv .CloneImage (imgbluethresh )
273
- storage = cv .CreateMemStorage (0 )
274
- contour = cv .FindContours (imgbluethresh , storage , cv .CV_RETR_CCOMP , cv .CV_CHAIN_APPROX_SIMPLE )
275
- # blank list into which points for bounding rectangles around blobs are appended
276
- points = []
277
- while contour :
278
-
279
- # Draw bounding rectangles
280
- bound_rect = cv .BoundingRect (list (contour ))
281
- contour = contour .h_next ()
282
- #print contour # not sure why print contour
283
-
284
- # for more details about cv.BoundingRect,see documentation
285
- pt1 = (bound_rect [0 ], bound_rect [1 ])
286
- pt2 = (bound_rect [0 ] + bound_rect [2 ], bound_rect [1 ] + bound_rect [3 ])
287
- points .append (pt1 )
288
- points .append (pt2 )
289
- cv .Rectangle (frame_east , pt1 , pt2 , cv .CV_RGB (255 ,0 ,0 ), 1 )
290
-
291
- # calculating centroids
292
- centroidx = cv .Round ((pt1 [0 ]+ pt2 [0 ])/ 2 )
293
- centroidy = cv .Round ((pt1 [1 ]+ pt2 [1 ])/ 2 )
294
-
295
- # identifying if blue blobs exist and adding centroids to corresponding lists.
296
- # note that the lower and upper bounds correspond to the the lower and upper bounds
297
- # in the getthresholdedimg(im): function earlier in the script.
298
- # e.g., yellow has a lowerbound of 95 and upper bound of 115 in both sections of code
299
- if (55 < cv .Get2D (imghsv ,centroidy ,centroidx )[0 ] < 155 ):
300
- blue .append ((centroidx ,centroidy ))
301
-
302
- # draw colors in windows; exception handling is used to avoid IndexError.
303
- # after drawing is over, centroid from previous part is removed from list by pop.
304
- # so in next frame, centroids in this frame become initial points of line to draw
305
-
306
- # draw blue box around blue blimp blob
307
- try :
308
- cv .Circle (imdraw_east , blue [1 ], 5 , (255 ,0 ,0 ))
309
- cv .Line (imdraw_east , blue [0 ], blue [1 ], (255 ,0 ,0 ), 3 , 8 , 0 )
310
- blue .pop (0 )
311
- print ("east centroid x:" + str (centroidx ))
312
- print ("east centroid y:" + str (centroidy ))
313
- print ("" )
314
- except IndexError :
315
- print "no blimp detected"
316
-
241
+ #find the blimp with one camera
242
+ centroids = procImg (frame_west ,"west" )
243
+ centx_west = centroids [0 ]
244
+ centy_west = centroids [1 ]
317
245
318
- # adds
319
- cv .Add (test ,imdraw_east ,test )
320
-
321
- centx_east = centroidx
322
- centy_east = centroidy
246
+ #find the blimp with one camera
247
+ centroids = procImg (frame_east ,"east" )
248
+ centx_east = centroids [0 ]
249
+ centy_east = centroids [1 ]
323
250
251
+ #display the images with the blimp outlined
324
252
cv .ShowImage ("west" , frame_west )
325
253
cv .WaitKey (100 )
326
-
327
-
328
254
cv .ShowImage ("east" , frame_east )
329
255
cv .WaitKey (100 )
330
256
257
+ #get the 3D location of the blimp
331
258
triang_3D (centx_west , centy_west , centx_east , centy_east )
332
259
333
260
print ("x_3d: " + str (x_3d [0 ]))
334
261
print ("y_3d: " + str (y_3d [0 ]))
335
262
print ("z_3d: " + str (z_3d [0 ]))
336
-
337
- try :
338
- #x,y,z = getPosition()
339
- msg = "" + str (x_3d [0 ]) + "," + str (y_3d [0 ]) + "," + str (z_3d [0 ]) + "\n "
340
- s .send (msg )
341
- #time.sleep(1)
342
- except Exception as err :
343
- print ("disconnected" )
344
- #we got disconnected somehow, reconnect
345
- s = connect (ip ,port )
263
+ print ("-----------------------------------" )
264
+
265
+ #send the 3D location to the control server
266
+ ## try:
267
+ ## #x,y,z = getPosition()
268
+ ## msg = "" + str(x_3d[0]) + "," + str(y_3d[0]) + "," + str(z_3d[0]) + "\n"
269
+ ## s.send(msg)
270
+ ## #time.sleep(1)
271
+ ## except Exception as err:
272
+ ## print("disconnected")
273
+ ## #we got disconnected somehow, reconnect
274
+ ## s = connect(ip,port)
346
275
347
276
348
277
0 commit comments