要在Python中使用pyopencv旋转图像,可以按照以下步骤进行操作:

  1. 导入必要的库和模块:
import cv2
import numpy as np
  1. 加载要旋转的图像:
img = cv2.imread('path/to/image.jpg')
  1. 定义旋转角度和旋转中心点:
angle = 45
center = (img.shape[1]//2, img.shape[0]//2)
  1. 定义旋转矩阵:
M = cv2.getRotationMatrix2D(center, angle, 1.0)
  1. 应用旋转矩阵到图像上:
rotated = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
  1. 显示旋转后的图像:
cv2.imshow('Rotated Image', rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()

完整的代码示例:

import cv2
import numpy as np

img = cv2.imread('path/to/image.jpg')
angle = 45
center = (img.shape[1]//2, img.shape[0]//2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
cv2.imshow('Rotated Image', rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()
pyopencv rotate使用方法

原文地址: http://www.cveoy.top/t/topic/b1fN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录