@@ -55,10 +55,12 @@ def triang_3D(col_1, row_1, col_2, row_2) :
55
55
56
56
#Corrected camera matrix for west side
57
57
#P1 = np.array([[408.4918, -1607.7562, 3814.1879, 490234.8756], [-1793.2995, -707.4668, -45.8775, 646489.5760], [0.1810, -0.9505, -0.2524, 1285.5524]])
58
- P1 = np .array ([[- 2.1 , 0.0 , - 3.64 , 55432.5 ], [0.0 , 4.2 , 0.0 , - 18186.0 ], [0.866 , 0.0 , - 0.5 , 7620.0 ]])
58
+ #P1 = np.array([[-2.1, 0.0, -3.64, 55432.5], [0.0, 4.2, 0.0, -18186.0], [0.866, 0.0, -0.5, 7620.0]])
59
+ P1 = np .array ([[- 1.297871 , 0.000000 , - 3.994437 , 60875.225495 ],[0.000000 , 4.200000 , 0.000000 , - 16086.000000 ],[0.951057 , 0.000000 , - 0.309017 , 4709.418994 ]])
59
60
#Corrected camera matrix for east side
60
61
#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]])
61
- 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 ]])
62
+ #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]])
63
+ 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 ]])
62
64
#blimp position from camera 1
63
65
#col_1 = 396
64
66
#row_1 = 424
@@ -67,14 +69,12 @@ def triang_3D(col_1, row_1, col_2, row_2) :
67
69
#col_2 = 518
68
70
#row_2 = 538
69
71
70
- #Convert pixel numbers to mm, 0, 0 is the center of the image,
71
- col_1 = (col_1 - 640 )* 0.0035
72
- col_2 = (col_2 - 640 )* 0.0035
73
- row_1 = (row_1 - 360 )* 0.00475
74
- row_2 = (row_2 - 360 )* 0.00475
75
-
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" )
72
+ #Convert pixel numbers to mm, 0, 0 is the center of the image,
73
+ # negative value fixes image origin position differences between matlab and opencv
74
+ #col_1 = (col_1-640)*-0.0035
75
+ #col_2 = (col_2-640)*-0.0035
76
+ #row_1 = (row_1-360)*-0.00475
77
+ #row_2 = (row_2-360)*-0.00475 -> Doing this below
78
78
79
79
#col_1 = 0.8117
80
80
#row_1 = -0.9851
@@ -228,7 +228,8 @@ def procImg(img,sideName,dispFlag):
228
228
229
229
#if dispFlag:
230
230
#print("Area= " + str(area))
231
-
231
+
232
+ #Largest area over 5000 pixels
232
233
if (area > 5000 ) and (area > prevArea ):
233
234
pt1 = (bound_rect [0 ], bound_rect [1 ])
234
235
pt2 = (bound_rect [0 ] + bound_rect [2 ], bound_rect [1 ] + bound_rect [3 ])
@@ -238,11 +239,17 @@ def procImg(img,sideName,dispFlag):
238
239
cv .Rectangle (img , pt1 , pt2 , cv .CV_RGB (255 ,0 ,0 ), 3 )
239
240
240
241
# calculating centroid
241
- centroidx = cv .Round ((pt1 [0 ]+ pt2 [0 ])/ 2 )
242
- centroidy = cv .Round ((pt1 [1 ]+ pt2 [1 ])/ 2 )
243
-
242
+ centroidx = cv .Round ((pt1 [0 ]+ pt2 [0 ])/ 2 ) #in pixels
243
+ centroidy = cv .Round ((pt1 [1 ]+ pt2 [1 ])/ 2 )
244
+ col = (centroidx - 640 )* - 0.0035 #in mm, using sensor size of 4.54 mm X 3.42 mm
245
+ row = (centroidy - 360 )* - 0.00475
246
+
244
247
if (centroidx == 0 or centroidy == 0 ):
245
248
print ("no blimp detected from " + sideName )
249
+ blimpDet = 0
250
+ else :
251
+ print (sideName + " side centroid x: " + str (col ) + " y: " + str (row ) + " mm" )
252
+ blimpDet = 1
246
253
247
254
248
255
print ("" )
@@ -266,7 +273,7 @@ def procImg(img,sideName,dispFlag):
266
273
cv .ShowImage (sideName + "_hsv" , small_hsv )
267
274
cv .WaitKey (100 )
268
275
269
- return (centroidx , centroidy )
276
+ return (col , row , blimpDet )
270
277
271
278
272
279
@@ -299,13 +306,40 @@ def procImg(img,sideName,dispFlag):
299
306
if liveIP == 0 :
300
307
#need to be in directory with a bunch of images
301
308
#dirList = os.listdir(os.getcwd())
302
- dirName = 'E:\\ Google Drive\\ Medical_Physics\\ Blimp\\ 2013-09-19\\ 15,10,7.32\\ '
309
+ #Directory containing directories of calibrated images
310
+ dirName = 'E:\\ Google Drive\\ Medical_Physics\\ Blimp\\ 2013-09-19\\ '
303
311
dirList = os .listdir (dirName )
304
- num_base = 0
305
- both_offset = 0
306
- num_imgs = 5 - both_offset
307
- east_offset = 0 + both_offset
308
- west_offset = num_imgs + both_offset
312
+
313
+ #Create a list of files and list of real positions corresponding to each file
314
+ westList = []
315
+ eastList = []
316
+ xpList = []
317
+ ypList = []
318
+ zpList = []
319
+ wL = [] #just filename (no path)
320
+ eL = [] #just filename (no path)
321
+ for file in dirList :
322
+ p = file .split (',' ,3 );
323
+ xp = float (p [0 ])* 1000
324
+ yp = float (p [1 ])* 1000
325
+ zp = float (p [2 ])* 1000
326
+ fpath = dirName + file
327
+ subDirList = os .listdir (fpath )
328
+ len2 = len (subDirList )/ 2
329
+ numWest = len2
330
+ numEast = len2
331
+ for index in range (len2 ):
332
+ #add file name to list
333
+ #print dirName+file+"\\"+subDirList[index]
334
+ eastList .append (dirName + file + "\\ " + subDirList [index ])
335
+ westList .append (dirName + file + "\\ " + subDirList [index + len2 ])
336
+ eL .append (subDirList [index ][11 :13 ])
337
+ wL .append (subDirList [index + len2 ][11 :13 ])
338
+ xpList .append (xp )
339
+ ypList .append (yp )
340
+ zpList .append (zp )
341
+ imgIdx = 0
342
+ totImg = len (xpList )
309
343
310
344
311
345
#address of the control server
@@ -318,21 +352,20 @@ def procImg(img,sideName,dispFlag):
318
352
319
353
dispScale1 = 0.35
320
354
dispScale2 = 0.25
321
-
355
+ firstTime = 1
322
356
323
357
while (1 ):
358
+ blimpDet = 1 #flag saying blimp has been fully detected
324
359
if liveIP :
325
360
#capture images from cameras, store images to file
326
361
urllib .urlretrieve (url_west ,fname_west )
327
362
urllib .urlretrieve (url_east ,fname_east )
328
363
else :
329
- print ("image # = " + str (num_base ))
330
- west_num = west_offset + num_base
331
- east_num = east_offset + num_base
332
- fname_west = dirName + dirList [west_num ]
333
- fname_east = dirName + dirList [east_num ]
334
- num_base = (num_base + 1 )% (num_imgs )
335
- cv .WaitKey (2000 ) #wait for 2 seconds so I can see the output
364
+ print ("image # = " + str (imgIdx ))
365
+ fname_west = westList [imgIdx ]
366
+ fname_east = eastList [imgIdx ]
367
+ #imgIdx = (imgIdx + 1)%(totImg) -> Update below
368
+ #cv.WaitKey(2000) #wait for 2 seconds so I can see the output
336
369
337
370
#open the images from file
338
371
frame_west = cv .LoadImage (fname_west ,cv .CV_LOAD_IMAGE_COLOR );
@@ -342,11 +375,13 @@ def procImg(img,sideName,dispFlag):
342
375
centroids = procImg (frame_west ,"west" ,dispMore )
343
376
centx_west = centroids [0 ]
344
377
centy_west = centroids [1 ]
378
+ blimpDet = centroids [2 ] and blimpDet
345
379
346
380
#find the blimp with one camera, frame is passed in by reference
347
381
centroids = procImg (frame_east ,"east" ,dispMore )
348
382
centx_east = centroids [0 ]
349
383
centy_east = centroids [1 ]
384
+ blimpDet = centroids [2 ] and blimpDet
350
385
351
386
#decimate the resulting images
352
387
small_west = cv .CreateImage ((int (dispScale1 * cv .GetSize (frame_west )[0 ]), int (dispScale1 * cv .GetSize (frame_west )[1 ])), 8 , 3 )
@@ -360,8 +395,9 @@ def procImg(img,sideName,dispFlag):
360
395
cv .WaitKey (100 )
361
396
cv .ShowImage ("east" , small_east )
362
397
cv .WaitKey (100 )
363
-
364
- if (centx_west != 0 and centy_west != 0 and centx_east != 0 and centy_east != 0 ):
398
+
399
+ coord3D = [0 ,0 ,0 ]
400
+ if (blimpDet ):
365
401
#get the 3D location of the blimp
366
402
coord3D = triang_3D (centx_west , centy_west , centx_east , centy_east )
367
403
@@ -383,6 +419,34 @@ def procImg(img,sideName,dispFlag):
383
419
## #we got disconnected somehow, reconnect
384
420
## s = connect(ip,port)
385
421
print ("-----------------------------------" )
386
-
422
+
423
+ if liveIP :
424
+ #nothing yet
425
+ firstTime = 0
426
+
427
+ else :
428
+ #write out positions into a csv file
429
+ if firstTime == 1 :
430
+ #overwrite
431
+ fo = open (dirName + "..\\ tracking_optimization.txt" , "w" )
432
+ fo .write ("Wx(mm)\t Wy(mm)\t Ex(mm)\t Ey(mm)\t " )
433
+ fo .write ("Tru3Dx(mm)\t Tru3Dy(mm)\t Tru3Dz(mm)\t " )
434
+ fo .write ("Est3Dx(mm)\t Est3Dy(mm)\t Est3Dz(mm)\t " )
435
+ fo .write ("Err3Dx(mm)\t Err3Dy(mm)\t Err3Dz(mm)\n " )
436
+ firstTime = 0
437
+ else :
438
+ #append
439
+ fo = open (dirName + "..\\ tracking_optimization.txt" , "a" )
440
+ if blimpDet == 1 :
441
+ fo .write (str (centx_west )+ "\t " + str (centy_west )+ "\t " + str (centx_east )+ "\t " + str (centy_east )+ "\t " )
442
+ fo .write (str (xpList [imgIdx ])+ "\t " + str (ypList [imgIdx ])+ "\t " + str (zpList [imgIdx ])+ "\t " )
443
+ fo .write (str (coord3D [0 ])+ "\t " + str (coord3D [1 ])+ "\t " + str (coord3D [2 ])+ "\t " )
444
+ errX = abs (coord3D [0 ] - xpList [imgIdx ])
445
+ errY = abs (coord3D [1 ] - ypList [imgIdx ])
446
+ errZ = abs (coord3D [2 ] - zpList [imgIdx ])
447
+ fo .write (str (errX )+ "\t " + str (errY )+ "\t " + str (errZ )+ "\t " )
448
+ fo .write (wL [imgIdx ]+ "\t " + eL [imgIdx ]+ "\n " )
449
+ fo .close ()
450
+ imgIdx = (imgIdx + 1 )% (totImg )
387
451
388
452
######################################################
0 commit comments