def triangulateManualAndOutput(filename1, filename2, pointsFile, output_file=None, projections_file=None): projections_ply_file = PlyFile() points, r, t = triangulateWithImagesAndPointFile(filename1, filename2, pointsFile, projections_file=projections_ply_file) scene_ply_file = PlyFile() scene_ply_file.emitPoints(points) if (output_file): scene_ply_file.save(output_file) if projections_file: projections_ply_file.save(projections_file)
def triangulateImages(self, scene_file=None, projections_file=None, silent=False): ''' Triangulates all image matches and outputs triangulates and projections ''' self.matchPairs(KNN=True, filter=True) lastIdxCorrespondences = [] # find corresponding lists of matched image coordinates in images i and i+1 # also find what indices those coordinates have in their image's keypoint list # recover the pose between those two cameras for i in range(len(self.imageList)-1): if not silent: print "Triangulating " + self.imageList[i].fname + " and " + self.imageList[i+1].fname + "..." points1, points2 = self.getPointCorrespondences(i, i+1) indices1, indices2 = self.getIndexCorrespondences(i, i+1) K = self.imageList[i].K E, mask = CVFuncs.findEssentialMat(points1, points2, K) pts, r, t, newMask = CVFuncs.recoverPose(E, points1, points2, K) if i == 0: # if it's the first image pair, then there's no reprojection error to minimize. Keep these triangulated points self.triangulatedPoints[i].extend(CVFuncs.discreteTriangulate(points1, points2, K.matrix, r, t)) self.RTList.append([r,t]) if projections_file: self.projectedPoints[i].extend(draw.getProjections(points1, points2, K.matrix, r, t)) else: # find a list of (a,b) tuples, overlap, where: # a is the index in self.triangulatedPoints[i-1] of a triangulated point between image i-1 and i # make corresponding lists of triangulated points where oldPoints[j] and newPoints[j] both come from the same feature in image i # find the r and t that minimize reprojection error, and save all the new triangulations in self.triangulatedPoints[i] lastR = self.RTList[i-1][0] lastT = self.RTList[i-1][1] overlap = getOverlapIndices(lastIdxCorrespondences, indices1) oldTriangulatedPoints = [] newImagePoints1, newImagePoints2 = [], [] for pair in overlap: oldTriangulatedPoints.append(self.triangulatedPoints[i-1][pair[0]]) newImagePoints1.append(points1[pair[1]]) newImagePoints2.append(points2[pair[1]]) minimumErrorT = CVFuncs.minimizeError(oldTriangulatedPoints, newImagePoints1, newImagePoints2, K.matrix, lastR, lastT, r, t) newTriangulations = CVFuncs.discreteTriangulateWithTwoRT(points1, points2, K.matrix, lastR, lastT, r, minimumErrorT) self.RTList.append([r, minimumErrorT]) self.triangulatedPoints[i].extend(newTriangulations) if projections_file: self.projectedPoints[i].extend(draw.getProjectionsWithTwoRT(points1, points2, K.matrix, lastR, lastT, r, minimumErrorT)) lastIdxCorrespondences = indices2 if scene_file: for i in range(len(self.imageList)-1): scenePly = PlyFile() scenePly.emitPoints(self.triangulatedPoints[i]) scenePly.save(file(CVFuncs.addPostToPath(scene_file, str(i)+"-"+str(i+1)), "w")) if projections_file: for i in range(len(self.imageList)-1): projPly = PlyFile() projPly.emitPoints(self.projectedPoints[i]) projPly.save(file(CVFuncs.addPostToPath(projections_file, str(i)+"-"+str(i+1)), "w"))