|
| 1 | +import os |
| 2 | +import cv2 |
| 3 | +import numpy as np |
| 4 | +from utils.bundleAjust import bundleAdjustment |
| 5 | +from utils.dense import denseMatch, denseReconstruction, outputPly |
| 6 | +from utils.fundamental import default, implementacionRansac |
| 7 | +from utils.getPose import getPose |
| 8 | +from utils.graph import createGraph, triangulateGraph, showGraph, visualizeDense |
| 9 | +from utils.mergeGraph import mergeG, removeOutlierPts |
| 10 | +from utils.paresDescript import getPairSIFT |
| 11 | + |
| 12 | +#Creditos a % SFMedu: Structrue From Motion for Education Purpose |
| 13 | +# % Written by Jianxiong Xiao (MIT License) el codigo se base en este |
| 14 | + |
| 15 | + |
| 16 | +def mergeAllGraph(gL,imsize): |
| 17 | + graphMerged = gL[0] |
| 18 | + # merge de vistas parciales |
| 19 | + for i in range(len(gL) - 1): |
| 20 | + graphMerged = updateMerged(graphMerged, gL[i+1],imageSize) |
| 21 | + return graphMerged |
| 22 | +def updateMerged(gA,gB,imsize): |
| 23 | + gt = mergeG(gA, gB) |
| 24 | + gt = triangulateGraph(gt, imsize) |
| 25 | + gt = bundleAdjustment(gt, False) |
| 26 | + gt = removeOutlierPts(gt, 10) |
| 27 | + gt = bundleAdjustment(gt) |
| 28 | + return gt |
| 29 | + |
| 30 | +if __name__ == "__main__": |
| 31 | + maxSize = 640 #maxima resolucion de imagen |
| 32 | + carpetaImagenes = 'C:/Users/andres/Desktop/SFMedu2/images/cr/' |
| 33 | + # carpetaImagenes = "C:/Users/andres/Desktop/datasetImagenes/Reduced/jirafa/" |
| 34 | + # carpetaImagenes = "C:/Users/andres/Desktop/datasetImagenes/Reduced/apple/" |
| 35 | + debug = True |
| 36 | + outName = "apple" |
| 37 | + |
| 38 | + algoMatrizFundamental = implementacionRansac |
| 39 | + |
| 40 | + #declaraciones |
| 41 | + |
| 42 | + graphList = [] |
| 43 | + |
| 44 | + #Cargar imagenes |
| 45 | + listaArchivos = os.listdir(carpetaImagenes) |
| 46 | + tiposValidos = ['jpg','png','JPG'] #tipos validos de imagenes |
| 47 | + listaImages = filter(lambda x : x.split('.')[-1] in tiposValidos,listaArchivos ) |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + #Intentar conseguir la distancia focal |
| 52 | + # TODO agregar calculo este valor deberia funcionar con imagenes 480x640 focalLen 4mm |
| 53 | + f=719.5459 |
| 54 | + |
| 55 | + #Carga las imagenes |
| 56 | + listaImages = map(lambda x : cv2.imread(carpetaImagenes+x),listaImages) |
| 57 | + |
| 58 | + imageSize = listaImages[0].shape |
| 59 | + print "Dimensiones originales ",imageSize |
| 60 | + #todo Escala la imagen si pasa de maxSize |
| 61 | + if imageSize[0] > maxSize: |
| 62 | + print "Escalando" |
| 63 | + print "Size image ",imageSize," max size ",maxSize |
| 64 | + #480 640 funciona |
| 65 | + listaImages = map(lambda x: np.transpose(cv2.resize(x,(640,480)),axes=[1,0,2]), listaImages) |
| 66 | + imageSize = listaImages[0].shape |
| 67 | + print "Result size ",imageSize |
| 68 | + |
| 69 | + #calculo de matriz K |
| 70 | + K = np.eye(3) |
| 71 | + K[0][0] = f |
| 72 | + K[1][1] = f |
| 73 | + |
| 74 | + graphList = [0 for i in range(len(listaImages)-1)] |
| 75 | + #calcula pares a partir de SIFT u otro descriptor local |
| 76 | + #Se calculan como imagenes sucesivas |
| 77 | + print "Inicia calculo de pares SIFT" |
| 78 | + for i in range(len(listaImages)-1): |
| 79 | + keypointsA,keypointsB = getPairSIFT(listaImages[i],listaImages[i+1],show=debug) |
| 80 | + |
| 81 | + |
| 82 | + #Calcular la matriz fundamental o la matriz escencial |
| 83 | + #TODO conseguir las demas |
| 84 | + if type(keypointsA[0]) == np.ndarray: |
| 85 | + assert(len(keypointsA.shape) == 2) |
| 86 | + assert (len(keypointsB.shape) == 2) |
| 87 | + pointsA = keypointsA |
| 88 | + pointsB = keypointsB |
| 89 | + else: |
| 90 | + pointsA = np.array([(keypointsA[idx].pt) for idx in range(len(keypointsA))]).reshape(-1, 1, 2) |
| 91 | + pointsB = np.array([(keypointsB[idx].pt) for idx in range(len(keypointsB))]).reshape(-1, 1, 2) |
| 92 | + pointsA = pointsA[:,[1,0]] |
| 93 | + pointsB = pointsB[:, [1, 0]] |
| 94 | + |
| 95 | + F = np.array(algoMatrizFundamental(pointsA,pointsB)) |
| 96 | + Fmat = F[0] |
| 97 | + K = np.array(K) |
| 98 | + E = np.dot(np.transpose(K),np.dot(Fmat,K)) |
| 99 | + |
| 100 | + # Conseguir pose de las camaras |
| 101 | + Rtbest = getPose(E,K, np.hstack([pointsA,pointsB]),imageSize) |
| 102 | + |
| 103 | + #Crear grafico |
| 104 | + graphList[i] = createGraph(i,i+1,K, pointsA, pointsB, Rtbest, f) |
| 105 | + |
| 106 | + #Triangular |
| 107 | + graphList[i] = triangulateGraph(graphList[i],imageSize) |
| 108 | + |
| 109 | + #visualizar grafico |
| 110 | + # showGraph(graphList[i],imageSize) |
| 111 | + |
| 112 | + #Bundle ajustement |
| 113 | + graphList[i]=bundleAdjustment(graphList[i]) |
| 114 | + |
| 115 | + #Visualiza con mejoras |
| 116 | + # showGraph(graphList[i], imageSize) |
| 117 | + |
| 118 | + gM = mergeAllGraph(graphList,imageSize) |
| 119 | + print "Merge de grafos finalizado" |
| 120 | + #Visualizar resultado parcial |
| 121 | + showGraph(gM,imageSize) |
| 122 | + #Dense matching |
| 123 | + for i in range(len(listaImages)-1): |
| 124 | + graphList[i] = denseMatch(graphList[i],listaImages[i], |
| 125 | + listaImages[i+1], imageSize, imageSize) |
| 126 | + |
| 127 | + print "Dense match finalizado" |
| 128 | + print "Inicializando dense Triangulation" |
| 129 | + #Dense reconstruction |
| 130 | + for i in range(len(listaImages) - 1): |
| 131 | + graphList[i] = denseReconstruction(graphList[i], gM,K,imageSize) |
| 132 | + print "Dense reconstruct finalizado" |
| 133 | + data = visualizeDense(graphList, gM, imageSize) |
| 134 | + |
| 135 | + outputPly(data,outName) |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
0 commit comments