猿问

cv2 折线无法在 numpy 数组上绘制“参数 'img' 的预期 Ptr<cv::UMat>”

我正在将图像和一系列点的 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'

如何在该图像上绘制线条?



FFIVE
浏览 145回答 1
1回答

紫衣仙女

解决方案是复制图像img&nbsp;=&nbsp;img.copy()看起来 Tensor.numpy() 实际上为您提供了底层数据结构的不可变视图,因此您可以显示它但不能修改它。要修改它,我必须创建自己的副本
随时随地看视频慕课网APP

相关分类

Python
我要回答