Functions for image warping and so on
%load_ext autoreload
%autoreload 2
Function overlay_common_area
warps the common area, based on the (planar) correspondences between two images, of image 1 into image 2 and replaces one of RGB channels with original image 2.
The warp is perspective warp, defined be the least-squares fit of the correspondences. If some of them are incorrect - bad luck :)
The function is handy to check if the correspondences define a good transformation between images.
%matplotlib inline
import matplotlib.pyplot as plt
import cv2
import numpy as np
img1_fname = 'sample_project/ministry/01.png'
img2_fname = 'sample_project/ministry/02.png'
c_fname = 'sample_project/ministry/corrs.txt'
img1 = cv2.cvtColor(cv2.imread(img1_fname), cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(cv2.imread(img2_fname), cv2.COLOR_BGR2RGB)
corrs = np.loadtxt(c_fname)
overlay = overlay_common_area(img1, img2, corrs[:,:2], corrs[:,2:])
plt.imshow(overlay)
%matplotlib inline
import matplotlib.pyplot as plt
import cv2
import numpy as np
img1_fname = 'sample_project/ministry/01.png'
img2_fname = 'sample_project/ministry/02.png'
c_fname = 'sample_project/ministry/corrs.txt'
img1 = cv2.cvtColor(cv2.imread(img1_fname), cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(cv2.imread(img2_fname), cv2.COLOR_BGR2RGB)
corrs = np.loadtxt(c_fname)
F, inliers, err = get_model(corrs[:,:2], corrs[:,2:], 'F')
fig, ax = plt.subplots(1,1)
draw_epipolar_lines(img1, img2, corrs[:,:2], corrs[:,2:], F, ax)
H, inliers, err = get_model(corrs[:,:2], corrs[:,2:], 'H')
fig, ax = plt.subplots(1,1, figsize=(8,4))
draw_homography(img1, img2, corrs[:,:2], corrs[:,2:], H, ax)
This function helps showing tilted version of the images for more convinient labeling
%matplotlib inline
import matplotlib.pyplot as plt
import cv2
import numpy as np
img1_fname = 'sample_project/ministry/01.png'
img1 = cv2.cvtColor(cv2.imread(img1_fname), cv2.COLOR_BGR2RGB)
img_tilted, H = tilt_image(img1, 30,0)
plt.imshow(img_tilted)