【opencv】图像处理之相似变换、仿射变换、透视变换 您所在的位置:网站首页 图像处理仿射变换 【opencv】图像处理之相似变换、仿射变换、透视变换

【opencv】图像处理之相似变换、仿射变换、透视变换

2024-02-15 23:05| 来源: 网络整理| 查看: 265

■ Similarity Transform(相似变换)

– Similarity Transform(相似变换) = Rotation(旋转) + Translation(平移) + Scale(放缩)

● 性质:Right angle is still right angle (保角性)

代码:

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()

原图和相似变换后的图像: 在这里插入图片描述

■ Affine Transform(仿射变换)

● 性质:Parallel lines are still parallel lines(不再具有保角性,具有保平行性) ● 三个非共线的点对(6 parameters)确定一个仿射变换。

代码:

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()

原图和相似变换后的图像: 在这里插入图片描述

■ Perspective Transform(透视/投影变换)

● 性质:Lines are still lines(不保角,不保平行,保直线性) ● 四个非共线的点对(8 parameters)确定一个透视变换。

代码:

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()

原图和透视变换后的图像: 在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有