forked from emiltan97/pmvs-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expansion.py
124 lines (113 loc) · 4.2 KB
/
expansion.py
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
from math import cos, pi
from numpy.linalg.linalg import norm
from initialmatch import computeVpStar, refinePatch
from classes import Patch
import utils
from numpy import dot
import logging
import numpy as np
def run(P, images, alpha1, alpha2, gamma, sigma, rho, beta, numPatches, filename) :
p_num = 1
patchGenerated = 0
# While P is not empty
while len(P) > 0 :
# Pick and remove a patch p from P
p = P.pop(0)
# For each image cell Ci(x, y) containing p
c_num = 1
for cell in p.cells :
C = collectCells(cell, p, images, rho, alpha1)
ci_num = 1
for ci in C :
logging.info(f'PATCH : {p_num:02d}/{len(P):02d}')
logging.info(f'CELL : {c_num:02d}/{len(p.cells):02d}')
logging.info(f'Ci : {ci_num:02d}/{len(C):02d}')
ci_num += 1
# Create a new patch candidate pprime
pprime = reconstructPatch(p, ci)
Vp = p.VpStar
# Update V*(p')
VpStar = computeVpStar(Vp, pprime, alpha1, pprime.ref)
if len(VpStar) <= 1 :
logging.info("STATUS : FAILED")
logging.info("------------------------------------------------")
continue
# Refine c(p') and n(p')
new_pprime = refinePatch(pprime, VpStar, pprime.ref)
# Add visible images (a depth-map test) to V(p')
addImages(new_pprime, images, Vp, sigma)
# Update V*(p')
VpStar = computeVpStar(Vp, new_pprime, alpha2, pprime.ref)
# If |V*(p')| < gamma
if len(VpStar) < gamma :
# Go back to For loop (failure)pprime.ref
logging.info("STATUS : FAILED")
logging.info("------------------------------------------------")
continue
# Add p' to P
P.append(new_pprime)
# Add p' to corresponding Qj(x, y) and Q*j(x, y)
registerPatch(new_pprime, Vp, VpStar, beta)
patchGenerated += 1
logging.info("STATUS : SUCCESS")
logging.info("------------------------------------------------")
utils.savePatch(new_pprime, filename)
if patchGenerated >= numPatches and numPatches != -1:
return
c_num += 1
p_num += 1
for image in images :
for patch in image.allPatches() :
P.append(patch)
def collectCells(cell, patch, images, rho, alpha) :
id = cell[0]
x = cell[1][0]
y = cell[1][1]
image = utils.getImage(id, images)
C = []
c1 = image.cells[y-1][x]
c2 = image.cells[y+1][x]
c3 = image.cells[y][x-1]
c4 = image.cells[y][x+1]
if utils.identifyCell(c1, patch, rho) :
C.append(c1)
if utils.identifyCell(c2, patch, rho) :
C.append(c2)
if utils.identifyCell(c3, patch, rho) :
C.append(c3)
if utils.identifyCell(c4, patch, rho) :
C.append(c4)
return C
def reconstructPatch(p, ci) :
np = p.normal
ref = p.ref
cp = utils.computeCenter(p, ci)
pprime = Patch(cp, np, ref)
return pprime
def addImages(patch, images, Vp, sigma) :
for image in images :
if utils.hasImage(image, Vp) :
continue
else :
angle = (dot(patch.normal, (image.center - patch.center))) / (norm(image.center - patch.center))
if angle < cos(sigma * pi / 180) :
continue
else :
Vp.append(image)
def registerPatch(patch, Vp, VpStar, beta) :
for img in Vp :
pmat = img.pmat
pt = pmat @ patch.center
pt /= pt[2]
x = int(pt[0]/beta)
y = int(pt[1]/beta)
img.cells[y][x].q.append(patch)
isQStar = 0
if utils.getImage(img.id, VpStar) :
isQStar = 1
img.cells[y][x].qStar.append(patch)
patch.VpStar.append(img)
img.cells[y][x].hasRecon = True
cell = np.array([img.id, [x, y], isQStar, 1])
patch.cells.append(cell)
patch.Vp.append(img)