我正在將圖像和一系列點的 pytorch 張量表示形式轉(zhuǎn)換為 numpy,以便我可以在點之間繪制線條并在 jupyter lab 中顯示圖像(使用 matplotlib)如果我注釋掉該cv2.polylines行,該代碼將按預(yù)期工作,并向我顯示圖像。 # 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'如何在該圖像上繪制線條?
1 回答

紫衣仙女
TA貢獻1839條經(jīng)驗 獲得超15個贊
解決方案是復(fù)制圖像
img = img.copy()
看起來 Tensor.numpy() 實際上為您提供了底層數(shù)據(jù)結(jié)構(gòu)的不可變視圖,因此您可以顯示它但不能修改它。要修改它,我必須創(chuàng)建自己的副本
添加回答
舉報
0/150
提交
取消