简体   繁体   English

imread() 工作不正常

[英]imread() is not working properly

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

    #include<opencv\cv.h>
    #include<opencv\highgui.h>
    using namespace cv;
    using namespace std;

    int main()
         {
          Mat src;
          //src.create(200,500,CV_8UC3);
          src = imread( "a.bmp", 1 );
          namedWindow( "Display window", WINDOW_AUTOSIZE );
          if(!src.data)                              
                 cout<<"Could not open or find the image" << std::endl ;
          else
                imshow( "Display window", src);
          waitKey(0);
          return 0;
         }

It is always executing the if part它总是在执行if部分
when I am using src.create instead of imread() it shows an empty image.当我使用src.create而不是imread()它显示一个空图像。

To debug your issue you should try to confirm that the image path is correct.要调试您的问题,您应该尝试确认图像路径是否正确。

As suggested in the comments, try specifying the full absolute path of the file.正如评论中所建议的,尝试指定文件的完整绝对路径。 Remember to to use escape slashes if you are on windows (eg c:\\a.bmp will need to be "c:\\a.bmp")如果您在 Windows 上,请记住使用转义斜杠(例如 c:\\a.bmp 将需要为“c:\\a.bmp”)

OR或者

If you are executing your application from Visual Studio then you can configure the working directory to be that of the bitmap too!如果您正在从 Visual Studio 执行您的应用程序,那么您也可以将工作目录配置为位图的工作目录! ( OpenCV cvLoadImage() does not load images in visual studio debugger? ) OpenCV cvLoadImage() 不会在 Visual Studio 调试器中加载图像?

You can also try using cvLoadImage instead of imread.您也可以尝试使用cvLoadImage代替imread的。 If cvLoadImage can open the file then it is possible that you have a mix of release and debug libraries causing you an issue as per:如果 cvLoadImage 可以打开该文件,那么您可能混合使用了发布和调试库,从而导致了以下问题:

The OpenCV documentation has mentioned imread() would return an empty matrix ( Mat::data==NULL ) if the image file cannot be read. OpenCV 文档提到如果无法读取图像文件, imread()将返回一个空矩阵 ( Mat::data==NULL )。

http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imread http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imread

You should check if the "a.bmp" file is in your current working directory.您应该检查“a.bmp”文件是否在您当前的工作目录中。 The IDE (visual studio) may set executable's working directory (Debug/... Release/... ) on purpose. IDE(visual studio)可能会故意设置可执行文件的工作目录(Debug/... Release/...)。 Using an absolute path to "a.bmp", or starting executable in an expected directory from command line would also help, provided that "a.bmp" is a valid BMP file and you have the right file system permission to read it.使用“a.bmp”的绝对路径,或从命令行在预期目录中启动可执行文件也会有所帮助,前提是“a.bmp”是有效的 BMP 文件并且您具有读取它的正确文件系统权限。

I was having the same issue on visual studion and imread returning null data.我在visual studion和imread返回空数据上遇到了同样的问题。

img = cv::imread("c:\\data\\a.bmp",1);

Note the \\\\ delimiters to enable windows to correctly parse the path.请注意\\\\分隔符,以使 Windows 能够正确解析路径。

您的opencv库应该与config匹配,即在Visual Studio DebugRelease您的应用程序或您正在使用的任何构建(如果您使用的是预构建的二进制文件) WINDOWS

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

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