用python进行OpenCV进行图像旋转

更新时间:2023-06-17 13:14:40 阅读: 评论:0

⽤python进⾏OpenCV进⾏图像旋转
1 旋转
1.1 旋转基本操作
旋转的概念正如我们平常听见的⼀样:将图⽚选装x度。我们先通过多少度来旋转图⽚,然后我们将写⼀个旋转函数。
import numpy as np #1
import argpar #2
import imutils #3
import cv2 #4
ap = argpar.ArgumentParr() #5
ap.add_argument("-i", "--image", required = True,
help = "Path to the image") #6
args = vars(ap.par_args()) #7
image = cv2.imread(args["image"]) #8
cv2.imshow("Original", image) #9
(h, w) = image.shape[:2] #10
center = (w // 2, h // 2) #11
how to dealM = RotationMatrix2D(center, 45, 1.0) #12
rotated = cv2.warpAffine(image, M, (w, h)) #13
cv2.imshow("Rotated by 45 Degrees", rotated) #14
M = RotationMatrix2D(center, -90, 1.0) #15
rotated = cv2.warpAffine(image, M, (w, h)) #16
cv2.imshow("Rotated by -90 Degrees", rotated) #17
citationrotated = ate(image, 180) #18
ubiquitous
cv2.imshow("Rotated by 180 Degrees", rotated) #19
cv2.waitKey(0) #20
#10-11:
在第10⾏中我们得到了图像的宽和⾼,然后我们通过”//”将它们除以2取整来得到旋转的中⼼。当然我们也可以不以中⼼为旋转中⼼,这⾥为了⽅便。
#12:
正如我们定义⼀个矩阵来移动图像⼀样,我们还需要定义⼀个矩阵来旋转图像,然⽽不同的是我们不是通过NumPy来构造矩阵的,⽽是通过:
1
第⼀个参数:表⽰向以哪⼀点进⾏旋转?这⾥就是图像的中⼼
第⼆个参数:表⽰我们希望旋转的⾓度。这⾥为正45度,表⽰逆时针旋转45度自主招生网上报名
第三个参数:表⽰图像旋转后的⼤⼩,这⾥设为1表⽰⼤⼩与原图⼤⼩⼀致
#13-14:
通过cv2.warpAffine()⽅法,我们便可进⾏旋转图像的操作,第⼀个参数为原图,第⼆个参数为旋转矩阵,第三个参数为图像(宽,⾼)的元组,然后将旋转后的图像显⽰出来
#15-17:
采⽤同样的⽅法将图像逆时针旋转90度,然后展⽰出来艾美奖2012
#18-20:
在第18⾏我们使⽤了:imutils这个⾃⼰写的库,然后调⽤了rotate()⽅法。第⼀个参数是需要操作的图像,第⼆个参数是要旋转的度数。
1.2 ⾃写的函数库21st century
在imutils.py中我们⾃定义rotate函数
def rotate(image, angle, center=None, scale=1.0): #1
(h, w) = image.shape[:2] #2
if center is None: #3
center = (w // 2, h // 2) #4
如何练习英语听力M = RotationMatrix2D(center, angle, scale) #5
rotated = cv2.warpAffine(image, M, (w, h)) #6
return rotated #7
#1-4:
我们的旋转⽅法⼜四个参数,第⼀个是图像,第⼆个是我们所希望旋转的⾓度,我们还提供了两个可选择的变量:中⼼点和规模。中⼼点是我们希望我们的图像围绕哪⼀点旋转?如果,没有给它赋值,我们会默认将图像的中⼼点赋值给它。规模⼤⼩我们默认为1.0,表⽰没有任何⼤⼩的变化。
#5-7:
通过构造我们的旋转矩阵,然后我们将旋转后的结果返回。
2 效果展⽰
maroon
效果不好更新了⼀个新的代码整个图⽚旋转不会固定原⼤⼩
import cv2
import numpy as np
def rotate_bound(image, angle):
# grab the dimensions of the image and then determine the    # center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwi), then grab the sine and cosine    # (i.e., the rotation components of the matrix)
M = RotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
park是什么意思sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation    M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
永不变心return cv2.warpAffine(image, M, (nW, nH))
image=cv2.imread('./img/fin.png')
angle=90
imag=rotate_bound(image,angle)
cv2.imshow('ww',imag)
cv2.waitKey()

本文发布于:2023-06-17 13:14:40,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/975794.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:旋转   图像   矩阵   参数   选择   定义
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图