(Opencv) Similarity transformation, affine transformation, perspective transformation of image processing

■ Similarity Transform (similar transformation)

Similarity Transform (similar transformation) = Rotation (rotation) + Translation (translation) + Scale (zoom)

● Nature: Right angle is still right angle (conservative)

Code:

import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('lenna.jpg', 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# print(img.shape)

# 得到相似变换的矩阵  # center:旋转中心 angle:旋转角度   scale:缩放比例
M = cv2.getRotationMatrix2D(center = (img.shape[0]/2,img.shape[1]/2),
                              angle = 30,
                              scale = 0.5) 

# 原图像按照相似矩阵进行相似变换  三个参数:原图像,相似矩阵,画布面积
img_rotate = cv2.warpAffine(img, M, (img.shape[0], img.shape[1]))

plt.figure(figsize=(8,8))
plt.subplot(1,2,1)
plt.imshow(img)
plt.subplot(1,2,2)
plt.imshow(img_rotate)
plt.show()

Original image and similarly transformed image:
Insert picture description here

■ Affine Transform (affine transformation)

● Nature: Parallel lines are still parallel lines (no longer conformal, with parallelism)
● Three non-collinear point pairs (6 parameters) determine an affine transformation.

Code:

import cv2
import matplotlib.pyplot as plt
import numpy as np

# 3 Src(原始) Points + 3 Dst(目标) Points
# cols:列/长  rows:行/宽

img = cv2.imread('lenna.jpg', 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# print(img.shape)

cols = img.shape[0]
rows = img.shape[1]

pt1 = np.float32([[0,0], [cols, 0], [0, rows]])
pt2 = np.float32([[cols*0.3, rows*0.3], [cols*0.8, rows*0.2], [cols*0.1, rows*0.9]])

# [[0,0], [cols, 0], [0, rows]] --> [[cols*0.3, rows*0.3], [cols*0.8, rows*0.2], [cols*0.1, rows*0.9]]
M = cv2.getAffineTransform(pt1, pt2)       # 仿射变换矩阵
dst = cv2.warpAffine(img, M, (cols, rows))

plt.figure(figsize=(8,8))
plt.subplot(1,2,1)
plt.imshow(img)
plt.subplot(1,2,2)
plt.imshow(dst)
plt.show()

Original image and similarly transformed image:
Insert picture description here

■ Perspective Transform (perspective/projection transformation)

● Nature: Lines are still lines (not conformal, not parallel, and straight)
● Four non-collinear point pairs (8 parameters) determine a perspective transformation.

Code:

import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('lenna.jpg', 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

width = img.shape[1]
height = img.shape[0]

pts1 = np.float32([[0,0], [width,0], [0,height], [width,height]])
pts2 = np.float32([[width*0.1,height*0.1], [width*0.9, width*0.1], [height*0.2,height*0.8], [width*0.7,height*0.7]])

M_warp = cv2.getPerspectiveTransform(pts1, pts2)     # 单应性矩阵
img_warp = cv2.warpPerspective(img, M_warp, (width, height))

plt.figure(figsize=(8,8))
plt.subplot(1,2,1)
plt.imshow(img)
plt.subplot(1,2,2)
plt.imshow(img_warp)
plt.show()

Original image and perspective transformed image:
Insert picture description here

Guess you like

Origin blog.csdn.net/Roaddd/article/details/112365634