首页 > 解决方案 > 旋转后变换图像中的点时出错

问题描述

我试图在旋转图像后转换图像中的点。到目前为止,该功能有效,但是当我尝试通过循环遍历列表来运行该功能时,出现错误:

IndexError                                Traceback (most recent call last)
<ipython-input-34-5212c5d62ca8> in <module>
     31 #bb1 = [(45, 230), (283, 230),(283, 308),(45, 308)]
     32 for i in bb1:
---> 33     new_bb[i] = rotate_box(bb1[i], cx, cy, heigth, width)

IndexError: index 45 is out of bounds for axis 0 with size 4

我使用的代码是:

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), -degree, 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] = (int(calculated[0]),int(calculated[1])) 
    return new_bb 

h = 330
w = 335
cx = 167.5
cy = 167  
degree = 30  

new_bb = []
bb1 = np.array([[45, 230], [283, 230],[283, 308],[45, 308]])
#bb1 = [(45, 230), (283, 230),(283, 308),(45, 308)]
for i in bb1: 
    new_bb[i] = rotate_box(bb1[i], cx, cy, h, w)

它取自Find [x,y] 图像中的旋转坐标位置 [OpenCV / Python]

因此,如果我编写如下列表,则代码可以正常工作:

bb1 = [(45, 230)]

并将最后一行更改为:

new_bb = rotate_box(bb1, cx, cy, h, w)

但是如果我写了一个完整的列表并且不像这样改变最后一个 linbe

bb1 = [[45, 230], [283, 230],[283, 308],[45, 308]]

我得到的错误是:

TypeError                                 Traceback (most recent call 
last)
<ipython-input-52-dbcc21beb4e6> in <module>
     31 bb1 = [[45, 230], [283, 230],[283, 308],[45, 308]]
     32 for i in bb1:
---> 33     new_bb[i] = rotate_box(bb1[i], cx, cy, h, w)


TypeError: list indices must be integers or slices, not list

当我将其更改为:

bb1 = [45, 230, 283, 230,283, 308,45, 308]

我得到错误:

IndexError                                Traceback (most recent call 
last)
<ipython-input-51-6e6aae09f4f2> in <module>
     31 bb1 = [45, 230, 283, 230,283, 308,45, 308]
     32 for i in bb1:
---> 33     new_bb[i] = rotate_box(bb1[i], cx, cy, h, w)

IndexError: list index out of range

谁能建议我哪里出错了?

标签: pythonpython-3.x

解决方案


推荐阅读