当前位置:网站首页>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 .
边栏推荐
- portmap 端口转发
- pinia 入门及使用
- 数论整除分块讲解 例题:2021陕西省赛C
- Can CSC open an account for domestic futures? Is it safe?
- How to remove the top picture of the bubble skin article details of solo blog
- NPM install reported -4058 error
- HCNP Routing&Switching之DHCP中继
- opencv自带颜色操作
- DF2NET三维模型部署
- The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
猜你喜欢

The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced

Kyligence attended the Huawei global smart finance summit to accelerate the expansion of the global market

A problem of MySQL database

Can Lu Zhengyao hide from the live broadcast room dominated by Luo min?

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

Transformer structure analysis -- learning notes

Tensorflow introductory tutorial (40) -- acunet

It's time to consider slimming down your app

Keyboard input operation

近30所高校,获教育部点名表扬!
随机推荐
微信朋友圈的高性能复杂度分析
JS & TS learning summary
awk从入门到入土(19)awk扩展插件,让awk如虎添翼
二维卷积——torch.nn.conv2d的使用
How the computer accesses the Internet (IV) LAN and server response
Logical operation of image pixels
Canvas from getting started to persuading friends to give up (graphic version)
UFW port forwarding
Can Lu Zhengyao hide from the live broadcast room dominated by Luo min?
[spoken English] 01 - Introduction to atom
2022 Asia International Internet of things exhibition
Tensorflow introductory tutorial (37) -- DC Vnet
[matlab]: basic knowledge learning
Tensorflow introductory tutorial (40) -- acunet
Array double pointer - deliberate practice
Safety: how to provide more protection for pedestrians
es(1)
地表最强程序员装备“三件套”,你知道是什么吗?
Getaverse,走向Web3的远方桥梁
Transformer structure analysis -- learning notes