python - 在函数“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)
解决方案
确保以正确的顺序对轴进行切片,因为 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'
推荐阅读
- typescript - 如何在 Visual Studio 代码中使用最新版本的 tsc?
- python - RegEx - 特定匹配后的单词
- python - TypeError:'int'对象不支持项目分配Python
- python - 试图在 Django Python 中跟踪循环导入错误
- binary - 我可以同时在 AND 和 OR 上使用关联性吗?
- matlab - 如何使用 Matlab 中的 randn 函数创建一个大小为 1,000 且遵循高斯分布的值数组(范围 0-10)?
- flutter - ChangeNotifierProvider 的使用者始终为 null
- javascript - 为什么 push 方法使用 javascrip 在这个队列中工作
- linker - MEMORY 命令(链接器)中相邻区域的长度
- r - 我如何使用 dplyr 来修剪我的数据并按每年的平均值进行汇总?