def refinePatch(patch): # patch.center = np.array([-0.0093028, 0.0375282, -0.0426621, 1]) # patch.normal = np.array([0.400778, 0.210337, -0.891703, 0]) global m_center m_center = patch.center global m_ray m_ray = patch.center - ref.center m_ray /= norm(m_ray) p = encode(patch) min_angle = -23.99999 max_angle = 23.99999 lower_bounds = np.array([-inf, min_angle, min_angle]) upper_bounds = np.array([inf, max_angle, max_angle]) opt = nlopt.opt(LN_BOBYQA, 3) opt.set_lower_bounds(lower_bounds) opt.set_upper_bounds(upper_bounds) opt.set_maxeval(1000) opt.set_xtol_rel(1.e-7) opt.set_min_objective(myFunc) x = [] for i in range(3): x.append(max(min(p[i], upper_bounds[i]), lower_bounds[i])) res = opt.optimize(x) center, normal = decode(res) patch = Patch(center, normal, ref) patch.px, patch.py = utils.getPatchAxes(ref, patch) return patch
def my_f(x, grad): xs = np.array([x[0], x[1], x[2]]) ret = 0 coord, normal = decode(xs) temp = Patch(coord, normal, ref) temp.px, temp.py = utils.getPatchAxes(ref, temp) ans = 0 denom = 0 minimum = len(VpStar) grid1 = optim.projectGrid(temp, ref.pmat, ref) for image in VpStar: if image.id == ref.id: continue grid = optim.projectGrid(temp, image.pmat, image) if np.count_nonzero(grid) == 0: continue else: ncc = 1 - optim.ncc2(optim.computeGridValues(ref, grid1), optim.computeGridValues(image, grid)) ncc = ncc / (1 + 3 * ncc) ans += ncc denom += 1 if denom < minimum - 1: ret = 2 else: ret = ans / denom return ret
def computePatch(feat1, feat2, ref): cp = triangulation.myVersion(feat1, feat2, ref.pmat, feat2.image.pmat) normal = ref.center - cp normal /= norm(normal) # cp = np.array([-0.0093028, 0.0375282, -0.0426621, 1]) # normal = np.array([0.400778, 0.210337, -0.891703, 0]) patch = Patch(cp, normal, ref) patch.px, patch.py = utils.getPatchAxes(ref, patch) return patch
def computePatch(f, fprime, ref) : center = utils.triangulate(f, fprime, ref.pmat, fprime.image.pmat) normal = ref.center - center normal /= norm(normal) patch = Patch(center, normal, ref) return patch
def reconstructPatch(p, ci): np = p.normal ref = p.ref cp = utils.computeCenter(p, ci) pprime = Patch(cp, np, ref) return pprime
def loadPatches(images, filename): file = open(filename, 'r') lines = file.readlines() patches = [] for line in lines: words = line.split() center = np.empty(4) normal = np.empty(4) ref = getImage(int(words.pop(0)), images) for i in range(len(center)): center[i] = float(words.pop(0)) for i in range(len(normal)): normal[i] = float(words.pop(0)) patch = Patch(center, normal, ref) ids = [] while words: id = int(words.pop(0)) ids.append(id) x = int(words.pop(0)) y = int(words.pop(0)) isQStar = int(words.pop(0)) hasRecon = int(words.pop(0)) img = getImage(id, images) if isQStar == 1: img.cells[y][x].qStar.append(patch) img.cells[y][x].q.append(patch) if hasRecon == 1: img.cells[y][x].hasRecon = True cell = np.array([id, [x, y], isQStar, hasRecon]) patch.cells.append(cell) VpStar = [] while ids: VpStar.append(getImage(ids.pop(0), images)) patch.VpStar = VpStar patches.append(patch) file.close() return patches
def run(patch, i_ref, i_VpStar) : global ray global norm_ray global VpStar global ref global grid global val ref = i_ref VpStar = i_VpStar ray = patch.center - ref.center norm_ray = norm(ray) ray /= norm_ray grid = projectGrid(patch, ref) val = computeGrid(ref, grid) lower_bounds = np.array([-inf, 0, -360]) upper_bounds = np.array([inf, 180, 360]) params = encode(patch) center, normal = decode2(params[0], params[1], params[2]) opt = nlopt.opt(LN_NELDERMEAD, 3) opt.set_min_objective(myFunc) opt.set_maxeval(1000) opt.set_lower_bounds(lower_bounds) opt.set_upper_bounds(upper_bounds) opt.set_xtol_rel(1e-6) res = opt.optimize(params) center, normal = decode2(res[0], res[1], res[2]) patch = Patch(center, normal, ref) # for image in VpStar : # print(projectGrid(patch,image)) # input("") # exit() return patch
def myFunc(DoF, grad): center, normal = decode(DoF) patch = Patch(center, normal, g_ref) return computeGStar(patch)
def run(patch, ref, VpStar): global g_VpStar global g_ref global g_ray global g_center global g_ascale # center = np.array([ # 0.00994551, # 0.0377007, # -0.00483502, # 1 # ]) # normal = np.array([ # 0.23121, # 0.194608, # -0.953242, # 0 # ]) # ref = utils.getImage(0, VpStar) # patch = Patch(center, normal, ref) # print(patch.center) # print(patch.normal) g_ref = ref g_VpStar = VpStar g_ray = patch.center - ref.center g_ray /= norm(g_ray) g_center = patch.center g_ascale = pi / 48 p = encode(patch) # bestT = p[0] # bestA = p[1] # bestB = p[2] # center = np.array([ # 0.01007091, # 0.03777185, # -0.00515735, # 1 # ]) # normal = np.array([ # 0.0932068, # 0.27464589, # -0.95701731, # 0 # ]) # ref = utils.getImage(0, VpStar) # patch = Patch(center, normal, ref) # bestG = computeGStar(patch) # print(bestG) # center = np.array([ # 0.00999788, # 0.0377447, # -0.00505095, # 1 # ]) # normal = np.array([ # 0.38387, # -0.207272, # -0.899824, # 0 # ]) # ref = utils.getImage(0, VpStar) # patch = Patch(center, normal, ref) # bestG = computeGStar(patch) # print(bestG) # exit() # for i in range(-1, 1, 1) : # for j in range(-1, 1, 1) : # for k in range(-1, 1, 1) : # if i == 0 and j == 0 and k == 0 : # continue # tempT = p[0] + i * 2 # tempA = p[1] + j * pi/60 # tempB = p[2] + k * pi/60 # tempP = np.array([tempT, tempA, tempB]) # center, normal = decode(tempP) # patch = Patch(center, normal, g_ref) # gStar = computeGStar(patch) # if gStar < bestG : # bestG = gStar # bestA = tempA # bestB = tempB # bestT = tempT # bestP = np.array([bestT, bestA, bestB]) # center, normal = decode(bestP) # refinedPatch = Patch(center, normal, g_ref) lower_bounds = np.array([1e-12, -23.999, -23.999]) upper_bounds = np.array([inf, 23.999, 23.999]) opt = nlopt.opt(LN_BOBYQA, 3) opt.set_lower_bounds(lower_bounds) opt.set_upper_bounds(upper_bounds) opt.set_maxeval(1000) opt.set_xtol_rel(1e-7) opt.set_min_objective(myFunc) x = [] for i in range(3): x.append(max(min(p[i], upper_bounds[i]), lower_bounds[i])) res = opt.optimize(x) # res = np.array([-0.38853, -6.88612, -1.35397]) center, normal = decode(res) # print(res) # print(center) # print(normal) # exit() refinedPatch = Patch(center, normal, ref) return refinedPatch
def myFunc(DoF, grad): center, normal = decode(DoF) patch = Patch(center, normal, ref) patch.px, patch.py = getPatchAxes(ref, patch) return computeGStar(patch)