[英]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.jpg
和D:\\wall.jpg
D:/wall.jpg
and D://wall.jpg
D:/wall.jpg
和D://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: 至少满足以下条件之一:
"wall.jpg"
uses the working directory, which is not necessarily the solution directory. 如果从Visual Studio运行代码,则使用"wall.jpg"
类的相对路径会使用工作目录,而该目录不一定是解决方案目录。
start D:\\wall.jpg
. 使用另一种方法来验证您的路径是否正确,例如在Windows上从命令行运行以下命令: start D:\\wall.jpg
。 If that command returns a file not found error, your path is wrong. 如果该命令返回文件未找到错误,则您的路径错误。 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. 该功能通过内容而不是文件扩展名确定图像的类型。
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可执行文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.