-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tringulate__Nonnumba.pyx
592 lines (507 loc) · 24.4 KB
/
Tringulate__Nonnumba.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 12 21:45:14 2018
@author: Eng-Yasin
"""
import glob
from pdb import set_trace as bp
from time import time as watch
import matplotlib.pyplot as plt
from pointStruct import pointStruct
############### THE IMPORTS ########################
import numpy as np
cimport numpy as cnp
from mpl_toolkits.mplot3d import Axes3D
#from multiprocessing import process
import cv2
import pcl
#from read_poses_ekf_sim import GetMatrices
#or for ekf file,
from read_poses_sim import GetMatrices
from selectROI import SelectROI
#####################################################
import cython
##########################################################################
#Methods with njit
##########################################################################
ctypedef cnp.float32_t FLOAT
ctypedef cnp.uint8_t UINT8
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef list GiveGrid(int step_size,list rect):
#cdef list[(int,int,int)] grid
cdef unsigned int x,y
grid = [(x, y, step_size) for y in xrange(rect[1], rect[3], step_size)
for x in xrange(rect[0], rect[2], step_size)]
return grid
cpdef cnp.ndarray[FLOAT, ndim=2] strip_out(cnp.ndarray[FLOAT, ndim=3] arrayT):
cdef unsigned int a,b
a,b = arrayT.shape[0], arrayT.shape[2]
return arrayT.reshape((a, b))
#cdef double[:,:] x
#res = [x[0] for x in arrayT]
#return res
cpdef list calc_errors(cnp.ndarray[FLOAT,ndim=2] pn1,cnp.ndarray[FLOAT,ndim=2] pnt1,
cnp.ndarray[FLOAT,ndim=2] pn2,cnp.ndarray[FLOAT,ndim=2] pnt2):
cdef cnp.ndarray[FLOAT,ndim=2] o ,p
cdef cnp.ndarray[FLOAT,ndim=1] x
cdef list a,b,errors
cdef double u,v
o = np.subtract(pn1,pnt1)**2
a = []
b = []
for x in np.transpose(o):
a.append(np.sqrt(x[0]+x[1]))
#a = np.sqrt(map(lambda x: x[0]+x[1],o.T))
p = np.subtract(pn2,pnt2)**2
for x in np.transpose(p):
b.append(np.sqrt(x[0]+x[1]))
#b = np.sqrt(map(lambda x: x[0]+x[1],o.T))
errors = [(u+v)/2.0 for u,v in zip(a,b)]
#errors = np.array(a) + np.array(b)
#errors = np.mean(np.vstack((a,b)),axis=0)
return errors
cpdef list get_good_2(list good,list good3DPointsIndx):
cdef bint y
good_2 = [x for x,y in zip(good,good3DPointsIndx) if y]
#good_2 = [p for i,p in zip(good3DPointsIndx,good) if i]
return good_2
MakeKpsGrid = lambda x:cv2.KeyPoint(*x)
#Sort2MostVar= lambda x:np.var(x[1])
cdef float Sort2MostVar(x):
#for ORB
cdef cnp.ndarray[UINT8,ndim=1] x1
x1 = x[1]
return np.var(x1)
#ArrangeNames = lambda x: int(x[len(direct)+1:-4])
##########################################################################
#End of Methods
##########################################################################
cdef class ImagesWithPoses(object):
cdef public list all_poses, all_vec_pose, Rs, Ts, image_names, Rects, nodesAndProjs
cdef public list pointsCloud, PointsBankActive , PointsBankNonActive, CurrPrjErr
cdef public unsigned int SiftStep, Image_Index , UpperLimitOfFeats
cdef public double watch
cdef public float lowe_ratio , ReprojectThresh
cdef public str direct, FeauterType
cdef public cnp.float64_t[:,:] Instrinc_Mat
#may need to enter it as float
cdef public cnp.float32_t[:] Destoration
cdef public cnp.uint8_t[:,:,:] I_last
#cdef public unsigned char [:,:,:] I_last
cdef public object activeFeat, activeFlann , PointsFile , ProjsFile
def __init__(self,int SiftStep,str direct="",int checks_match_num=75, float Lowe_ratio = 0.75,
int out_Max = 20000, str FeauterType = 'ORB', int UpperLimitOfFeats = 30000):
self.watch = watch()
if len(direct)==0:
raise IOError("Please Enter The Valid Images Directory")
else:self.direct = direct
self.all_poses,self.all_vec_pose,self.Rs,self.Ts,self.Instrinc_Mat,self.Destoration = GetMatrices(self.direct)
image_names = glob.glob(direct+"/*.png")
self.image_names = sorted(image_names,key=self.ArrangeNames)
self.SiftStep = SiftStep
self.FeauterType = FeauterType
self.UpperLimitOfFeats = UpperLimitOfFeats
if FeauterType == 'SIFT':
self.activeFeat = cv2.xfeatures2d.SIFT_create(out_Max)
# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=checks_match_num) # or pass empty dictionary
self.activeFlann = cv2.FlannBasedMatcher(index_params,search_params)
elif FeauterType == 'ORB':
self.activeFeat = cv2.ORB_create(out_Max)
FLANN_INDEX_LSH = 6
index_params = dict(algorithm = FLANN_INDEX_LSH,
table_number = 12,
key_size = 20,
multi_probe_level = 2,
trees = 3)
search_params = dict(checks=checks_match_num) # or pass empty dictionary
self.activeFlann = cv2.FlannBasedMatcher(index_params,search_params)
self.lowe_ratio = Lowe_ratio
#np.save('allP',np.array(self.all_poses))
self.pointsCloud = []
#self.nodesAndProjs = []
self.Image_Index = 0
self.PointsFile = file('pointsNew.txt','w+')
self.ProjsFile = file('NodesProjs.txt','w+')
self.PointsBankActive = []
self.PointsBankNonActive = []
self.Rects = [[],[]]
#self.pool2go = Pool(3)
self.CurrPrjErr = []
def ArrangeNames(self,x):
return int(x[len(self.direct)+1:-4])
cpdef DetectComputeDenseSift(self,cnp.ndarray[UINT8, ndim=3] img,unsigned int step_size,list rect):
#kp = [cv2.KeyPoint(x, y, step_size) for y in range(0, gray.shape[0], step_size)
# for x in range(0, gray.shape[1], step_size)]
cdef list kps_in,Ks,sortedKsDs,Ks_list, kps_out = []
cdef cnp.ndarray[UINT8, ndim=2] mask, Ds_np, Ds
cdef unsigned int N
if step_size:
grid = GiveGrid(step_size,rect)
kps_in = map(MakeKpsGrid,grid)
mask = np.ones((img.shape[0], img.shape[1]),dtype=np.uint8)
mask[rect[1]:rect[3],rect[0]:rect[2]] = 0
else:
kps_in = []
kps_out = self.activeFeat.detect(img,None)
if not(kps_out): kps_out = self.activeFeat.detect(img,mask)
N = len(kps_in)+len(kps_out)
print('Number of Kps {}'.format(N))
if N>self.UpperLimitOfFeats:
Ks,Ds = self.activeFeat.compute(img,kps_in+kps_out)
#take the most variance
#with nogil:
sortedKsDs = sorted(zip(Ks,Ds),key=Sort2MostVar)
Ks_,Ds_ = zip(*sortedKsDs[-self.UpperLimitOfFeats:])
""""
Ks_,Ds_ = [], []
for KD in sortedKsDs[-self.UpperLimitOfFeats:]:
Ks_.append(KD[0])
Ds_.append(KD[1])
"""
# for ORB here
Ds_np = np.asarray(Ds_,dtype=np.uint8)
Ks_list = list(Ks_)
return Ks_list , Ds_np
return self.activeFeat.compute(img,kps_in+kps_out)
def DetectComputeHDenseSift(self,gray,step_size):
kp = self.activeFeat.detect(gray)
kp += [cv2.KeyPoint(x, y, step_size) for y in xrange(0, gray.shape[0], step_size)
for x in xrange(0, gray.shape[1], step_size)]
#kp = self.sift.detect(gray)
print(len(kp),' Number of Kps')
return self.activeFeat.compute(gray,kp)
cpdef list FindRect(self,cnp.ndarray[UINT8, ndim=3] img1,list rect,cnp.ndarray[UINT8, ndim=3] img2):
cdef cnp.ndarray[UINT8, ndim=2] I1,I2,temp
cdef cnp.ndarray[FLOAT, ndim=2] res
cdef unsigned int w,h
cdef float min_val, max_val
cdef (int,int) bottom_right, min_loc, max_loc
I1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
I2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
# rect is x,y,x*,y* putten y:y*,x:x*
# rect = map(int,rect1)
temp = I1[rect[1]:rect[3],rect[0]:rect[2]]
h,w = temp.shape[0],temp.shape[1]
# methode is cv2.TM_CCOEFF (4) or Normalized (5)
res = cv2.matchTemplate(I2,temp,5)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
print('max val is:' + str(max_val))
if max_val < 0.7:
k = SelectROI(self.image_names[self.Image_Index+1])
Rects = map(int,k.rect)
return Rects
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
# same as rect x,y,x1,y1
return [top_left[0] , top_left[1] , bottom_right[0] , bottom_right[1]]
def FirstTwo(self,Draw_matches):
I1 = cv2.imread(self.image_names[0],1)
I2 = cv2.imread(self.image_names[1],1)
"""
I1 = cv2.undistort(I1,
#cv2.imread(self.image_names[0],0),
self.Instrinc_Mat,self.Destoration)
I2 = cv2.undistort(I2,
#cv2.imread(self.image_names[1],0),
self.Instrinc_Mat,self.Destoration)
"""
#for Drawing porpouses:
self.I_last = I2.copy()
if self.SiftStep: self.Rects[1] = self.FindRect(I1,self.Rects[0],I2)
kp1, des1 = self.DetectComputeDenseSift(I1,self.SiftStep,self.Rects[0])
kp2, des2 = self.DetectComputeDenseSift(I2,self.SiftStep,self.Rects[1])
firstImagePoints = [pointStruct(kp,des,0) for kp,des in zip(kp1,des1)]
secondImagePoints = [pointStruct(kp,des,1) for kp,des in zip(kp2,des2)]
matches = self.activeFlann.knnMatch(des1,des2,k=2)
good = []
for mn in matches:
if len(mn)!=2: continue
elif mn[0].distance < self.lowe_ratio*mn[1].distance: good.append(mn[0])
#good = [m for m,n in matches if m.distance < self.lowe_ratio *n.distance]
#src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
#dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
#_, mask = cv2.findFundamentalMat(src_pts, dst_pts,cv2.FM_RANSAC ,
# Max_Error_Allowed,0.99)
#good_after_ransac=[[good[i]] for i,x in enumerate(mask) if x]
p1 = np.float32([ kp1[m.queryIdx].pt for m in good])
p2 = np.float32([ kp2[m.trainIdx].pt for m in good])
#bp()
if Draw_matches:
I12=cv2.drawMatches(I1,kp1,I2,kp2,good,np.vstack((I1,I2)))
cv2.namedWindow('pic', cv2.WINDOW_NORMAL)
cv2.imshow('pic',I12)
cv2.waitKey(0)
ptsrc = cv2.triangulatePoints(self.all_poses[0],self.all_poses[1],p1.T,p2.T)
points_cloud = cv2.convertPointsFromHomogeneous(ptsrc.T)
points_cloud = np.transpose(strip_out(np.transpose(points_cloud)))
#points_cloud = np.array(self.pool2go.map(lambda x: x[0],points_cloud.T)).T
#points_cloud = np.array([z[0] for z in points_cloud.T]).T
good3DPointsIndx = self.ReprojectionError(points_cloud,p1.T,p2.T)
good_2 = get_good_2(good,good3DPointsIndx)
self.CurrPrjErr = get_good_2(self.CurrPrjErr,good3DPointsIndx)
#[p for i,p in enumerate(good) if good3DPointsIndx[i]]
self.PointsBankActive = secondImagePoints
for c,m in enumerate(good_2):
pntS = firstImagePoints[m.queryIdx]
mpntS= self.PointsBankActive[m.trainIdx]
kp,des,i = mpntS.returnKDActive()
pntS.addProj(kp,des,i, self.CurrPrjErr[c])
self.PointsBankActive[m.trainIdx] = pntS
self.pointsFilter()
print('Number of aduqate Points {}'.format(len(good_2)))
#good_after_ransac = good_after_ransac[good3DPointsIndx]
self.Image_Index += 1
cpdef bint AddImage(self,cnp.ndarray[UINT8, ndim=3] I,bint Draw_matches):
#I = cv2.undistort(I,self.Instrinc_Mat,self.Destoration)
cdef list kpn, kp1,des0, matches, good, addedImagePoints, good3DPointsIndx
cdef list good_2, addedImagePointsCopy, tempPntsMat
cdef cnp.ndarray[cnp.npy_bool, ndim=1, cast=True] addedImagePointsIdx,PointsActiveIdx
cdef bint y
cdef cnp.ndarray[FLOAT, ndim=2] p1,p2
cdef cnp.ndarray[FLOAT, ndim=3] points_cloud3
cdef cnp.ndarray[FLOAT, ndim=2] points_cloud
cdef unsigned int i,t,q,c
cdef cnp.ndarray[UINT8, ndim=3, cast=True] I12
#for ORB
#cdef cnp.uint8_t[:,:] des1
cdef cnp.ndarray[UINT8, ndim=2] desn , des1
cdef cnp.ndarray[UINT8, ndim=1] des
cdef cnp.ndarray[FLOAT, ndim=2] ptsrc
if self.SiftStep: self.Rects.append(self.FindRect(np.asarray(self.I_last,dtype=np.uint8),self.Rects[-1],I))
kpn, desn = self.DetectComputeDenseSift(I,self.SiftStep,self.Rects[-1])
kp1,des0 = [],[]#self.Feat_and_Desc_a_b[2][0],self.Feat_and_Desc_a_b[3][0]
print('**************0')
for k in self.PointsBankActive:
kp,des,_ = k.returnKDActive()
kp1.append(kp)
des0.append(des)
print('**************1')
#change this to uint8 4 orb, or float32 for sift, DetectComputeDenseSift
if self.FeauterType=='SIFT': des1 = np.asarray(des0,dtype=np.float32)
elif self.FeauterType == 'ORB': des1 = np.asarray(des0,dtype=np.uint8)
#with nogil:
matches = self.activeFlann.knnMatch(des1,desn,2)
print('**************2')
good = []
for mn in matches:
#if len(mn)==1:good += mn
if len(mn)!=2: continue
elif mn[0].distance < self.lowe_ratio*mn[1].distance: good.append(mn[0])
# adding dense sift if match es aren't enough
"""
if len(good) < 50:
kpn, desn = self.DetectComputeHDenseSift(I,self.SiftStep+2)
matches = self.flann.knnMatch(des1,desn,k=2)
good = []
for mn in matches:
if len(mn)==1:good += mn
elif len(mn)!=2: continue
elif mn[0].distance < 0.75*mn[1].distance: good += [mn[0]]
"""
#if(self.Image_Index>=80):bp()
print('**************3')
addedImagePoints = []
for kp,des in zip(kpn,desn):
addedImagePoints.append(pointStruct(kp,des,self.Image_Index+1))
addedImagePointsIdx = np.ones(len(addedImagePoints),dtype=np.bool)
PointsActiveIdx = np.ones(len(self.PointsBankActive),dtype=np.bool)
#good = [m for m,n in matches if m.distance < 0.75*n.distance]
#src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
#dst_pts = np.float32([ kpn[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
#_, mask = cv2.findFundamentalMat(src_pts, dst_pts,cv2.FM_RANSAC ,
# Max_Error_Allowed,0.99)
#good_after_ransac=[[good[i]] for i,x in enumerate(mask) if x]
#good_after_ransac=[[i] for i in good]
print('**************4')
p1 = np.float32([ kp1[m.queryIdx].pt for m in good ]).T
p2 = np.float32([ kpn[m.trainIdx].pt for m in good ]).T
ptsrc = cv2.triangulatePoints(self.all_poses[self.Image_Index],
self.all_poses[self.Image_Index+1],p1,p2)
points_cloud3 = cv2.convertPointsFromHomogeneous(ptsrc.T)
points_cloud = np.transpose(strip_out(np.transpose(points_cloud3)))
#points_cloud = np.array([z[0] for z in points_cloud.T]).T
good3DPointsIndx = self.ReprojectionError(points_cloud,p1,p2)
print('**************5')
#good_2 = [p for i,p in zip(good3DPointsIndx,good) if i]
good_2 = get_good_2(good,good3DPointsIndx)
self.CurrPrjErr = get_good_2(self.CurrPrjErr,good3DPointsIndx)
#good_after_ransac = good_after_ransac[good3DPointsIndx]
#newPointsBankActive = []
for c,m in enumerate(good_2):
q,t = m.queryIdx,m.trainIdx
originalP = self.PointsBankActive[q]
otherP = addedImagePoints[t]
k,d,i = otherP.returnKDActive()
originalP.addProj(k,d,i,self.CurrPrjErr[c])
#newPointsBankActive.append(originalP)
#self.PointsBankActive[m.queryIdx] = originalP
addedImagePointsIdx[t] = False
PointsActiveIdx[q] = False
print('**************6')
tempPntsMat = []
for x,y in zip(self.PointsBankActive,PointsActiveIdx):
if y:
self.PointsBankNonActive.append(x)
else:
tempPntsMat.append(x)
#self.PointsBankNonActive.extend([x for x,y in zip(self.PointsBankActive,PointsActiveIdx) if y])
#tempPntsMat = [x for x,y in zip(self.PointsBankActive,PointsActiveIdx) if not(y)]
self.PointsBankActive = tempPntsMat
addedImagePointsCopy = [x for x,y in zip(addedImagePoints,addedImagePointsIdx) if y]
self.PointsBankActive.extend(addedImagePointsCopy)
#self.pointsFilter()
print('Number of aduqate Points {}'.format(len(good_2)))
#bp()#self.TestPointsVsF(p1,p2)
cv2.destroyAllWindows()
if Draw_matches:
I12=cv2.drawMatches(np.asarray(self.I_last,dtype=np.uint8),kp1,I,kpn,good_2,np.vstack((self.I_last,I)))
cv2.namedWindow('pic'+str(len(good_2)), cv2.WINDOW_NORMAL)
cv2.imshow('pic'+str(len(good_2)),I12)
k=cv2.waitKey(300)
if k==27 & 0xFF:return True
print('**************7')
self.I_last = I.copy()
self.Image_Index += 1
return False
def TringulateAll(self,ReprojectThresh,Draw_matches=False,load_auto ="",
tracks_diffs = 1, min_projections = 3, FinalErrPrj = 1.0):
cdef cnp.ndarray[FLOAT, ndim=1] Prslt
if self.SiftStep:
k = SelectROI(self.image_names[0])
self.Rects[0] = map(int,k.rect)
if load_auto:
self.PointsBankNonActive = np.load(load_auto)
for x in xrange(len(self.PointsBankNonActive)):
self.PointsBankNonActive[x].prop2retrive()
else:
self.ReprojectThresh = ReprojectThresh
self.FirstTwo(Draw_matches)
for img in self.image_names[2:]:
print("adding image"+img)
#I = cv2.cvtColor(cv2.imread(img,1),cv2.COLOR_BGR2YUV)
I = cv2.imread(img,1)
if self.AddImage(I,Draw_matches):
break
cv2.destroyAllWindows()
print("Tracking Points is over , Now to 3D")
# some clean up
for ps in self.PointsBankActive:
if ps.numberOfProjs > 1:
self.PointsBankNonActive.append(ps)
Points2save = np.array(self.PointsBankNonActive[:]).copy()
#bp()
"""
for x in range(len(self.PointsBankNonActive)):
Points2save[x].prop2save()
#self.PointsBankNonActive[x].prop2retrive()
np.save("LastResult",Points2save)
for x in range(len(self.PointsBankNonActive)):
Points2save[x].prop2retrive()
"""
self.nodesAndProjs = [[] for x in self.image_names]
m = 1
#N_true = 0
for ps in self.PointsBankNonActive:
if (ps.numberOfProjs < min_projections):
if len(ps.Errors)==0:continue
elif (np.mean(ps.Errors)>FinalErrPrj):continue
# this is bug, pnts from second image!
#print("Really still zeros here with {} projection".format(ps.numberOfProjs))
Prslt = ps.track23D(self.all_poses,tracks_diffs)
if any(Prslt):
self.pointsCloud.append(Prslt)
#print(cv2.triangulatePoints(self.all_poses[0],self.all_poses[1],np.float32([ps.kpStruct[0].pt]).T,np.float32([ps.kpStruct[1].pt]).T))
#print(ps.track2d3(self.Ts,self.Rs,self.Instrinc_Mat,2))
#self.pointsCloud.append(ps.track2d3(self.Ts,self.Rs,self.Instrinc_Mat,1))
#add projections form ps
for j,i in enumerate(ps.imagesIndices):
self.nodesAndProjs[i].append([m]+ps.kpPositions[j])
m += 1
N = len(self.pointsCloud)
print("Tringluate Complete with {0} Points from {1} Cameras".format(N,
self.Image_Index+1))
def WriteNodes3DPoints(self):
#all zero as start
self.PointsFile.writelines(str(np.zeros((1,12))[0])[1:-1])
for p in self.pointsCloud:
#No covariance
p = list(p)
p.extend([0,0,0,0,0,0,0,0,0])
self.PointsFile.writelines('\n')
self.PointsFile.writelines(" ".join(map(str,p)))
self.PointsFile.close()
#Note: Last line here is \n i.e: empty
for i,p in enumerate(self.nodesAndProjs):
self.ProjsFile.writelines('P'+str(i+1)+'\n')
self.ProjsFile.writelines("\n".join(map(str,self.all_vec_pose[i])))
self.ProjsFile.writelines('\n')
for pInImg in p:
self.ProjsFile.writelines(" ".join(map(str,pInImg)))
self.ProjsFile.writelines('\n')
self.ProjsFile.close()
xyz = np.float32(self.pointsCloud)
#pcloud = pcl.PointCloud_PointXYZRGB()
pcloud_nocolor = pcl.PointCloud()
Magnifize = 10
xyz*=Magnifize
#mean_c = np.float32([[(int(c)<<16)|(int(c)<<8)|int(c)] for c in self.D3_points_index[2][0]])
#bp()
#xyzc = np.hstack((xyz,mean_c))
#pcloud.from_array(xyzc)
pcloud_nocolor.from_array(xyz)
#pcloud.to_file(self.direct+'/xyz_cloud.pcd')
pcloud_nocolor.to_file(self.direct+'/xyz_cloud_nocolor.xyz')
print("Time taken is : {0} second".format(watch() - self.watch))
cpdef list ReprojectionError(self,cnp.ndarray[FLOAT, ndim=2] Point_cloud,
cnp.ndarray[FLOAT, ndim=2] pnt1, cnp.ndarray[FLOAT, ndim=2] pnt2):
cdef unsigned int n,m,npts
cdef float NewThresh,x
cdef cnp.ndarray[FLOAT, ndim=2] pn1,pn2
cdef cnp.ndarray[FLOAT, ndim=3] pnr1,pnr2
cdef list errors, Result
n = self.Image_Index
m = self.Image_Index + 1
#Todo : do u rellay need Destoration if aleardy undistrored
pnr1,_ = cv2.projectPoints(Point_cloud,self.Rs[n],self.Ts[n],
np.array(self.Instrinc_Mat),np.array([[0.0]*5]))
pnr2,_ = cv2.projectPoints(Point_cloud,self.Rs[m],self.Ts[m],
np.array(self.Instrinc_Mat),np.array([[0.0]*5]))
pn1 = np.transpose(strip_out(pnr1))
pn2 = np.transpose(strip_out(pnr2))
errors = calc_errors(pn1,pnt1,pn2,pnt2)
"""
o = (pn1- pnt1)**2
a = np.sqrt(map(lambda x: x[0]+x[1],o.T))
o = (pn2- pnt2)**2
b = np.sqrt(map(lambda x: x[0]+x[1],o.T))
errors = np.mean(np.vstack((a,b)),axis=0)
"""
self.CurrPrjErr = errors
Result = [x<=self.ReprojectThresh for x in errors]
#Result = errors<=self.ReprojectThresh
if sum(Result)<20:
#no less than 40pts
npts = [40,len(errors)-1][len(errors)<41]
NewThresh = sorted(errors)[npts]
print("New Thresh is: "+str(NewThresh))
#return errors <NewThresh
return [x<=NewThresh for x in errors]
else:
return Result
#New_point_clouds = Point_cloud[errors<thresh]
####################################################
### All the following methods for Future Development:
####################################################
def pointsFilter(self):
newPointsBankActive = []
for ps in self.PointsBankActive:
if ps.imagesIndices[-1] == self.Image_Index+1 :
newPointsBankActive.append(ps)
continue
elif ps.numberOfProjs > 1:
self.PointsBankNonActive.append(ps)
self.PointsBankActive = newPointsBankActive