Skip to content

Commit d456932

Browse files
committed
Update interface/file io
Output a file with tracking calibration data. This data will be used in a matlab optimization routine for improving the camera matrices.
1 parent 9e83b9a commit d456932

File tree

1 file changed

+96
-32
lines changed

1 file changed

+96
-32
lines changed

BlimpDetect.py

Lines changed: 96 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ def triang_3D(col_1, row_1, col_2, row_2) :
5555

5656
#Corrected camera matrix for west side
5757
#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]])
5960
#Corrected camera matrix for east side
6061
#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]])
6264
#blimp position from camera 1
6365
#col_1 = 396
6466
#row_1 = 424
@@ -67,14 +69,12 @@ def triang_3D(col_1, row_1, col_2, row_2) :
6769
#col_2 = 518
6870
#row_2 = 538
6971

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
7878

7979
#col_1 = 0.8117
8080
#row_1 = -0.9851
@@ -228,7 +228,8 @@ def procImg(img,sideName,dispFlag):
228228

229229
#if dispFlag:
230230
#print("Area= " + str(area))
231-
231+
232+
#Largest area over 5000 pixels
232233
if (area > 5000) and (area > prevArea):
233234
pt1 = (bound_rect[0], bound_rect[1])
234235
pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
@@ -238,11 +239,17 @@ def procImg(img,sideName,dispFlag):
238239
cv.Rectangle(img, pt1, pt2, cv.CV_RGB(255,0,0), 3)
239240

240241
# 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+
244247
if (centroidx == 0 or centroidy == 0):
245248
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
246253

247254

248255
print("")
@@ -266,7 +273,7 @@ def procImg(img,sideName,dispFlag):
266273
cv.ShowImage(sideName + "_hsv", small_hsv)
267274
cv.WaitKey(100)
268275

269-
return (centroidx, centroidy)
276+
return (col, row, blimpDet)
270277

271278

272279

@@ -299,13 +306,40 @@ def procImg(img,sideName,dispFlag):
299306
if liveIP == 0:
300307
#need to be in directory with a bunch of images
301308
#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\\'
303311
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)
309343

310344

311345
#address of the control server
@@ -318,21 +352,20 @@ def procImg(img,sideName,dispFlag):
318352

319353
dispScale1 = 0.35
320354
dispScale2 = 0.25
321-
355+
firstTime = 1
322356

323357
while(1):
358+
blimpDet = 1 #flag saying blimp has been fully detected
324359
if liveIP:
325360
#capture images from cameras, store images to file
326361
urllib.urlretrieve(url_west,fname_west)
327362
urllib.urlretrieve(url_east,fname_east)
328363
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
336369

337370
#open the images from file
338371
frame_west = cv.LoadImage(fname_west,cv.CV_LOAD_IMAGE_COLOR);
@@ -342,11 +375,13 @@ def procImg(img,sideName,dispFlag):
342375
centroids = procImg(frame_west,"west",dispMore)
343376
centx_west = centroids[0]
344377
centy_west = centroids[1]
378+
blimpDet = centroids[2] and blimpDet
345379

346380
#find the blimp with one camera, frame is passed in by reference
347381
centroids = procImg(frame_east,"east",dispMore)
348382
centx_east = centroids[0]
349383
centy_east = centroids[1]
384+
blimpDet = centroids[2] and blimpDet
350385

351386
#decimate the resulting images
352387
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):
360395
cv.WaitKey(100)
361396
cv.ShowImage("east", small_east)
362397
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):
365401
#get the 3D location of the blimp
366402
coord3D = triang_3D(centx_west, centy_west, centx_east, centy_east)
367403

@@ -383,6 +419,34 @@ def procImg(img,sideName,dispFlag):
383419
## #we got disconnected somehow, reconnect
384420
## s = connect(ip,port)
385421
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)\tWy(mm)\tEx(mm)\tEy(mm)\t")
433+
fo.write("Tru3Dx(mm)\tTru3Dy(mm)\tTru3Dz(mm)\t")
434+
fo.write("Est3Dx(mm)\tEst3Dy(mm)\tEst3Dz(mm)\t")
435+
fo.write("Err3Dx(mm)\tErr3Dy(mm)\tErr3Dz(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)
387451

388452
######################################################

0 commit comments

Comments
 (0)