Skip to content

Commit 3438fa5

Browse files
committed
Added error calculations
Estimates how far apart the two rays are from eachother. Helps to give a confidence in the estimated position. If the error is too large, then the object is not likely to be the blimp.
1 parent 43af4a2 commit 3438fa5

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

BlimpDetect-8-28_JB.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ def triang_3D(col_1, row_1, col_2, row_2) :
8787
x = M1[0]/M1[3]
8888
y = M1[1]/M1[3]
8989
z = M1[2]/M1[3]
90+
v1L = np.array([x0, y0, z0])
91+
v2L = np.array([x, y, z])
9092
a = x-x0
9193
b = y-y0
9294
c = z-z0
@@ -103,6 +105,8 @@ def triang_3D(col_1, row_1, col_2, row_2) :
103105
x = M2[0]/M2[3]
104106
y = M2[1]/M2[3]
105107
z = M2[2]/M2[3]
108+
v1R = np.array([x1, y1, z1])
109+
v2R = np.array([x, y, z])
106110
d = x-x1
107111
e = y-y1
108112
f = z-z1
@@ -120,8 +124,26 @@ def triang_3D(col_1, row_1, col_2, row_2) :
120124
x_coord = x0+a*r[0]
121125
y_coord = y0+b*r[0]
122126
z_coord = z0+c*r[0]
127+
M = np.array([x_coord, y_coord, z_coord])
123128

124-
return (x_coord[0], y_coord[0], z_coord[0])
129+
#compute distance to each ray from the estimated 3D location
130+
v1L = v1L.transpose()
131+
v2L = v2L.transpose()
132+
v1R = v1R.transpose()
133+
v2R = v2R.transpose()
134+
135+
d1 = LA.norm(np.cross(np.subtract(v1L, v2L),np.subtract(M.transpose(),v2L)))/LA.norm(np.subtract(v1L,v2L))
136+
d2 = LA.norm(np.cross(np.subtract(v1R, v2R),np.subtract(M.transpose(),v2R)))/LA.norm(np.subtract(v1R,v2R))
137+
err1 = d1 + d2;
138+
139+
#project the estimated 3D position onto image, then find distance from original position
140+
m1rn = np.dot(P1,np.vstack((M,[1])))
141+
m2rn = np.dot(P2,np.vstack((M,[1])))
142+
m1r = m1rn/m1rn[2]
143+
m2r = m2rn/m2rn[2]
144+
err2 = np.sqrt(np.sum(np.square(m1r[0:2]-m1[0:2]))) + np.sqrt(np.sum(np.square(m2r[0:2]-m2[0:2])))
145+
146+
return (x_coord[0], y_coord[0], z_coord[0], err1, err2)
125147

126148
#---------------------------------------------------------
127149
def getthresholdedimg(im):
@@ -188,7 +210,7 @@ def procImg(img,sideName):
188210
pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
189211

190212
# Draw bounding rectangle
191-
cv.Rectangle(img, pt1, pt2, cv.CV_RGB(255,0,0), 1)
213+
cv.Rectangle(img, pt1, pt2, cv.CV_RGB(255,0,0), 3)
192214

193215
# calculating centroid
194216
centroidx = cv.Round((pt1[0]+pt2[0])/2)
@@ -217,16 +239,16 @@ def procImg(img,sideName):
217239
url_west = 'http://10.129.20.12/snapshot/view0.jpg'
218240

219241
# three windows that will open upon execution
220-
cv.NamedWindow("west",cv.CV_WINDOW_NORMAL)
221-
cv.NamedWindow("east",cv.CV_WINDOW_NORMAL)
242+
cv.NamedWindow("west",cv.CV_WINDOW_AUTOSIZE)
243+
cv.NamedWindow("east",cv.CV_WINDOW_AUTOSIZE)
222244

223245
#address of the control server
224246
ip = "md-red5.discovery.wisc.edu"
225247
port = 7779
226248
size = 1024
227249

228250
#first get a connection to the server
229-
s = connect(ip,port)
251+
#s = connect(ip,port)
230252

231253

232254
while(1):
@@ -251,10 +273,17 @@ def procImg(img,sideName):
251273
centx_east = centroids[0]
252274
centy_east = centroids[1]
253275

276+
#decimate the resulting images
277+
small_west = cv.CreateImage((int(0.5*cv.GetSize(frame_west)[0]), int(0.5*cv.GetSize(frame_west)[1])), 8, 3)
278+
small_east = cv.CreateImage((int(0.5*cv.GetSize(frame_east)[0]), int(0.5*cv.GetSize(frame_east)[1])), 8, 3)
279+
cv.Resize(frame_west, small_west)
280+
cv.Resize(frame_east, small_east)
281+
282+
254283
#display the images with the blimp outlined
255-
cv.ShowImage("west", frame_west)
284+
cv.ShowImage("west", small_west)
256285
cv.WaitKey(100)
257-
cv.ShowImage("east", frame_east)
286+
cv.ShowImage("east", small_east)
258287
cv.WaitKey(100)
259288

260289
#get the 3D location of the blimp
@@ -263,18 +292,20 @@ def procImg(img,sideName):
263292
print("x_3d: " + str(coord3D[0]))
264293
print("y_3d: " + str(coord3D[1]))
265294
print("z_3d: " + str(coord3D[2]))
295+
print("err1: " + str(coord3D[3]))
296+
print("err2: " + str(coord3D[4]))
266297
print("-----------------------------------")
267298

268-
#send the 3D location to the control server
269-
try:
270-
#x,y,z = getPosition()
271-
msg = "" + str(coord3D[0]) + "," + str(coord3D[1]) + "," + str(coord3D[2]) + "\n"
272-
s.send(msg)
273-
#time.sleep(1)
274-
except Exception as err:
275-
print("disconnected")
276-
#we got disconnected somehow, reconnect
277-
s = connect(ip,port)
299+
## #send the 3D location to the control server
300+
## try:
301+
## #x,y,z = getPosition()
302+
## msg = "" + str(coord3D[0]) + "," + str(coord3D[1]) + "," + str(coord3D[2]) + "\n"
303+
## s.send(msg)
304+
## #time.sleep(1)
305+
## except Exception as err:
306+
## print("disconnected")
307+
## #we got disconnected somehow, reconnect
308+
## s = connect(ip,port)
278309

279310

280311

0 commit comments

Comments
 (0)