python画图旋转图形_OpenCV Python将图像围绕特定点旋转X度

1586010002-jmsa.png

I'm having a hard time finding examples for rotating an image around a specific point by a specific (often very small) angle in Python using OpenCV.

This is what I have so far, but it produces a very strange resulting image, but it is rotated somewhat:

def rotateImage( image, angle ):

if image != None:

dst_image = cv.CloneImage( image )

rotate_around = (0,0)

transl = cv.CreateMat(2, 3, cv.CV_32FC1 )

matrix = cv.GetRotationMatrix2D( rotate_around, angle, 1.0, transl )

cv.GetQuadrangleSubPix( image, dst_image, transl )

cv.GetRectSubPix( dst_image, image, rotate_around )

return dst_image

解决方案import numpy as np

def rotate_image(image, angle):

image_center = tuple(np.array(image.shape[1::-1]) / 2)

rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)

result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)

return result

Assuming you're using the cv2 version, that code finds the center of the image you want to rotate, calculates the transformation matrix and applies to the image.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值