当前位置:网站首页>OpenCV 图片旋转
OpenCV 图片旋转
2022-07-24 17:30:00 【wu_zhiyuan】
OpenCV 图片旋转的两个函数
- cv2.getRotationMatrix2D 获得仿射变化矩阵
- cv2.warpAffine 进行仿射变化
getRotationMatrix2D 获得仿射变化矩阵(warpAffine 方法的重要参数)
rot_mat = cv2.getRotationMatrix2D(center, -5, 1)
参数说明:
- center 表示中间点的位置,
- -5 表示逆时针旋转5度,
- 1 表示进行等比列的缩放
返回值:
rot_mat 仿射变化矩阵
例如:
warpAffine 进行仿射变化
img_rotated_by_alpha = cv2.warpAffine(img, rot_mat, (img.shape[1], img.shape[0]))
参数说明:
- img 表示输入的图片,
- rot_mat 表示仿射变化矩阵,
- (image.shape[1], image.shape[0]) 表示变换后的图片大小,image.shape[1]表示宽,image.shape[0]表示高
返回值:
旋转后的图像
代码说明:
第一步:读入图片,进行图片展示
第二步:获取图片的宽、长、通道数,构造[0, 0, w, h]列表,然后用该列表实列化一个bbox对象(用于计算中心点)
第三步:计算其center值,将其代入到cv2.getRotationMatrix2D生成仿射变化矩阵
第四步:使用cv2.warpAffine(img, rot_mat, (img.shape[1], img.shape[0])) 获得仿射变化以后的图像
第五步:如果计算源图像上的某点,位于转换后图像的位置。如果是源图片上的一个点,那么经过变化以后的坐标值为
(rot_mat[0][0] * x + rot_mat[0][1] * y + rot_mat[0][2], rot_mat[1][0] * x + rot_mat[1][1] * y + rot_mat[1][2]
第六步:进行旋转图片展示,为了保证,这里也可以进行截图操作
import cv2
import numpy as np
class BBox(object):
def __init__(self, bbox):
self.left = bbox[0]
self.top = bbox[1]
self.right = bbox[2]
self.bottom = bbox[3]
img = cv2.imread('0001.jpg')
cv2.imshow('img', img) # 显示图像
cv2.waitKey(0)
h, w, c = img.shape # 图像的高,宽,通道;注意:OpenCV 先获得高!然后是宽和通道。
box = [0, 0, w, h] # 创建一个列表,共4项,前两项表示原点,第三项为宽,第四项为高
bbox = BBox(box) # 创建BBox对象
center = ((bbox.left + bbox.right) / 2, (bbox.top + bbox.bottom) / 2) # 计算中心点
rot_mat = cv2.getRotationMatrix2D(center, -5, 1) # 仿射变化矩阵
img_rotated_by_alpha = cv2.warpAffine(img, rot_mat, (img.shape[1], img.shape[0]))
# rot_mat 仿射变化矩阵,warpAffine的重要参数
# img.shape[1]为图像宽度,img.shape[0]为图像高度 img为OpenCV变量
# 获得图片旋转以后的关键点的位置
# lanmark_ = np.asarray([(rot_mat[0][0] * x + rot_mat[0][1] * y + rot_mat[0][2],
# rot_mat[1][0] * x + rot_mat[1][1] + rot_mat[1][2]) for (x, y) in landmark])
cv2.imshow('img_rotated', img_rotated_by_alpha) # 显示旋转后的图像
cv2.waitKey(0)
# face = img_rotated_by_alpha[bbox.top:bbox.bottom + 1, bbox.left:bbox.right + 1]
#
# cv2.imshow('face', face)
# cv2.waitKey(0)
# 注意 该代码在Jupyter Notebook运行时有bug

原始图像
旋转后图像
关于img.shape[0]、[1]、[2]到底代表什么
img.shape[0]:图像的垂直尺寸(高度)
img.shape[1]:图像的水平尺寸(宽度)
img.shape[2]:图像的通道数
在矩阵中,[0]就表示行数,[1]则表示列数。
边栏推荐
- The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
- Still developing games with unity? Then you're out. Try unity to build an answer system
- Hcip fourth day notes
- Is computer monitoring true? Four experiments to find out
- NPM install reported -4058 error
- Canvas from getting started to persuading friends to give up (graphic version)
- Df2net 3D model deployment
- Are the top ten securities companies safe and risky to open accounts?
- List of stringutils and string methods
- EF core data filtering
猜你喜欢

Check the actual data growth of the database

Bring 120W goods in 15 seconds. You can also shoot such a popular video

Exception handling - a small case that takes you to solve NullPointerException

C # print reports using fastreport.net

Still using xshell? You are out, recommend a more modern terminal connection tool!

别再到处乱放配置文件了!试试我司使用 7 年的这套解决方案,稳的一秕

Rare earth Developer Conference | Apache pulsar committee Liu Dezhi shares the way of cloud native technology transformation
Shardingsphere database read / write separation

Image information is displayed by browser: data:image/png; Base64, + image content

通道的分离与合并
随机推荐
2022 Yangtze River Delta industrial automation exhibition will be held in Nanjing International Exhibition Center in October
Colleges and universities have introduced 23 Philippine doctors with heavy funds, and the relevant departments have launched an investigation!
别再到处乱放配置文件了!试试我司使用 7 年的这套解决方案,稳的一秕
Coldplay weekly issue 10
Array double pointer - deliberate practice
Practical application cases of digital Twins - Smart Park
Yolopose practice: one-stage human posture estimation with hands + code interpretation
Is computer monitoring true? Four experiments to find out
Method of querying comma separated strings in a field by MySQL
Safety: how to provide more protection for pedestrians
Mobile robot (IV) four axis aircraft
什么是模糊理论,基础,流程
使用matplotlib模拟线性回归
HCNP Routing&Switching之DHCP中继
High performance complexity analysis of wechat circle of friends
Eth POS 2.0 stacking test network pledge process
实习报告1——人脸三维重建方法
Logical operation of image pixels
Trends of semiconductor industry
[matlab]: basic knowledge learning