因为写的代码需要在图片上画带角度的检测框,因此我需要使用opencv里的polylines这个函数,但是在写完后发现编译器报这个错:
TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)
但是我打印了一下发现整个过程中的维度都是没有问题的,所以错在哪呢。。。
后面看到这个简书回答,发现是在使用cv2.polylines函数过程中整个numpy的存储形式发生了改变,输入也变成了输出,所以在用这个函数之前需要先copy()一下,这样就正常了。
之前的代码:
def show_bbox_image(image, bbox):
if not isinstance(image, np.ndarray):
image = tvtsf2np(image)
if not isinstance(bbox, list):
bbox = numpy_box2list(bbox)
# 不copy会报错
# img = image.copy()
for elm in bbox:
grasp = np.array(elm, np.int32)
grasp.reshape((-1, 1, 2))
cv2.polylines(image, [grasp], True, (255, 0, 0), 2)
cv2.imshow('1', image)
cv2.waitKey(0)
copy()之后就能正常使用了
def show_bbox_image(image, bbox):
if not isinstance(image, np.ndarray):
image = tvtsf2np(image)
if not isinstance(bbox, list):
bbox = numpy_box2list(bbox)
# 不copy会报错
img = image.copy()
for elm in bbox:
grasp = np.array(elm, np.int32)
grasp.reshape((-1, 1, 2))
cv2.polylines(img, [grasp], True, (255, 0, 0), 2)
cv2.imshow('1', img)
cv2.waitKey(0)