C++ OpenCV cvLoadImage() 不会在 Visual Studio 调试器中加载图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2115816/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:09:10  来源:igfitidea点击:

OpenCV cvLoadImage() does not load images in visual studio debugger?

c++visual-studio-2008opencvvisual-studio-debugging

提问by Nerf42

I am trying to work out a simple hello world for OpenCV but am running out of ideas as to why it is not working.

我正在尝试为 OpenCV 制定一个简单的 hello world,但我想知道它为什么不起作用。

When I compile and run this code:

当我编译并运行此代码时:

#include <cv.h>
#include <highgui.h> 
int main(int argc, char* argv[])
{
 IplImage* img = cvLoadImage( "myjpeg.jpg" );
 cvNamedWindow( "MyJPG", CV_WINDOW_AUTOSIZE );
 cvShowImage("MyJPG", img);
 cvWaitKey(0);
 cvReleaseImage( &img );
 cvDestroyWindow( "MyJPG" );
 return 0;
}

I get a grey box about 200x200 instead of the indicated .jpg file. If I use a different jpg I get the same kind of window, and if I put an invalid filename in, I get a very tiny window (expected).

我得到一个大约 200x200 的灰色框,而不是指示的 .jpg 文件。如果我使用不同的 jpg,我会得到相同类型的窗口,如果我输入一个无效的文件名,我会得到一个非常小的窗口(预期)。

I am using Visual Studio 2008 under Windows 7 Professional.

我在 Windows 7 Professional 下使用 Visual Studio 2008。

Most of the sample programs seem to work fine, so I am doubly confused how that code loads the sample jpgs just fine but in the code above it does not work (even tried the sample jpeg).

大多数示例程序似乎工作正常,所以我很困惑该代码如何很好地加载示例 jpg,但在上面的代码中它不起作用(甚至尝试了示例 jpeg)。

Update

更新

The executables produced by compiling work fine, however the Visual Studio 2008 debugger loads a null pointer into img every time I try to run the debugger - regardless if the file location is implicit or explicit.

编译生成的可执行文件工作正常,但是每次我尝试运行调试器时,Visual Studio 2008 调试器都会将空指针加载到 img 中 - 无论文件位置是隐式还是显式。

回答by Catalin Iacob

It really seems like there's a problem with the path to myjpeg.jpgsince the current directory could be different when you're running under the debugger.

似乎路径有问题,myjpeg.jpg因为当您在调试器下运行时,当前目录可能不同。

By default, the current directory that the Visual Studio debugger uses is the directory containing the .vcprojfile, but you can change it in the project properties (Debugging -> Working Directory).

默认情况下,Visual Studio 调试器使用的当前目录是包含.vcproj文件的目录,但您可以在项目属性(调试 -> 工作目录)中更改它。

Are you 100% sure that you pass the absolute path correctly? Try to pass the same path to fopenand see if it also returns NULL. If so, then the path is incorrect.

您是否 100% 确定您正确传递了绝对路径?尝试将相同的路径传递给fopen并查看它是否也返回NULL. 如果是,则路径不正确。

If you want to see exactly what file is the library trying to open you can use Project Monitorwith a filter on myjpeg.jpg.

如果您想确切地查看库尝试打开的文件,您可以使用带有过滤器的Project Monitormyjpeg.jpg

回答by Jacob

Which version of OpenCV are you using? I've tried your code on the latest (OpenCV2.0) and it works fine. You can download OpenCV2.0 from here.

您使用的是哪个版本的 OpenCV?我已经在最新的(OpenCV2.0)上尝试过你的代码,它工作正常。你可以从这里下载 OpenCV2.0 。

If you want the latest build, you can get check it out with SVN from here.

如果您想要最新版本,可以从这里使用 SVN 进行检查。

回答by oleole

Try to add HAVE_JPEGinto preprocessor definitions.

尝试添加HAVE_JPEG到预处理器定义中。

回答by Jeremy

I encountered the same problem. The Debug version does not load the image, but when I compile and link it as a Release it works. I hope this helps

我遇到了同样的问题。调试版本不加载图像,但是当我编译并将其链接为发布时,它可以工作。我希望这有帮助