旋转图像后更新cv :: rect

时间:2019-02-03 06:02:41

标签: python opencv

我有一个与here类似的问题。我有一个ROI,要在旋转主图像后进行更新。到目前为止,这是我迄今为止没有做过的事情:

cords = ['0', '0.175', '0.46875', '0.125', '0.5875']
x = float(cords[1]) * 160
y = float(cords[2]) * 96
w = float(cords[3]) * 160
h = float(cords[4]) * 96
x = x - (w / 2)
y = y - (h / 2)
rect = np.float32([[x, y],
                   [x + w, y],
                   [x + w, y + h],
                   [x, y + h]])
for angle in range(-10, 10, 2):
    rot = cv2.getRotationMatrix2D((160 / 2, 96 / 2), angle, 1.0)
    trect = cv2.transform(rect, rot)
    print('Transformed rectangle: ', trect)
    print('done')

但是它抛出:

(-215:Assertion failed) scn == m.cols || scn + 1 == m.cols in function 'cv::transform'

主图像是160 x 96像素的灰度图像。正如我从opencv 的文档中了解到的那样,函数cv :: transform对数组src的每个元素执行矩阵转换并将结果存储在dst 中。我不知道问题出在哪里,因为所接受的链接问题的答案建议这样做是为了获得与旋转图像中的rect相对应的四个点。

1 个答案:

答案 0 :(得分:0)

文档中不清楚,但需要使用numpy数组,如下所示:

rect = np.array([[[x, y]],
                 [[x + w, y]],
                 [[x + w, y + h]],
                 [[x, y + h]]])