-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathimage_registration.py
69 lines (52 loc) · 1.83 KB
/
image_registration.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
# Created by Qixun Qu
# 2017/04/10
#
import numpy as np
from affine_ransac import Ransac
from align_transform import Align
from affine_transform import Affine
# Affine Transform
# |x'| = |a, b| * |x| + |tx|
# |y'| |c, d| |y| |ty|
# pts_t = A * pts_s + t
# -------------------------------------------------------------
# Test Class Affine
# -------------------------------------------------------------
# Create instance
af = Affine()
# Generate a test case as validation with
# a rate of outliers
outlier_rate = 0.9
A_true, t_true, pts_s, pts_t = af.create_test_case(outlier_rate)
# At least 3 corresponding points to
# estimate affine transformation
K = 3
# Randomly select 3 pairs of points to do estimation
idx = np.random.randint(0, pts_s.shape[1], (K, 1))
A_test, t_test = af.estimate_affine(pts_s[:, idx], pts_t[:, idx])
# Display known parameters with estimations
# They should be same when outlier_rate equals to 0,
# otherwise, they are totally different in some cases
print(A_true, '\n', t_true)
print(A_test, '\n', t_test)
# -------------------------------------------------------------
# Test Class Ransac
# -------------------------------------------------------------
# Create instance
rs = Ransac(K=3, threshold=1)
residual = rs.residual_lengths(A_test, t_test, pts_s, pts_t)
# Run RANSAC to estimate affine tansformation when
# too many outliers in points set
A_rsc, t_rsc, inliers = rs.ransac_fit(pts_s, pts_t)
print(A_rsc, '\n', t_rsc)
# -------------------------------------------------------------
# Test Class Align
# -------------------------------------------------------------
# Load source image and target image
source_path = 'Images/mona_source.png'
target_path = 'Images/mona_target.jpg'
# Create instance
al = Align(source_path, target_path, threshold=1)
# Image transformation
al.align_image()