简体   繁体   English

OpenCV使用imgproc函数时出错(2)

[英]OpenCV Gives an error when using the imgproc functions (2)

Every time I use the image processing functions in opencv, I get a c++ runtime error. 每次我在opencv中使用图像处理函数时,都会出现c ++运行时错误。

This is my code, and I get "the application has requested to terminate it in an unusual way" 这是我的代码,我得到“应用程序已请求以不寻常的方式终止它”

#include <opencv2/opencv.hpp>
#include <string>

int main() {
    cv::Mat a = cv::imread("img.jpg");
    cv::Mat b(a);

    cv::Canny(a,b,250,300);
    cv::namedWindow("Hello");
    cv::imshow("Hello",b);
    cv::waitKey(2000);
    return 0;
}

The cv::Canny function requires always a grayscale image as input. cv::Canny函数需要始终将灰度图像作为输入。 You need to convert a to grayscale first. 您需要先将a转换为灰度。 The following code snippet does the trick: 以下代码片段可以解决这个问题:

cv::cvtColor(a, a, CV_BGR2GRAY);

Have you checked what 'a' is after the imread? 你有没有检查过imread后的'a'是什么?

What if it fails because the "img.jpg" is in a different directory or you don't have permission. 如果因为“img.jpg”位于不同的目录或您没有权限而失败,该怎么办?

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

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