当前位置:网站首页>Opencv picture rotation
Opencv picture rotation
2022-07-24 17:38:00 【wu_ zhiyuan】
OpenCV Two functions of image rotation
- cv2.getRotationMatrix2D Obtain affine change matrix
- cv2.warpAffine Make affine changes
getRotationMatrix2D Obtain affine change matrix (warpAffine Important parameters of method )
rot_mat = cv2.getRotationMatrix2D(center, -5, 1)
Parameter description :
- center Express The position of the middle point ,
- -5 Express Anti-clockwise rotate 5 degree ,
- 1 It indicates that The zoom
Return value :
rot_mat Affine change matrix
for example :
warpAffine Make affine changes
img_rotated_by_alpha = cv2.warpAffine(img, rot_mat, (img.shape[1], img.shape[0]))
Parameter description :
- img Represents the input image ,
- rot_mat Represents an affine change matrix ,
- (image.shape[1], image.shape[0]) Represents the size of the transformed picture ,image.shape[1] Indicates width ,image.shape[0] High
Return value :
Rotated image
Code instructions :
First step : Read in the picture , Show pictures
The second step : Get the width of the picture 、 Long 、 The channel number , structure [0, 0, w, h] list , Then use this list to materialize a bbox object ( Used to calculate the center point )
The third step : Calculating the center value , Substitute it into cv2.getRotationMatrix2D Generate affine change matrix
Step four : Use cv2.warpAffine(img, rot_mat, (img.shape[1], img.shape[0])) Get the image after affine change
Step five : If you calculate a point on the source image , In the position of the converted image . If it is a point on the source image , Then the coordinate value after the change is
(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]
Step six : Show rotating pictures , In order to ensure , You can also take screenshots here
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) # Display images
cv2.waitKey(0)
h, w, c = img.shape # The height of the image , wide , passageway ; Be careful :OpenCV Get high first ! Then there is kuanhe channel .
box = [0, 0, w, h] # Create a list , common 4 term , The first two terms represent the origin , The third item is width , The fourth item is high
bbox = BBox(box) # establish BBox object
center = ((bbox.left + bbox.right) / 2, (bbox.top + bbox.bottom) / 2) # Calculate the center point
rot_mat = cv2.getRotationMatrix2D(center, -5, 1) # Affine change matrix
img_rotated_by_alpha = cv2.warpAffine(img, rot_mat, (img.shape[1], img.shape[0]))
# rot_mat Affine change matrix ,warpAffine An important parameter of
# img.shape[1] Is the image width ,img.shape[0] For image height img by OpenCV Variable
# Get the position of key points after image rotation
# 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) # Displays the rotated image
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)
# Be careful The code is in Jupyter Notebook There are bug

original image 
Rotate the image
About img.shape[0]、[1]、[2] What does it stand for
img.shape[0]: The vertical size of the image ( Height )
img.shape[1]: The horizontal size of the image ( Width )
img.shape[2]: The number of channels in the image
In the matrix ,[0] It means the number of rows ,[1] Indicates the number of columns .
边栏推荐
- Getaverse,走向Web3的远方桥梁
- 2022 Asia International Internet of things exhibition
- 安全:如何为行人提供更多保护
- List of stringutils and string methods
- Shardingsphere database read / write separation
- TCP协议调试工具TcpEngine V1.3.0使用教程
- In the morning, Tencent took out 38K, which let me see the ceiling of the foundation
- Image information is displayed by browser: data:image/png; Base64, + image content
- Analog electricity - what is the resistance?
- 【网络安全】网站中间件存在的解析漏洞
猜你喜欢

C语言编程训练题目:左旋字符串中的k个字符、小乐乐与欧几里得、打印箭型图案、公务员面试、杨树矩阵
![[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~](/img/10/996d594a53d9a34a36079fed829f27.png)
[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~

C语言自定义类型讲解 — 结构体

分家后印象笔记过日子依然不好过,骚操作却不少

opencv自带颜色操作

C语言自定义类型讲解 — 联合体

Stop littering configuration files everywhere! Try our 7-year-old solution, which is stable

Getaverse,走向Web3的远方桥梁

OpenCV 图片旋转

快速完成intelij idea的单元测试JUnit4设置
随机推荐
NPM install reported -4058 error
How to remove the top picture of the bubble skin article details of solo blog
Tensorflow introductory tutorial (37) -- DC Vnet
Coldplay weekly issue 10
使用matplotlib模拟线性回归
Link editing tips of solo blog posts illegal links
2022年最新浙江建筑安全员模拟题库及答案
The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
In the morning, Tencent took out 38K, which let me see the ceiling of the foundation
[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~
Socat port forwarding
JS & TS learning summary
Eth POS 2.0 stacking test network pledge process
C语言实现静态版本的通讯录
C语言中的字符与字符串库函数的使用以及模拟实现
2022-07-21 Daily: Wu Enda wrote: how to establish projects suitable for AI career
Iqiyi Tiktok reconciled, Weibo lying gun?
Six ways for JS to implement inheritance
Iftnews | Christie's launched its venture capital department, aiming at Web3 and metauniverse industries
Atcoder Beginner 202 E - Count Descendants(离线查询 重链剖分树上启发式合并)