hans

hans

【Opencv】cv2.rectangle出错


Error Message:

TypeError: 未找到必需的参数'rec' (位置 2)

Source Code:

ori_img = img[0].detach().cpu().numpy()
ori_img = (ori_img*127.5 + 127.5).transpose([1,2,0]).astype(np.uint8)
x1, y1, x2, y2 = int(bb[0]), int(bb[1]), int(bb[2]), int(bb[3])
ori_img = cv2.rectangle(img=ori_img, pt1=(x1, y1), pt2=(x2, y2), color=(255, 255, 0), thickness=2)

The above error occurred, and after searching online, 99% of the solutions suggest converting all vertex coordinates to int type. However, in my case, I have already done the conversion, but the problem still persists.

At the same time, ori_img has also been converted to uint8 type, which should be fine. But the problem actually lies with ori_img.

The following code is the modified version, with an additional line that calls the function np.ascontiguousarray. After searching online, it is explained as:
Converts an array with non-contiguous memory storage to an array with contiguous memory storage, making it faster to run.

It is not difficult to understand the literal meaning, but I don't quite understand why a numpy matrix that has been converted from a Tensor and all the way down would become non-contiguous in memory.

ori_img = img[0].detach().cpu().numpy()
ori_img = (ori_img*127.5 + 127.5).transpose([1,2,0]).astype(np.uint8)
ori_img = np.ascontiguousarray(ori_img)
x1, y1, x2, y2 = int(bb[0]), int(bb[1]), int(bb[2]), int(bb[3])
ori_img = cv2.rectangle(img=ori_img, pt1=(x1, y1), pt2=(x2, y2), color=(255, 255, 0), thickness=2)
加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。