cv2.imread()中的错误:(-215:声明失败)!_src.empty()-怎么了?

时间:2019-01-10 15:29:39

标签: python opencv

我已使用以下代码来检测大问题,但出现错误,如何解决?错误出现在代码的最后一行

    import cv2
    import numpy as np;

    # Read image
    im = cv2.imread("'C:/Data/frame2.jpg'", cv2.IMREAD_GRAYSCALE)

    # Set up the detector with default parameters.
    detector = cv2.SimpleBlobDetector_create()

    # Detect blobs.
    keypoints = detector.detect(im)

    # Draw detected blobs as red circles.
    # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
    im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0, 0, 255),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

    # Show keypoints
    cv2.imshow("Keypoints", im_with_keypoints)
    cv2.waitKey(0) 

错误:

    Error : im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0, 0, 255),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    cv2.error: OpenCV(3.4.5) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

1 个答案:

答案 0 :(得分:0)

修复:

import cv2
import numpy as np;

# Read image (fixed path)
im = cv2.imread("C:/Data/frame2.jpg", cv2.IMREAD_GRAYSCALE)

if not im:      # always check for None
    raise ValueError("unable to load Image")

错误原因:

如果未正确加载图像,则命令m = cv2.imread("'C:/Data/frame2.jpg'", cv2.IMREAD_GRAYSCALE)可能导致imNone

这是调用cvtColor时错误告诉您的信息:

(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

检查im-如果None没有继续,请检查您的路径以及为何未正确加载它。

文档:

带有一个红色的大框,告诉您:

  

即使图像路径错误,也不会引发任何错误,但是print img会给您None