用python进行OpenCV进行图像旋转

 

1 旋转

1.1 旋转基本操作

旋转的概念正如我们平常听见的一样:将图片选装x度。我们先通过多少度来旋转图片,然后我们将写一个旋转函数。

import numpy as np #1
import argparse #2
import imutils #3
import cv2 #4

ap = argparse.ArgumentParser() #5
ap.add_argument("-i", "--image", required = True,
    help = "Path to the image") #6
args = vars(ap.parse_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

M = cv2.getRotationMatrix2D(center, 45, 1.0) #12
rotated = cv2.warpAffine(image, M, (w, h)) #13
cv2.imshow("Rotated by 45 Degrees", rotated) #14

M = cv2.getRotationMatrix2D(center, -90, 1.0) #15
rotated = cv2.warpAffine(image, M, (w, h)) #16
cv2.imshow("Rotated by -90 Degrees", rotated) #17

rotated = imutils.rotate(image, 180) #18
cv2.imshow("Rotated by 180 Degrees", rotated) #19
cv2.waitKey(0) #20
  •  

 

#10-11: 
在第10行中我们得到了图像的宽和高,然后我们通过”//”将它们除以2取整来得到旋转的中心。当然我们也可以不以中心为旋转中心,这里为了方便。

#12: 
正如我们定义一个矩阵来移动图像一样,我们还需要定义一个矩阵来旋转图像,然而不同的是我们不是通过NumPy来构造矩阵的,而是通过:

cv2.getRotationMatrix2D()
  • 1

第一个参数:表示向以哪一点进行旋转?这里就是图像的中心 
第二个参数:表示我们希望旋转的角度。这里为正45度,表示逆时针旋转45度 
第三个参数:表示图像旋转后的大小,这里设为1表示大小与原图大小一致

#13-14: 
通过cv2.warpAffine()方法,我们便可进行旋转图像的操作,第一个参数为原图,第二个参数为旋转矩阵,第三个参数为图像(宽,高)的元组,然后将旋转后的图像显示出来

#15-17: 
采用同样的方法将图像逆时针旋转90度,然后展示出来

#18-20: 
在第18行我们使用了:imutils这个自己写的库,然后调用了rotate()方法。第一个参数是需要操作的图像,第二个参数是要旋转的度数。

1.2 自写的函数库

在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 = cv2.getRotationMatrix2D(center, angle, scale) #5

    rotated = cv2.warpAffine(image, M, (w, h)) #6
    return rotated #7
  •  

#1-4: 
我们的旋转方法又四个参数,第一个是图像,第二个是我们所希望旋转的角度,我们还提供了两个可选择的变量:中心点和规模。中心点是我们希望我们的图像围绕哪一点旋转?如果,没有给它赋值,我们会默认将图像的中心点赋值给它。规模大小我们默认为1.0,表示没有任何大小的变化。

#5-7: 
通过构造我们的旋转矩阵,然后我们将旋转后的结果返回。

2 效果展示

这里写图片描述

效果不好 更新了一个新的代码 整个图片旋转 不会固定原大小

 

 

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 clockwise), then grab the sine and cosine
    # (i.e., the rotation components of the matrix)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    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()

 

  • 24
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
立体校正(Stereo Rectification)是将双目图像中的像素点转换成同一平面上的像素点,以便进行后续的立体匹配。使用Python-OpenCV可以很方便地进行立体校正。下面是一个简单的示例代码: ```python import cv2 # 读取左右两张图像 img_left = cv2.imread('left.png') img_right = cv2.imread('right.png') # 读取相机参数 K_left = np.loadtxt('K_left.txt') d_left = np.loadtxt('dist_left.txt') K_right = np.loadtxt('K_right.txt') d_right = np.loadtxt('dist_right.txt') R = np.loadtxt('R.txt') T = np.loadtxt('T.txt') # 计算校正映射 size = img_left.shape[:2][::-1] R1, R2, P1, P2, Q, validPixROI1, validPixROI2 = cv2.stereoRectify(K_left, d_left, K_right, d_right, size, R, T) # 计算校正映射表 map1_left, map2_left = cv2.initUndistortRectifyMap(K_left, d_left, R1, P1, size, cv2.CV_16SC2) map1_right, map2_right = cv2.initUndistortRectifyMap(K_right, d_right, R2, P2, size, cv2.CV_16SC2) # 应用校正映射表,进行立体校正 img_left_rect = cv2.remap(img_left, map1_left, map2_left, cv2.INTER_LINEAR) img_right_rect = cv2.remap(img_right, map1_right, map2_right, cv2.INTER_LINEAR) ``` 在上述代码中,我们首先读取了左右两张图像和相机参数,然后使用`cv2.stereoRectify()`函数计算校正映射。该函数的参数包括左右相机的内参矩阵、畸变系数、旋转矩阵和平移向量,以及图像大小。校正映射包括左右两张图像的旋转矩阵、投影矩阵和视差变换矩阵等信息,可以通过该函数的返回值获取。 接着,我们使用`cv2.initUndistortRectifyMap()`函数计算校正映射表。该函数的参数包括相机的内参矩阵、畸变系数、旋转矩阵、投影矩阵、图像大小和插值方法等。该函数的返回值是两个映射表,可以通过这两个映射表对图像进行校正。 最后,我们使用`cv2.remap()`函数对左右两张图像进行校正。该函数的参数包括原图像、映射表和插值方法等。校正后得到的图像可以用于后续的立体匹配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值