imgproc

Functions for image warping and so on
/opt/homebrew/Caskroom/miniforge/base/envs/python39/lib/python3.9/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)

source

overlay_common_area

def overlay_common_area(
    img1, img2, pts1, pts2
):

Call self as a function.


source

get_bbox

def get_bbox(
    pts_
):

Call self as a function.

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.

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)
/opt/homebrew/Caskroom/miniforge/base/envs/python39/lib/python3.9/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)


source

draw_homography

def draw_homography(
    img1, img2, pts1_, pts2_, H, ax
):

Call self as a function.


source

get_model

def get_model(
    pts1_, pts2_, model_type:str='F', leave_idx:NoneType=None
):

Call self as a function.


source

clip_corrs

def clip_corrs(
    pts1, pts2
):

Crops out pts1 and pts2 to the smallest common length


source

draw_epipolar_lines

def draw_epipolar_lines(
    img1, img2, pts1_, pts2_, Fm, ax
):

Call self as a function.

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)
/opt/homebrew/Caskroom/miniforge/base/envs/python39/lib/python3.9/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)

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)
/opt/homebrew/Caskroom/miniforge/base/envs/python39/lib/python3.9/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)


source

tilt_image

def tilt_image(
    img, angle_vert_deg, angle_hor_deg
):

Call self as a function.


source

rectify_plane

def rectify_plane(
    img, angle_vert_deg, angle_hor_deg
):

Call self as a function.

This function helps showing tilted version of the images for more convinient labeling

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, new_w, new_h = tilt_image(img1, 30,0)
plt.imshow(img_tilted)
/opt/homebrew/Caskroom/miniforge/base/envs/python39/lib/python3.9/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)
<ipython-input-88-6830b16fa8e8>:42: DeprecationWarning: an integer is required (got type numpy.float32).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
  img_out = cv2.warpPerspective(img, H,  (new_corners[:,0].max(),
<ipython-input-88-6830b16fa8e8>:42: DeprecationWarning: an integer is required (got type numpy.float32).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
  img_out = cv2.warpPerspective(img, H,  (new_corners[:,0].max(),