我正在尝试使用 cv2 在 python 中学习轮廓。
我尝试了教程指南中给出的以下代码:
import cv2
import numpy as np
from matplotlib import pyplot as plt
im = cv2.imread('C:\Users\Prashant\Desktop\test.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
img = cv2.drawContour(im, contours, -1, (0,255,0), 3)
cv2.imshow('Image1',img)
我收到这个错误:
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in unknown function, file ..\..\..\modules\imgproc\src\color.cpp, line 3402
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
execfile(filename, namespace)
File "C:/Users/Prashant/.spyder2/.temp.py", line 15, in <module>
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
cv2.error: ..\..\..\modules\imgproc\src\color.cpp:3402: error: (-215) scn == 3 || scn == 4
最佳答案
它说在应用函数 cv2.cvtColor
之前,您的输入图像应该有 3 或 4 个 channel 。
因此,在通过 print im.shape
应用该功能之前,请检查您的图像形状。如果结果是 None type
(大多数情况下,这是问题所在),则您的图像未正确加载,很可能是因为您的路径不正确。
关键是你的图像应该有 3 个维度,行
,列
和深度
。
关于python-2.7 - Python-OpenCV cv2 OpenCV错误: Assertion failed (scn == 3 || scn == 4) in unknown function,文件..\..\..\modules\imgproc\src\color.cpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20822288/