1

我使用视觉工作室 2013 和 opencv。我运行一个简单的代码来读取图像并显示它。我将图像从 Donloads 文件添加到我的项目的资源文件中。当我在下面运行代码时,image.data它是空的。

  #include "stdafx.h"
   #include <opencv2\core\core.hpp>
  #include <opencv2\highgui\highgui.hpp>
 #include <opencv2\opencv.hpp>


int _tmain(int argc, _TCHAR* argv[])
{

cv::Mat image;
image = cv::imread("im.png", CV_LOAD_IMAGE_COLOR);   // Read the file

if (!image.data)                              // Check for invalid input
{
    std::cout << "Could not open or find the image" << std::endl;
    return -1;
}

cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);// Create a window for display.
imshow("Display window", image);                   // Show our image inside it.

cv::waitKey(0);                                          // Wait for a keystroke in the window
return 0;

}

4

2 回答 2

3

尝试:C:\\Users\\pr\\Downloads\\im.png如果路径正确,那应该可以工作。

于 2015-02-16T07:49:17.113 回答
1

这是因为 VS 中的默认工作目录是vcxproj文件的位置(值为$(ProjectDir))。

如果您想通过 VS(即F5按键)启动您的应用程序,那么您应该覆盖您项目的调试参数。例如,您可以将应用程序的工作目录设置为链接器创建的程序 (exe) 的位置,方法是将 的值设置Project menu -> Properties -> Configuration properties -> Debugging -> Working directory$(OutDir)

于 2015-02-16T08:09:40.330 回答