简体   繁体   English

OpenCV:断言失败

[英]OpenCV:Assertion Failed

I get this type of error when I try to use the adaptiveThreshold() function.当我尝试使用adaptiveThreshold() 函数时,我遇到了这种类型的错误。 What could have gone wrong?可能出了什么问题?

here is my code:这是我的代码:

int main()
{
    try 
    {
        cv::Mat img = cv::imread("rubikscube.png");
        cv::Mat thres_final;
        cv::Mat adap_thres_final;

        cv::threshold(img, thres_final, 0, 255, CV_THRESH_BINARY);
        cv::adaptiveThreshold(img, adap_thres_final, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY, 11, 2);

        cv::namedWindow("Source");
        cv::imshow("Source", img);

        cv::namedWindow("Threshold");
        cv::imshow("Threshold", thres_final);

        cv::namedWindow("Adaptive Threshold");
        cv::imshow("Adaptive Threshold", adap_thres_final);

        cv::waitKey(0);

    }
    catch (...)
    {
        std::cout << "Exception Occured! \n";
    }

    return 0;
}

在此处输入图片说明

When I run this code on my machine, I get the following error当我在我的机器上运行此代码时,出现以下错误

OpenCV Error: Assertion failed (src.type() == CV_8UC1) in adaptiveThreshold, file /Users/lakshayg/dev/opencv/modules/imgproc/src/thresh.cpp, line 795

adaptiveThreshold accepts images of type CV_8UC1 but the one you are passing is CV_8UC3 . adaptiveThreshold CV_8UC1接受CV_8UC1类型的图像,但您传递的是CV_8UC3

One way of fixing this would be to read the image as grayscale in the first place.解决此问题的一种方法是首先将图像读取为灰度。

The imread call with change to更改为的imread调用

cv::Mat img = cv::imread("rubikscube.png", CV_LOAD_IMAGE_GRAYSCALE);

The CV_LOAD_IMAGE_GRAYSCALE here directs the function to read the image as grayscale.此处的CV_LOAD_IMAGE_GRAYSCALE指示函数将图像读取为灰度。

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

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