当前位置:网站首页>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 .
边栏推荐
- socat 端口转发
- EF core data filtering
- 2022 ranking list of database audit products - must see!
- Shardingsphere database read / write separation
- [waiting for insurance] what does waiting for insurance rectification mean? What are the rectification contents?
- How to remove the top picture of the bubble skin article details of solo blog
- Open source Invoicing system, 10 minutes to complete, it is recommended to collect!
- Demonstration experiment of scrollbar for adjusting image brightness
- NC port forwarding
- Safety: how to provide more protection for pedestrians
猜你喜欢

获取同程(艺龙)酒店数据

DF2NET三维模型部署

Yolopose practice: one-stage human posture estimation with hands + code interpretation

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

Tensorflow introductory tutorial (38) -- V2 net

CDN (content delivery network) content distribution network from entry to practice

C语言实现静态版本的通讯录
![[wechat official account H5] authorization](/img/d1/2712f87e134c0b8b8fdeaab9e30492.png)
[wechat official account H5] authorization
![[matlab]: basic knowledge learning](/img/8c/d13597e402c55df6cbd5e008aef0a5.png)
[matlab]: basic knowledge learning

Opencv has its own color operation
随机推荐
通道的分离与合并
使用matplotlib模拟线性回归
Is Shenwan Hongyuan securities' low commission account reliable, reliable and safe
Trends of semiconductor industry
地表最强程序员装备“三件套”,你知道是什么吗?
Wallys/3 × 3/2 × 2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz/QCN9074
Transformer structure analysis -- learning notes
C语言中的字符与字符串库函数的使用以及模拟实现
Socat port forwarding
ROC and AUC details of the recommended system
Df2net 3D model deployment
The orders in the same city are delivered in the same city, and the order explosion is still handy!
数论整除分块讲解 例题:2021陕西省赛C
ansible自动化运维详解(五)ansible中变量的设定使用、JINJA2模板的使用以及ansible的加密控制
Array double pointer - deliberate practice
ShardingSphere数据库读写分离
Canvas from getting started to persuading friends to give up (graphic version)
List of stringutils and string methods
TCP协议调试工具TcpEngine V1.3.0使用教程
The latest Zhejiang construction safety officer simulation question bank and answers in 2022