使用 openCV Mat c++ 加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16109471/
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
load image with openCV Mat c++
提问by vidzz
I want to load an image using Mat in openCV
我想在 openCV 中使用 Mat 加载图像
My code is:
我的代码是:
Mat I = imread("C:/images/apple.jpg", 0);
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", I );
I am getting the following error in a message box:
我在消息框中收到以下错误:
Unhandled exception at 0x70270149 in matching.exe: 0xC0000005: Access violation
reading location 0xcccccccc.
Please note that I am including:
请注意,我包括:
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
#include <math.h>
回答by karlphillip
I've talkedabout this so many timesbefore, I guess it's pointless to do it again, but code defensively: if a method/function call can fail, make sure you know when it happens:
我之前已经多次讨论过这个问题,我想再次这样做是没有意义的,但是防御性代码:如果方法/函数调用可能失败,请确保您知道它何时发生:
Mat I = imread("C:\images\apple.jpg", 0);
if (I.empty())
{
std::cout << "!!! Failed imread(): image not found" << std::endl;
// don't let the execution continue, else imshow() will crash.
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", I );
waitKey(0);
Note that Windows' path uses backslash \
instead of the standard /
used on *nix systems. You need to escape the backslash when passing the filename: C:\\images\\apple.jpg
请注意,Windows 的路径使用反斜杠\
而不是/
*nix 系统上使用的标准。传递文件名时需要转义反斜杠:C:\\images\\apple.jpg
Calling waitKey()
is mandatory if you use imshow()
.
EDIT:
编辑:
If it's cv::imread()
that is throwing the exception the only solution I know to work is downloading OpenCV sources and building iton the machine, since re-installing OpenCV doesn't fix the issue.
如果cv::imread()
是抛出异常,我知道唯一可行的解决方案是下载 OpenCV 源代码并在机器上构建它,因为重新安装 OpenCV 并不能解决问题。
回答by Martin Beckett
Have you checked that I exists after imread? Perhaps the file read failed
你有没有检查过我在 imread 之后存在?也许文件读取失败
After reading a file do if ( I.empty() )
to check if it failed
读取文件后执行if ( I.empty() )
检查是否失败
回答by Vuwox
I don't know why you don't have include problem because normally it .hpp file so you're suppose to do
我不知道你为什么没有包含问题,因为通常它是 .hpp 文件,所以你应该这样做
#include <opencv2/highgui/highgui.hpp>
#include <opencv2\core\eigen.hpp>
But your code seems good but add a cv::waitKey(0);
after your imshow.
但是你的代码看起来不错,但cv::waitKey(0);
在你的 imshow 之后添加一个。
回答by Thili
Are you using visual studio 2010 to run the OpenCV code? If so, try compiling in Release mode.
您是否使用 Visual Studio 2010 来运行 OpenCV 代码?如果是这样,请尝试在 Release 模式下编译。
回答by user2580731
As pointed out by @karlphillip, however trivial it may sound but this statement " You need to escape the backslash when passing the filename: C:\images\apple.jpg" is really important.
正如@karlphillip 所指出的,无论听起来多么微不足道,但这句话“传递文件名时需要转义反斜杠:C:\images\apple.jpg”非常重要。