我想按顺序以几个角度旋转图像。 我使用
我使用了以下略有改动的代码http://www.pyimagesearch.com/2017/01/02/rotate-images-correctly-with-opencv-and-python/和Affine Transformation的解释,尝试映射旋转后图像中的点:http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html。
问题是我的映射或旋转错误,因为计算得到的变换坐标是错误的。(我尝试手动计算简单验证的角落坐标) 代码:
如果我有例如坐标
如果我使用
问题:如何使用Python找到旋转图像中一对像素坐标的正确位置?
cv2.getRotationMatrix2D
和cv2.warpAffine
完成此操作。 对于一组像素坐标[x,y],其中x = cols,y = rows(在这种情况下),我想在旋转后的图像中找到它们的新坐标。我使用了以下略有改动的代码http://www.pyimagesearch.com/2017/01/02/rotate-images-correctly-with-opencv-and-python/和Affine Transformation的解释,尝试映射旋转后图像中的点:http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html。
问题是我的映射或旋转错误,因为计算得到的变换坐标是错误的。(我尝试手动计算简单验证的角落坐标) 代码:
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,511]
时,我会得到[-0.5, 511.50]
([列,行])。如果我使用
w // 2
,则会在图像上添加黑色边框,并且我的旋转更新后的坐标将再次偏离。问题:如何使用Python找到旋转图像中一对像素坐标的正确位置?