简体   繁体   English

opencv 错误:(-215:断言失败)size.width>0 && size.height>0 in function 'cv::imshow'

[英]opencv error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

I wanna crop the image within the coordinates of rectangle from object detection.我想从 object 检测中裁剪矩形坐标内的图像。 Are there any problems in my x,y coordinates?我的 x,y 坐标有问题吗? The rectangle can be drawn but the image cannot be cropped.可以绘制矩形,但不能裁剪图像。

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)

the result of printing bbox is shown below:打印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]]

So,I think the coordinates in the bbox can be used directly.所以,我认为可以直接使用bbox中的坐标。

Print the Xs and Ys, see if they didn't create a 0 area rectangle.打印 Xs 和 Ys,看看它们是否没有创建一个 0 区域的矩形。 You can also change the imshow part to be:您还可以将 imshow 部分更改为:

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

Also, make sure that y2 is actually greater than y1 (same goes to x).此外,确保 y2 实际上大于 y1(x 也是如此)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 相机流 - OpenCV 错误:(-215:断言失败)size.width>0 && size.height>0 in function 'cv::imshow' - Camera Streaming - OpenCV error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow' OpenCV(4.2.0) 错误: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow' - OpenCV(4.2.0) error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow' 错误:(-215:断言失败)函数 cv::imshow 中的 size.width>0 && size.height>0 - error: (-215:Assertion failed) size.width>0 && size.height>0 in function cv::imshow OpenCV 错误:(-215:断言失败)size.width>0 && size.height>0 in function 'imshow' - OpenCV error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow' 错误:(-215:断言失败)函数“imshow”中的 size.width>0 && size.height>0 - error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow' OpenCV错误:在cv :: imshow(Python)中断言失败(size.width> 0 && size.height> 0) - OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow (Python) 错误:函数imshow中的(-215)size.width> 0 && size.height> 0 - error: (-215) size.width>0 && size.height>0 in function imshow OpenCV 错误:(-215)size.width>0 && size.height>0 in function imshow - OpenCV Error: (-215)size.width>0 && size.height>0 in function imshow OpenCV错误:在imshow中断言失败(size.width> 0 && size.height> 0) - OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow 相机校准opencv python imshow错误:(-215)size.width> 0 && size.height> 0 - Camera Calibration opencv python imshow error: (-215) size.width>0 && size.height>0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM