openCV 错误:(-215:断言失败)函数'cv::imshow'中的 size.width>0 && size.height>0



我想从对象检测中裁剪矩形坐标内的图像。我的x,y坐标有问题吗?可以绘制矩形,但不能裁剪图像。

import cv2 
import numpy as np 
image = cv2.imread('venv/img.jpg')
classNames = []
classFile = 'coco.names'
with open(classFile,'rt') as f:
classNames = f.read().rstrip('n').split('n')
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weightsPath, configPath)
net.setInputSize(320, 320)
net.setInputScale(1.0 / 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
classIds, confs, bbox = net.detect(image, confThreshold=0.5)
print(classIds, bbox)
if len(classIds) != 0:
for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox):
cv2.rectangle(image, box, color=(0, 255, 0), thickness=2)
print(bbox)
x1=box[0]
y1=box[1]
x2=box[2]
y2=box[3]
crop_person = image[y1:y2,x1:x2]
cv2.imshow('img',crop_person)
cv2.waitKey(0)

image = cv2.resize(image,(480,640))
cv2.imshow('img',image)
cv2.waitKey(0)

打印bbox的结果如下所示:

[[384 617 218 731]
[ 45 722 188 472]
[568 648 251 622]
[767 691 165 528]
[888 615 131 537]
[232 657 187 545]
[ 27 598 128 475]
[168 590  98 169]
[782 573 137 301]
[795 667 186 499]
[150 717 117 472]
[227 628 312 672]]

所以,我认为可以直接使用框中的坐标。

打印X和Y,看看它们是否没有创建一个0面积的矩形。您也可以将imshow部分更改为:

if (y2-y1)*(x2-x1) > 0:
cv2.imshow(....)

此外,请确保y2实际上大于y1(x也是如此(。

最新更新