Python-OpenCV cv2 OpenCV错误:未知函数中的断言失败(scn == 3 || scn == 4),文件..\..\..\modules\imgproc\src\color.cpp

 指尖的烟味让我清醒7758_371 发布于 2023-02-07 17:13

我正在尝试使用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 "", line 1, in 
   execfile(filename, namespace)
 File "C:/Users/Prashant/.spyder2/.temp.py", line 15, in 
   imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
 cv2.error: ..\..\..\modules\imgproc\src\color.cpp:3402: error: (-215) scn == 3 || scn == 4

Abid Rahman .. 85

它表示在应用该功能之前,您的输入图像应该有3或4个通道cv2.cvtColor.

因此在应用函数之前检查图像形状print im.shape.如果结果是None type(大多数情况下,这是问题),您的图像未正确加载,很可能是因为您的路径不正确.

要点是您的图像应该有3个维度,行,列和深度.

1 个回答
  • 它表示在应用该功能之前,您的输入图像应该有3或4个通道cv2.cvtColor.

    因此在应用函数之前检查图像形状print im.shape.如果结果是None type(大多数情况下,这是问题),您的图像未正确加载,很可能是因为您的路径不正确.

    要点是您的图像应该有3个维度,行,列和深度.

    2023-02-07 17:14 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有