首页 > 解决方案 > 在函数“inRange”中(-215:断言失败)!_src.empty()

问题描述

我目前正在使用 openCV 进行一个小项目,并且收到以下错误消息:

Traceback (most recent call last):
  File "main.py", line 115, in <module>
    value = cv2.inRange(roi, (0,0,0), (255, 255, 75)) 
cv2.error: OpenCV(4.1.2) /root/opencv/opencv-4.1.2/modules/core/src/arithm.cpp:1726: error: (-215:Assertion failed) ! _src.empty() in function 'inRange'

我已经在互联网上做了一些研究,但还没有找到适合我的解决方案。当我更改感兴趣区域的坐标(短 roi)时,总是会发生错误。

代码(至少是最重要的部分):

roi= (50, 270, 150, 192)

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):  

    image = frame.array 
    cut = image[roi[0]:roi[1]][roi[2]:roi[3]]
    value = cv2.inRange(cut, (0,0,0), (255, 255, 75))
    rawCapture.truncate(0)

标签: pythonopencvraspberry-pi

解决方案


确保以正确的顺序对轴进行切片,因为 frame.array返回一个形状为 (y,x,3) 的 numpy 数组。

调用 flush() 后,此属性包含帧的数据作为多维numpy array. 这通常使用维度(行、列、平面)进行组织。因此,尺寸为 x 和 y 的 RGB 图像将生成一个形状为 (y, x, 3) 的数组。确保您的轴切片顺序正确

#RGB Image 640x480
img = np.ones((480,640,3))

roi= (500, 600, 150, 192)

cut = img[roi[0]:roi[1]][roi[2]:roi[3]]
value = cv2.inRange(cut, (0,0,0), (255, 255, 75))

#error: (-215:Assertion failed) ! _src.empty() in function 'cv::inRange'

推荐阅读