【问题标题】:ERROR: identifier "img" is undefined错误:标识符“img”未定义
【发布时间】:2014-06-05 07:37:24
【问题描述】:

我是 openCV 的初学者。我下载了 opencv2.4.5 和 Visual Studio Express 2012,然后我点击此链接 http://opencv-srf.blogspot.in/2013/05/installing-configuring-opencv-with-vs.html 设置环境变量等中的所有内容。然后我点击下面的链接 http://opencv-srf.blogspot.in/2013/06/load-display-image.html 创建示例应用程序。我包括了正确的#include 路径。但我遇到了错误。

#include "stdafx.h"

#include <C:\opencv\build\include\opencv\cv.h>
#include <C:\opencv\build\include\opencv\cxcore.h>
#include <C:\opencv\build\include\opencv\highgui.h>

#include "C:\opencv\build\include\opencv2\highgui\highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
     Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'

     if (img.empty()) //check whether the image is loaded or not
     {
          cout << "Error : Image cannot be loaded..!!" << endl;
          //system("pause"); //wait for a key press
          return -1;
     }

     namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
     imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window

     waitKey(0); //wait infinite time for a keypress

     destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"

     return 0;
}

【问题讨论】:

  • 哪一行导致错误?

标签: c++ visual-studio opencv visual-studio-2012


【解决方案1】:

不要为包含使用绝对路径,这是完全不可移植的。

应该是这样的:

// the usual suspects:
#include "opencv2\core\core.hpp"       // Mat is defined here.
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\highgui\highgui.hpp"

另外,为了使这项工作,您的“其他包含文件夹”应指向“opencv/build/include”

并避免使用旧的 c-api 标头,例如 cv.h、highgui.h、cxcore.h

【讨论】:

  • 我无法为所有这些包含行打开源文件“opencv2\core\core.hpp”。在“其他包含文件夹”中,我在 $(OPENCV_DIR)\include 下使用了
  • 所以去检查 $(OPENCV_DIR) 是否指向正确的位置。这甚至可能是错误的。文件应该在“opencv/build/include”中
  • 我不明白。在哪里检查?
  • 这是路径 C:\opencv\build\include\opencv2\core
  • 是的。因此 "c:\opencv\build\include" 用于附加包含
猜你喜欢
  • 2015-05-12
  • 2012-02-26
  • 2011-10-23
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多