python-opencv-图像旋转

这种旋转方式,旋转后的图像,是原图的外接矩形框,四个角的部分,填充了黑色。


import cv2
import numpy as np

# 旋转angle角度,缺失背景白色(255, 255, 255)填充
def rotate_bound_white_bg(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)
    # -angle位置参数为角度参数负值表示顺时针旋转; 1.0位置参数scale是调整尺寸比例(图像缩放参数),建议0.75
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1)
    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
    # borderValue 缺失背景填充色彩,此处为白色,可自定义
    return cv2.warpAffine(image, M, (nW, nH), borderValue=(0, 0, 0))
    # borderValue 缺省,默认是黑色(0, 0 , 0)
    # return cv2.warpAffine(image, M, (nW, nH))

angle = 30
image = cv2.imread('G:/110w2/mask_tif4/0.png')
imgRotation = rotate_bound_white_bg(image, angle)
# 旋转后的图片显示
cv2.imshow("imgRotation",imgRotation)
# 旋转后的图片保存
cv2.imwrite('G:/110w2/mask_tif4/1_.png', imgRotation)
cv2.waitKey(0)


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
立体校正(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()`函数对左右两张图像进行校正。该函数的参数包括原图像、映射表和插值方法等。校正后得到的图像可以用于后续的立体匹配。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值