图像可以各种角度旋转(90、180、270和360)。 OpenCV计算执行仿射变换的仿射矩阵, 这意味着尽管保留了直线上的点之间的距离之比, 但它不会保留线之间的角度或点之间的距离。
旋转图像的语法如下:
cv2.getRotationMatrix2D(center, angle, scale rotated = cv2.warpAfifne(img, M, (w, h))
参数:
- 中心:它代表图像的中心。
- 角度:它表示特定图像沿逆时针方向旋转的角度。
- 旋转:保存旋转图像数据的ndarray。
- 比例尺:值1.0表示保留了形状。根据提供的值缩放图像。
import cv2# read image as greyscaleimg = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg')# get image height, width(h, w) = img.shape[:2]# calculate the center of the imagecenter = (w / 2, h / 2)angle90 = 90angle180 = 180angle270 = 270scale = 1.0# Perform the counterclockwise rotation holding at the center# 90 degreesM = cv2.getRotationMatrix2D(center, angle90, scale)rotated90 = cv2.warpAffine(img, M, (h, w))# 180 degreesM = cv2.getRotationMatrix2D(center, angle180, scale)rotated180 = cv2.warpAffine(img, M, (w, h))# 270 degreesM = cv2.getRotationMatrix2D(center, angle270, scale)rotated270 = cv2.warpAffine(img, M, (h, w))cv2.imshow('Original Image', img)cv2.waitKey(0)# waits until a key is pressedcv2.destroyAllWindows()# destroys the window showing imagecv2.imshow('Image rotated by 90 degrees', rotated90)cv2.waitKey(0)# waits until a key is pressedcv2.destroyAllWindows()# destroys the window showing imagcv2.imshow('Image rotated by 180 degrees', rotated180)cv2.waitKey(0)# waits until a key is pressedcv2.destroyAllWindows()# destroys the window showing imagecv2.imshow('Image rotated by 270 degrees', rotated270)cv2.waitKey(0)# waits until a key is pressedcv2.destroyAllWindows()# destroys the window showing image
【OpenCV图像旋转】输出
文章图片
推荐阅读
- OpenCV图像侵蚀和膨胀
- OpenCV图像过滤器
- OpenCV图像基本操作
- OpenCV图像轮廓
- OpenCV模糊(图像平滑)
- OpenCV Canny边缘检测
- OpenCV教程 | Python使用OpenCV
- OpenCV Blob检测
- 使用OpenCV进行人脸识别和人脸检测