如何解决OpenCV(4.6.0)中rectangle函数的(-5:Bad argument)错误?
实例分割推理阶段OpenCV rectangle函数报错解决思路
研究实例分割模型,训练完成后推理时遭遇OpenCV(4.6.0)的rectangle函数报错,错误类型为(-5:Bad argument),尝试将boxes[i][0]转换为int类型后问题仍存在,相关代码及错误详情如下:
相关代码
def get_prediction(img_path, confidence): img = Image.open(img_path) transform = transforms.ToTensor() img = transform(img) img = img.to(device) pred = model([img]) pred_score = list(pred[0]['scores'].detach().cpu().numpy()) pred_t = [pred_score.index(x) for x in pred_score if x>confidence][-1] masks = (pred[0]['masks']>0.5).squeeze().detach().cpu().numpy() # print(pred[0]['labels'].numpy().max()) pred_class = [CLASS_NAMES[i] for i in list(pred[0]['labels'].cpu().numpy())] pred_boxes = [[(i[0], i[1]), (i[2], i[3])] for i in list(pred[0]['boxes'].detach().cpu().numpy())] masks = masks[:pred_t+1] pred_boxes = pred_boxes[:pred_t+1] pred_class = pred_class[:pred_t+1] return masks, pred_boxes, pred_class def segment_instance(img_path, confidence=0.5, rect_th=2, text_size=2, text_th=2): masks, boxes, pred_cls = get_prediction(img_path, confidence) img = cv2.imread(img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) for i in range(len(masks)): rgb_mask = get_coloured_mask(masks[i]) img = cv2.addWeighted(img, 1, rgb_mask, 0.5, 0) cv2.rectangle(img, boxes[i][0], boxes[i][1],color=(0, 255, 0), thickness=rect_th) cv2.putText(img,pred_cls[i], boxes[i][0], cv2.FONT_HERSHEY_SIMPLEX, text_size, (0,255,0),thickness=text_th) plt.figure(figsize=(20,30)) plt.imshow(img) plt.xticks([]) plt.yticks([]) plt.show() segment_instance('image1.png', confidence=0.4)
错误信息
Input In [31], in segment_instance(img_path, confidence, rect_th, text_size, text_th) 20 rgb_mask = get_coloured_mask(masks[i]) 21 img = cv2.addWeighted(img, 1, rgb_mask, 0.5, 0) ---> 22 cv2.rectangle(img, boxes[i][0], boxes[i][1],color=(0, 255, 0), thickness=rect_th) 23 cv2.putText(img,pred_cls[i], boxes[i][0], cv2.FONT_HERSHEY_SIMPLEX, text_size, (0,255,0),thickness=text_th) 24 plt.figure(figsize=(20,30))
error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'rectangle'
Overload resolution failed:
- Can't parse 'pt1'. Sequence item with index 0 has a wrong type
- Can't parse 'pt1'. Sequence item with index 0 has a wrong type
- argument for rectangle() given by name ('color') and position (3)
- argument for rectangle() given by name ('color') and position (3)
解决思路
修正坐标类型与格式:OpenCV的
rectangle要求pt1和pt2必须是整数类型的元组,每个坐标元素都得是int。当前代码中pred_boxes生成的是numpy浮点类型的元组,需修改生成逻辑,逐个转换坐标为int:pred_boxes = [[(int(i[0]), int(i[1])), (int(i[2]), int(i[3]))] for i in list(pred[0]['boxes'].detach().cpu().numpy())]统一参数传递方式:错误提示显示混合位置参数与关键字参数导致重载解析失败,可调整
rectangle的调用方式:# 方式1:去掉color关键字,按位置传参 cv2.rectangle(img, boxes[i][0], boxes[i][1], (0, 255, 0), rect_th) # 方式2:全部使用关键字参数(适配新版本OpenCV) cv2.rectangle(img=img, pt1=boxes[i][0], pt2=boxes[i][1], color=(0, 255, 0), thickness=rect_th)额外校验点:
- 确认
boxes中的坐标值在图像宽高范围内,避免负数或超出边界的情况 - 检查
get_prediction中pred_t的取值逻辑,确保pred_boxes、masks、pred_cls三者长度一致,防止索引越界
- 确认
内容的提问来源于stack exchange,提问作者dilara
相关产品推荐
相关产品推荐

