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

I want to rotate an image at several angles sequentially. I do that using cv2.getRotationMatrix2D and cv2.warpAffine. Having a pair of pixels coordinates [x,y], where x=cols, y=rows (in this case) I want to find their new coordinates in the rotated images.

The problem is my mapping or my rotation is wrong because the transformed calculated coordinates are wrong. (I tried to compute the corners manually for simple verification)

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)

If I have for example the coordinates [511,511] I will obtain [-0.5, 511.50] ([col, row]) when I expect to obtain [0,511].

If I use instead the w // 2 a black border will be added on the image and my rotated updated coordinates will be off again.

Question: How can I find the correct location of a pair of pixels coordinates in a rotated image (by a certain angle) using Python ?

解决方案

For this case of image rotation, where the image size changes after rotation and also the reference point, the transformation matrix has to be modified. The new with and height can be calculated using the following relations:

new.width = h*\sin(\theta) + w*\cos(\theta)

new.height = h*\cos(\theta) + w*\sin(\theta)

Since the image size changes, because of the black border that you might see, the coordinates of the rotation point (centre of the image) change too. Then it has to be taken into account in the transformation matrix.

def rotate_box(bb, cx, cy, h, w):

new_bb = list(bb)

for i,coord in enumerate(bb):

# opencv calculates standard transformation matrix

M = cv2.getRotationMatrix2D((cx, cy), theta, 1.0)

# Grab the rotation components of the matrix)

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

# adjust the rotation matrix to take into account translation

M[0, 2] += (nW / 2) - cx

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

# Prepare the vector to be transformed

v = [coord[0],coord[1],1]

# Perform the actual rotation and return the image

calculated = np.dot(M,v)

new_bb[i] = (calculated[0],calculated[1])

return new_bb

## Calculate the new bounding box coordinates

new_bb = {}

for i in bb1:

new_bb[i] = rotate_box(bb1[i], cx, cy, heigth, width)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值