在图像[OpenCV/Python]中查找[x,y]旋转坐标位置 [英] Find [x,y] rotated coordinates locations in image [OpenCV / Python]

查看:620
本文介绍了在图像[OpenCV/Python]中查找[x,y]旋转坐标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想依次旋转几个角度的图像.我使用cv2.getRotationMatrix2Dcv2.warpAffine来做到这一点.有一对像素坐标[x,y],其中x = cols,y = rows (在这种情况下),我想在旋转的图像中找到它们的新坐标.

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.

由于 http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html .

问题是我的映射或我的旋转不正确,因为转换后的计算坐标不正确. (我尝试手动计算角点以进行简单验证)

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)

代码:

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]([col,行]).

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].

如果我改用w // 2在图像上添加黑色边框,并且旋转的更新坐标将再次关闭.

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.

问题:如何使用Python在旋转的图像中(以一定角度)找到一对像素坐标的正确位置?

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

推荐答案

对于这种图像旋转的情况,在旋转后图像大小以及参考点都会发生变化,因此必须修改变换矩阵.可以使用以下关系来计算新的with和height:

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.width = h*\sin(\theta) + w*\cos(\theta)

new.height = h * \ cos(\ theta)+ w * \ sin(\ 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.

我在博客中解释了一个示例图像旋转边界框opencv

I explain an example in my blog image rotation bounding box opencv

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)

这篇关于在图像[OpenCV/Python]中查找[x,y]旋转坐标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆