我正在将图像和一系列点的 pytorch 张量表示形式转换为 numpy,以便我可以在点之间绘制线条并在 jupyter lab 中显示图像(使用 matplotlib)
如果我注释掉该cv2.polylines行,该代码将按预期工作,并向我显示图像。
# convert img tensor to np
img = img / 2 + 0.5
img = img.detach().cpu().numpy().astype(np.float32)
img = np.transpose(img, (1, 2, 0))
print(type(img))
# prints:
# <class 'numpy.ndarray'>
# convert label tensor to numpy ndarray
pts = lbl.detach().cpu().numpy().reshape(-1, 1, 2)
pts = np.rint(pts).astype(np.int32)
print([pts])
# prints:
# [array([[[ 17, 153]],
# [[153, 154]],
# [[159, 692]],
# [[ 14, 691]]], dtype=int32)]
# draw lines between the vertices in pts
cv2.polylines(img, [pts], True, (0,255,255))
# show the image with matplotlib.pyplot
plt.imshow(img)
plt.show()
但是折线给出了错误:
---> 36 cv2.polylines(img, [pts], True, (0,255,255))
37 plt.imshow(img)
38 plt.show()
TypeError: Expected Ptr<cv::UMat> for argument 'img'
如何在该图像上绘制线条?
紫衣仙女
相关分类