centerx python_在图像[OpenCV/Python]中查找[x,y]旋转坐标位置

该博客探讨了在使用OpenCV的cv2.getRotationMatrix2D和cv2.warpAffine函数旋转图像时,如何正确计算旋转后图像中像素坐标的问题。作者注意到在旋转过程中坐标转换存在误差,并提供了rotate_bound和rotated_coord两个函数的代码示例。问题在于旋转后的坐标不匹配预期值,尤其是在图像边缘附近。作者寻求解决方案来找到旋转图像中像素的准确位置。
摘要由CSDN通过智能技术生成

我想按顺序在多个角度旋转图像。我这样做使用cv2.getRotationMatrix2D和cv2.warpAffine。有一个对像素坐标[x,y],其中x = cols,y =行(在这种情况下)我想在旋转的图像中找到它们的新坐标。在图像[OpenCV/Python]中查找[x,y]旋转坐标位置

问题是我的映射或我的旋转是错误的,因为转换后的计算坐标是错误的。 (我试图手动计算的角,简单验证)

CODE:

def rotate_bound(image, angle):

# grab the dimensions of the image and then determine the

# center

(h, w) = image.shape[:2]

(cX, cY) = ((w-1) // 2.0, (h-1)// 2.0)

# 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))

print nW, nH

# adjust the rotation matrix to take into account translation

M[0, 2] += ((nW-1)/2.0) - cX

M[1, 2] += ((nH-1)/2.0) - cY

# perform the actual rotation and return the image

return M, cv2.warpAffine(image, M, (nW, nH))

#function that calculates the updated locations of the coordinates

#after rotation

def rotated_coord(points,M):

points = np.array(points)

ones = np.ones(shape=(len(points),1))

points_ones = np.concatenate((points,ones), axis=1)

transformed_pts = M.dot(points_ones.T).T

return transformed_pts

#READ IMAGE & CALL FCT

img = cv2.imread("Lenna.png")

points = np.array([[511, 511]])

#rotate by 90 angle for example

M, rotated = rotate_bound(img, 90)

#find out the new locations

transformed_pts = rotated_coord(points,M)

如果我有例如坐标[511,511]我将得到[-0.5, 511.50]([COL,行])当我期望获得[0,511]。

如果我使用w // 2 a 黑色边框将被添加到图像上,我的旋转更新坐标将再次关闭。

问题:我怎样才能找到一个像素坐标在一个旋转的图像(以一定的角度)使用Python的正确位置?

2017-02-20

Roxanne

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值