-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrectify.py
More file actions
58 lines (43 loc) · 1.46 KB
/
rectify.py
File metadata and controls
58 lines (43 loc) · 1.46 KB
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
#!/anaconda/bin/python
import matplotlib.image as mplimg
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
from utils import *
# ---------- Settings ---------
nLinePairs = 2; # Select this may pairs of perpendicular lines
# -----------------------------
def usage(sname):
print(sname + " <image>");
if len(sys.argv) != 2:
usage(sys.argv[0]);
exit(0);
# print(f"Two-stage (affine and metric) rectification using {d} parallel line pairs {nLinePairs}");
# Read input files
imgPath = sys.argv[1];
fileparts = os.path.splitext(imgPath);
if fileparts[1] == '':
fileparts = (fileparts[0], '.png');
imgPath = fileparts[0] + fileparts[1];
im = mplimg.imread(imgPath);
# Do rectification in two stages
imA, HA = rectifyAffineF(im, nLinePairs)
plt.close('all')
imM, HM = rectifyMetricF(imA, nLinePairs)
# Translate and scale HM * HA to the image
H = translateAndScaleHToImage(np.dot(HM,HA), imA.shape)
# Apply translated and scaled version of HM * HA
imM = myApplyH(im, H) # Chain the two transforms
plt.close('all')
# Show result
imshow(np.concatenate((im,imA,imM),axis=1));
axis('off')
plt.suptitle('Original (left), Affine rectified (middle), Metric rectification (right)')
margin = 0.05;
plt.subplots_adjust(left=margin, right=1.0-margin, top=1.0-margin, bottom=margin)
show()
# Save output
imgPathRect = fileparts[0] + "_rect" + fileparts[1];
# print(f"Saving output to {s}... {imgPathRect}");
mplimg.imsave(imgPathRect,cropOuterRegion(imM));