C++ imread 在 Opencv 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7417637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:54:14  来源:igfitidea点击:

imread not working in Opencv

c++opencv

提问by luckydevil

I'm trying to use the imreadfunction from OpenCV2.2.

我正在尝试使用imreadOpenCV2.2 中的函数。

My code is very simple.

我的代码很简单。

cv::Mat host= imread("1.bmp", CV_LOAD_IMAGE_GRAYSCALE);

After that, the host matrix became filled by zeros pointers, i.e. an image has not loaded.

之后,宿主矩阵被零指针填充,即图像尚未加载。

If I use cvLoadImagethen it all works properly.

如果我使用,cvLoadImage那么它一切正常。

The file exists, and I am not mixing the release and debug libraries. Why imreaddoesn't work?

该文件存在,我没有混合发布和调试库。为什么imread不起作用?

回答by Jon Ander Ortiz Durántez

Reproduced with opencv 2.4.8.

用 opencv 2.4.8 转载。

If you are running in Debug, check that you are also using debug libraries, it fixed our problem. : OpenCV imread(filename) fails in debug mode when using release libraries.

如果您在 Debug 中运行,请检查您是否也在使用调试库,它解决了我们的问题。: OpenCV imread(filename) 在使用发布库时在调试模式下失败

回答by kadrian

I can confirm, that there are some problems with imread in OpenCV 2.2. However the problems only occurred on a Windows 32bit system. On a linux and on a mac it worked. I can't tell why it didn't work, but we had a small workaround for that.

我可以确认,OpenCV 2.2 中的 imread 存在一些问题。然而,问题只发生在 Windows 32 位系统上。在 linux 和 mac 上它都有效。我不知道为什么它不起作用,但我们有一个小的解决方法。

We fixed this problem with the following macros, maybe you could try this out and use "ourImread" from then on.

我们用以下宏解决了这个问题,也许你可以试试这个,从那时起使用“ourImread”。

#ifdef WIN32
#define ourImread(filename, isColor) cvLoadImage(filename.c_str(), isColor)
#else
#define ourImread(filename, isColor) imread(filename, isColor)
#endif

回答by user2975919

i was facing the same problem with 2.4.6 . The reason was that while selecting the library , i had selected both the debug version and the release version. When i selected only the debug version for the library everything worked fine

我在 2.4.6 遇到了同样的问题。原因是在选择库时,我同时选择了调试版本和发布版本。当我只选择库的调试版本时,一切正常

回答by Lorenzo

I've had the same problem

我遇到了同样的问题

cv::Mat image= cv::imread("immagine12.jpg");   // Read the file

if(! image.data )                              // Check for invalid input
{
    cout <<  "Could not open or find the image" << std::endl ;
    cv::waitKey(5000);
    return -1;
}

cv::namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image );  

//non so perchè ma senza il waitKey e lasciando solo il system pause non carica l'immagine...
cv::waitKey(5000);


system("pause");

but I fixed it when I inserted the cv::waitKey(5000);
I don't know why but with system pauseit can't load the image and it goes on pause after it loads the image!

但是我在插入时修复了它我cv::waitKey(5000);
不知道为什么但是system pause它无法加载图像并且在加载图像后它会暂停!

回答by mrgloom

I have similar problem on Linux, when reading 32 bit tiff images only.

我在 Linux 上有类似的问题,仅在读取 32 位 tiff 图像时。

Mat mat= imread(filename, CV_LOAD_IMAGE_ANYDEPTH);

Problem was because OpenCV was not build with Tiff support for some reason.

问题是因为某些原因 OpenCV 没有使用 Tiff 支持构建。

回答by Maham

I know it is late but someone might found this helpful. I was facing the same problem while using imreadusing OpenCV-3.0. I tried all solutions but actually I was not adding the library opencv2/imgcodecs.hpp. Although imshowwas working without it, but after I add this I was able to read the image.

我知道现在已经晚了,但有人可能会发现这很有帮助。我在imread使用 OpenCV-3.0 时遇到了同样的问题。我尝试了所有解决方案,但实际上我没有添加库opencv2/imgcodecs.hpp。虽然imshow在没有它的情况下工作,但在我添加这个之后我能够阅读图像。

回答by Andrey Kamaev

If you think that it is an OpenCV bug then, please, post your image and instructions for reproducing to the OpenCV bugtracker.

如果您认为这是 OpenCV 错误,请将您的图像和说明发布到OpenCV 错误跟踪器

回答by TripleS

  1. see related question here

  2. Please make sure your path is correct ,

  3. According to the Opencv's API , I'd try this call:

  1. 在此处查看相关问题

  2. 请确保您的路径正确,

  3. 根据 Opencv 的 API ,我会尝试这个调用:

arrayMat[i]=imread("1.jpg" , 1 );

arrayMat[i]=imread("1.jpg" , 1 );

The parameters for imread :

imread 的参数:

Mat imread(const string& filename, int flags=1)
Loads an image from a file.

Parameters: 
 filename – Name of file to be loaded.
 flags – Specifies color type of the loaded image:
   >0 the loaded image is forced to be a 3-channel color image
   =0 the loaded image is forced to be grayscale
   <0 the loaded image will be loaded as-is (note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB if  ). 

Good luck S

祝你好运

回答by lifeiteng

I have the same problem. I solved it. The key is whether the filename has jpg. If the filename is p1, you should use something like this imread("p1.jpg"). But we often set the filename as p1.jpg, here we should use something like this imread("p1.jpg.jpg").

我也有同样的问题。我解决了。关键是文件名有没有jpg。如果文件名是p1,则应使用类似的内容imread("p1.jpg")。但是我们经常将文件名设置为p1.jpg,这里我们应该使用这样的东西 imread("p1.jpg.jpg")

回答by madhat1

This also happened to me, my simple solution was to use the C API and then convert to Mat:

这也发生在我身上,我的简单解决方案是使用 C API,然后转换为Mat

IplImage* img = cvLoadImage("c://frame_201.bmp");
Mat mat = Mat(img);