函数 'cv::cvtColor' 中的 !_src.empty()

from skimage.measure import compare_ssim

import argparse

import imutils

import cv2


first = ("original_02.png")

second = ("modified_02.png")



imageA = cv2.imread("first")

imageB = cv2.imread("second")



grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)

grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)



(score, diff) = compare_ssim(grayA, grayB, full=True)

diff = (diff * 255).astype("uint8")

print("SSIM: {}".format(score))



thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cnts = imutils.grab_contours(cnts)



for c in cnts:


    (x, y, w, h) = cv2.boundingRect(c)

    cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2)

    cv2.rectangle(imageB, (x, y), (x + w, y + h), (0, 0, 255), 2)



cv2.imshow("Original", imageA)

cv2.imshow("Modified", imageB)

cv2.imshow("Diff", diff)

cv2.imshow("Thresh", thresh)

cv2.waitKey(0)

这是我的代码,我遇到了标题中提到的错误。我将代码和必要的文件保存在同一个文件夹中


greyA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-j8nxabm_\opencv\modules\imgproc \src\color.cpp:182: 错误: (-215:断言失败) !_src.empty() 在函数“cv::cvtColor”中


上面是我收到的错误


慕村9548890
浏览 171回答 2
2回答

绝地无双

您不是在读取图像,而是将字符串读取到 imread 中。imread()将此解释为路径并可能返回,None因为它找不到路径。相反尝试:first = ("original_02.png")second = ("modified_02.png")imageA = cv2.imread(first)imageB = cv2.imread(second)

紫衣仙女

错误消息告诉您您的图像是空的。这是为什么?— 因为您的代码正在尝试读取名称为'first'且'second'. 实际上,您想要读取这些名称的变量。因此删除引号。此外,没有理由将变量名称放在括号中 - 也删除它们。目前看来您打算将这些变量定义为元组(但您没有):first = "original_02.png"second = "modified_02.png"imageA = cv2.imread(first)imageB = cv2.imread(second)接下来,您应该检查这是否真的有效。如果无法读取图像(...),该函数将返回一个空矩阵
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python