简体   繁体   English

OpenCV color_bgr2gray 错误

[英]OpenCV color_bgr2gray error

There are a bunch of questions about this function throwing errors, such as here and here but I think mine has a unique twist.有很多关于这个函数抛出错误的问题,比如这里这里,但我认为我的有一个独特的转折。 What I'm trying to do is go through all the images in a folder and detect any faces.我想要做的是浏览文件夹中的所有图像并检测任何人脸。

import cv2
import sys
import os

#Get the folder to be searched and the location of the cascade file from arguments
imageDirectory = sys.argv[1]
cascadePath = sys.argv[2]
faceCascade = cv2.CascadeClassifier(cascadePath)

#Iterate over the png files in the directory
for imagePath in os.listdir(imageDirectory):
    if imagePath.endswith(".png"):
        image = cv2.imread(imagePath)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.1,
            minNeighbors=5,
            minSize=(30, 30)
            #flags = cv2.CV_HAAR_SCALE_IMAGE
        )

The first image will be processed correctly, but then the second will throw an error:第一个图像将被正确处理,但第二个将引发错误:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /tmp/opencv3-20170202-24427-1s95vpr/modules/imgproc/src/color.cpp, line 9748 Traceback (most recent call last): File "face_detect_flip.py", line 22, in gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.error: /tmp/opencv3-20170202-24427-1s95vpr/modules/imgproc/src/color.cpp:9748: error: (-215) scn == 3 || OpenCV 错误:cvtColor 中的断言失败(scn == 3 || scn == 4),文件 /tmp/opencv3-20170202-24427-1s95vpr/modules/imgproc/src/color.cpp,第 9748 行回溯(最近一次调用):文件“face_detect_flip.py”,第 22 行,灰色 = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.error: /tmp/opencv3-20170202-24427-1s95vpr/modules/imgproc/src/color.cpp: 9748: 错误: (-215) scn == 3 || scn == 4 in function cvtColor函数 cvtColor 中的 scn == 4

The files are all color .pngs so I can't tell why the script will work on some of them but not others.这些文件都是彩色的 .png,所以我不知道为什么脚本可以在其中一些文件上运行,而在其他文件上不起作用。 Sometimes changing the order or the filename (but not the extension) will change whether the script runs or not, but not in any pattern that I can discern.有时更改顺序或文件名(但不是扩展名)会更改脚本是否运行,但不会以我可以辨别的任何模式进行。

Looks like the image that causes this error does not have 3 (rgb image) or 4 (rgba image) channels.看起来导致此错误的图像没有 3(rgb 图像)或 4(rgba 图像)通道。 The cvtColor function needs the the image to have at least 3 channels to convert it. cvtColor函数需要图像至少有 3 个通道来转换它。

One possible reason could be that the file isn't being read correctly.一种可能的原因可能是文件没有被正确读取。 This would cause the variable image to be None and hence it wont have the correct number of channels.这将导致可变图像为 None ,因此它不会有正确的通道数。

Another reason could be if the image has fewer channels.另一个原因可能是图像的通道较少。 Maybe it's a grayscale image to begin with and thus has only one channel.也许它一开始是灰度图像,因此只有一个通道。

You can use print(image.shape) to check the number of channels.您可以使用print(image.shape)来检查通道数。 If the value is None then the file wasn't read correctly.如果值为 None 则文件未正确读取。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM