我想使用OpenCV2.2中的imread
函数。
我的代码非常简单。
cv::Mat host= imread("1.bmp", CV_LOAD_IMAGE_GRAYSCALE);
之后,主矩阵被填充了零指针,即图像未加载。
如果我使用cvLoadImage
函数,则一切正常。
文件存在,而且我没有混合使用发布和调试库。为什么imread
函数不起作用?
我想使用OpenCV2.2中的imread
函数。
我的代码非常简单。
cv::Mat host= imread("1.bmp", CV_LOAD_IMAGE_GRAYSCALE);
之后,主矩阵被填充了零指针,即图像未加载。
如果我使用cvLoadImage
函数,则一切正常。
文件存在,而且我没有混合使用发布和调试库。为什么imread
函数不起作用?
使用opencv 2.4.8重现。
如果您正在运行调试模式,请检查您是否也在使用debug库,这解决了我们的问题:OpenCV imread(filename) fails in debug mode when using release libraries。
我也遇到了2.4.6版本的相同问题。原因是在选择库时,我选择了调试版本和发布版本。当我只选择了库的调试版本时,一切正常。
#ifdef WIN32
#define ourImread(filename, isColor) cvLoadImage(filename.c_str(), isColor)
#else
#define ourImread(filename, isColor) imread(filename, isColor)
#endif
我也遇到了同样的问题。
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");
但是当我插入cv::waitKey(5000);
时,问题得到了解决。
我不知道为什么,在使用system pause
时无法加载图像,而且在加载完图像后会暂停!
我在Linux上遇到了类似的问题,只有在读取32位tiff图像时才会出现。
Mat mat= imread(filename, CV_LOAD_IMAGE_ANYDEPTH);
我终于解决了Opencv4.3在Windows10 x64和Visual Studio 2019上的问题!!!
好的,现在当你遇到这个问题时,你可以尝试以下方法:
opencv_xxx430d.lib
(注意有d
)是用于Debug,而opencv_xxx430.lib
(没有d
)是用于Release,并且平台是x64
。C/C++->常规
中,你需要设置附加包含目录
为X:/(opencv_build/install)/include
和X:/(opencv_build/install)/include/opencv2
,分别用于Release/Debug。#include <opencv2/imgcodecs.hpp>
。string path = "D:/kk.jpg
(注意,它是/
而不是\
),但是你可以尝试调试你的代码,也许你会发现有些像path = "?D:/kk.jpg"
的东西,该死!所以不要直接复制/粘贴你的图像路径,即使你已经使用了绝对路径,你仍然可能遇到Unicode问题,所以你需要做的是删除代码行(string path = "D:/kk.jpg
),然后逐个重新输入每个字符!如果你幸运的话,你会看到用imshow显示的图像。祝你好运。imread
时遇到了同样的问题。我尝试了所有的解决方案,但实际上我没有添加库opencv2/imgcodecs.hpp
。虽然imshow
没有问题,但是在添加了这个库之后,我能够成功读取图片。请查看相关问题这里
请确保您的路径正确,
根据Opencv的API,我会尝试调用以下内容:
arrayMat[i]=imread("1.jpg" , 1 );
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 ).
祝你好运
另一种可能性:
如果您使用的是OS X并且静态链接OpenCV,请确保使用OpenCV捆绑的libjpeg,而不是系统的libjpeg。
我在使用OpenCV 3.0时遇到了类似的问题,除了cvLoadImage
也无法正常工作。因此,这可能并没有真正回答您的问题,但或许可以帮助其他人。