简体   繁体   English

OpenCV不会加载图片

[英]OpenCV wont load image

So I linked all the libraries for OpenCV, I've added all the .lib's and .dll's needed for it to work but when i go to try and get a picture to show it says that the picture is not there. 所以我链接了OpenCV的所有库,我添加了所有.lib和.dll才能正常工作,但是当我尝试尝试显示图片时,它说图片不存在。 All the paths are correct and the image is in the main directory of the solution here's the code. 所有路径都是正确的,图像在解决方案的主目录中,这里是代码。

Mat color = imread("wall.jpg");

also tried: 还尝试了:

  • D:\\\\wall.jpg and D:\\wall.jpg D:\\\\wall.jpgD:\\wall.jpg

  • D:/wall.jpg and D://wall.jpg D:/wall.jpgD://wall.jpg


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

      using namespace std;

      using namespace cv;
      int main()
      {

    Mat color = imread("wall.jpg");
    if (color.empty())
    {
        cout << "image is empty" << endl;
        system("pause");
        exit(-1);
    }
    else
    {
        cout << "image is displayed" << endl;
        imshow("window", color);
    }



    waitKey();

    }

Output of the code 代码输出

image is empty Press any key to continue . . .

its suppose to say 它应该说

image displayed

My answer assumes you are using Windows since you used the word solution in your description. 我的回答假设您正在使用Windows,因为您在说明中使用了“解决方案”一词。

At least one of the following is true: 至少满足以下条件之一:

  • Your paths are incorrect. 您的路径不正确。 If you are running your code from visual studio, using a relative path like "wall.jpg" uses the working directory, which is not necessarily the solution directory. 如果从Visual Studio运行代码,则使用"wall.jpg"类的相对路径会使用工作目录,而该目录不一定是解决方案目录。
    • Use another mechanicm to verify your path is correct, like running the following from the command line on Windows: start D:\\wall.jpg . 使用另一种方法来验证您的路径是否正确,例如在Windows上从命令行运行以下命令: start D:\\wall.jpg If that command returns a file not found error, your path is wrong. 如果该命令返回文件未找到错误,则您的路径错误。
  • You are be using an unsupported file format. 您正在使用不受支持的文件格式。
    • Make sure you can open the file and view it. 确保您可以打开文件并查看它。
    • Make sure it is a JPEG, TIFF, or PNG. 确保它是JPEG,TIFF或PNG。 The extension may be incorrect. 扩展名可能不正确。 Try with a file you know is one of these types if you aren't certain. 如果不确定,请尝试使用您知道是其中一种类型的文件。
  • You don't have permission to read the file. 您无权读取文件。
    • Open the file and verify you can view it. 打开文件并确认您可以查看它。 If you can, and your opencv process is running as the same user, you should be okay. 如果可以,并且您的opencv进程正在以同一用户身份运行,则应该可以。

From the imread docs : imread文档

Loads an image from a file. 从文件加载图像。

The function imread loads an image from the specified file and returns it. 该函数imread从指定的文件加载图像并返回它。 If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ). 如果无法读取图像(由于缺少文件,权限不正确,格式不受支持或格式无效),该函数将返回一个空矩阵( Mat::data==NULL )。

... ...

On Microsoft Windows* OS and MacOSX*, the codecs shipped with an OpenCV image (libjpeg, libpng, libtiff, and libjasper) are used by default. 在Microsoft Windows * OS和MacOSX *上,默认使用OpenCV映像附带的编解码器(libjpeg,libpng,libtiff和libjasper)。 So, OpenCV can always read JPEGs, PNGs, and TIFFs. 因此,OpenCV始终可以读取JPEG,PNG和TIFF。

... ...

Note 注意

  • The function determines the type of an image by the content, not by the file extension. 该功能通过内容而不是文件扩展名确定图像的类型。

Command Line Working Directory 命令行工作目录

If running from the command line, the working directory is the directory you are in. If you run the file listing command on your OS and can see your "wall.jpg" image, then you are in the correct place. 如果从命令行运行,则工作目录就是您所在的目录。如果在操作系统上运行文件列表命令,并且可以看到"wall.jpg"图像,那么您的位置正确。 Run your OpenCV executable from here with a relative path like .\\path\\to\\my\\opencv-program.exe . 使用相对路径(如.\\path\\to\\my\\opencv-program.exe从此处运行OpenCV可执行文件。

Visual Studio Debug Working Directory Visual Studio调试工作目录

Visual Studio中的工作目录

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

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