【问题标题】:Python 3.8.5 cv2 -215:Assertion failedPython 3.8.5 cv2 -215:断言失败
【发布时间】:2021-02-13 02:47:25
【问题描述】:

错误: Build\OpenCV\opencv-4.4.0\modules\imgproc\src\color.cpp:182: 错误: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

代码:

import cv2

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('test.jpg')
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display the output
cv2.imshow('img', img)
cv2.waitKey()

【问题讨论】:

  • 如果 test.jpg 存在并且没有损坏,那么它可能不在您的当前目录中。因此,您可能需要在 cv2.imread() 命令中提供 test.jpg 的路径。

标签: python python-3.x opencv camera cv2


【解决方案1】:

当您传递给它的图像为空时会出现此错误,即 OpenCV 无法读取图像。 您确定test.jpg 与此代码在同一目录中吗?

检查扩展名。可能是jpeg

你有两个选择-

  1. cv2.imread 函数中给出完整路径。
  2. 将图像移动到您的代码所在的目录。

【讨论】:

    猜你喜欢
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 2018-05-20
    • 2021-10-29
    相关资源
    最近更新 更多