-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
executable file
·69 lines (53 loc) · 2.21 KB
/
example.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
from src.alproj.surface import create_db, crop
from src.alproj.project import sim_image, reverse_proj
from src.alproj.gcp import akaze_match, set_gcp
from src.alproj.optimize import CMAOptimizer
import sqlite3
import numpy as np
import rasterio
res = 5.0 # resolution in m
aerial = rasterio.open("devel_data/tateyama2.tif")
dsm = rasterio.open("devel_data/tateyamadem_small.tif")
out_path = "devel_data/pc.db"
#create_db(aerial, dsm, out_path, res)
# crop_surface
conn = sqlite3.connect("devel_data/pc.db")
params = {"x":732731,"y":4051171, "z":2458, "fov":70, "pan":100, "tilt":0, "roll":0,\
"a1":1, "a2":1, "k1":0.5, "k2":0.0, "k3":0, "k4":0, "k5":0.0, "k6":-0.2, \
"p1":0, "p2":0, "s1":0, "s2":0, "s3":0, "s4":0, \
"w":5616, "h":3744, "cx":5616/2, "cy":3744/2}
distance = 3000
chunksize = 1000000
vert, col, ind = crop(conn, params, distance, chunksize) # This takes some minites.
# make sim
import cv2
sim = sim_image(vert, col, ind, params)
cv2.imwrite("devel_data/test.png", sim)
df = reverse_proj(sim, vert, ind, params)
del(vert, col, ind) # Release memories
# Setting GCPs
path_org = "devel_data/ttym_2016.jpg"
path_sim = "devel_data/test.png"
match, plot = akaze_match(path_org, path_sim, ransac_th=200, plot_result=True)
cv2.imwrite("devel_data/matched.png", plot)
gcps = set_gcp(match, df)
del(df, sim)
# Optimize camera parameters
obj_points = gcps[["x","y","z"]]
img_points = gcps[["u","v"]]
params_init = params
target_params = ["fov", "pan", "tilt", "roll", "a1", "a2", "k1", "k2", "k3", "k4", "k5", "k6", "p1", "p2", "s1", "s2", "s3", "s4"]
cma_optimizer = CMAOptimizer(obj_points, img_points, params_init)
cma_optimizer.set_target(target_params)
params_optim, error = cma_optimizer.optimize(generation = 300, bounds = None, sigma = 1.0, population_size=50)
params_optim["error"] = error
import json
with open('devel_data/params_optim.json', 'w') as f:
json.dump(params_optim, f, indent=4)
# Use optimized parameters
vert, col, ind = crop(conn, params_optim, 3000, 1000000)
sim2 = sim_image(vert, col, ind, params_optim)
cv2.imwrite("devel_data/optimized.png", sim2)
# Reverse projection
original = cv2.imread("devel_data/ttym_2016.jpg")
georectified = reverse_proj(original, vert, ind, params_optim)