如何在 Windows 上将 OpenCV 库正确链接到 Eclipse?

新手上路,请多包涵

在过去的几个小时里,我一直在尝试让一个基本的 OpenCV 程序在我的 Eclipse Mars IDE 中工作。该程序由以下 main.cpp 组成:

 #include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
  if( argc != 2)
  {
   cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
   return -1;
  }

  Mat image;
  image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

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

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

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

到目前为止我所做的:

  1. 安装 MinGW(在 E:\NVPACK\MinGW 中)并将其 bin 文件路径添加到我的环境变量中。

  2. 在 E:\opencv 安装 OpenCV 2.4.12。在安装的文件夹内是“build”和“sources”文件夹。

  3. 使用 MinGW GCC 工具链制作了一个新的 Eclipse C++ 项目。

  4. 在工具设置(项目属性 –> C/C++ 构建 –> 设置)中,我将 OpenCV 库作为“E:\opencv\build\include”包含在 GCC C++ 编译器中

  5. 在工具设置中,我在 MinGW C++ 链接器中添加了所有库(即 opencv_core2412、opencv_highgui2412、opencv_imgproc2412)并将库搜索路径设置为“E:\opencv\build\x86\vc12\lib”

  6. 将我的二进制解析器设置为 PE Windows 解析器。

构建项目的所有 OpenCV 函数都会出现未定义的引用错误,例如:

 undefined reference to `cv::imread(std::string const&, int)'    main.cpp    line 17 C/C++ Problem
undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'   main.cpp    line 26 C/C++ Problem

很奇怪,声明 Mat 图像不会出错,并且将鼠标悬停在它上面会正确显示 OpenCV 的 Mat 文档。

谷歌搜索问题我发现由于某种原因我可能需要使用 CMake 来构建我自己的库文件(在步骤 5 中使用的那些)。所以安装CMake gui后,我使用E:\opencv\sources作为源目录,并新建了一个目录E:\opencv\MinGW作为我的构建目录。我使用“Eclipse CDT4 -MinGW Makefiles”生成器来配置文件。按下配置后,我的 g++ 编译器会收到以下错误(我的 gcc 编译器也有类似的错误):

 CMake Error at CMakeLists.txt:71 (project):
The CMAKE_CXX_COMPILER:

E:/NVPACK/MinGW/bin/mingw32-g++

is not a full path to an existing compiler tool.

Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.

然而,编译器被标识:“CXX 编译器标识是 GNU 4.8.1”和“C 编译器标识是 GNU 4.8.1”

非常感谢任何有关如何在 Windows 7 上的 Eclipse Mars 中正确设置 OpenCV 2.4.12 库的帮助!

原文由 JimmyJumbo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 714
1 个回答

好吧,我希望这足够详细!首先,一大杯咖啡会有所帮助!

让我们开始吧 :

要求 :

  • github repo 下载 OpenCV 源代码(将其解压缩到我们将命名为 opencv_src_folder 的文件夹)
  • 下载并安装 MinGW(同时将 /bin 文件夹添加到系统路径)
  • 下载并安装 CMake

脚步 :

  • 打开 CMake-gui

    • 将 src 文件夹设置为 opencv_src_folder
    • 将构建文件夹设置为 opencv_src_folder/Mingw_build/
    • 点击 configure 并选择 Eclipse CDT4 - MinGW MakeFile 然后下一步。
    • 如果配置成功,点击 generate
  • 打开命令行(Windows + R)

    • $ cd opencv_src_folder/Mingw_build/
    • $ mingw32-make :这将使用 MinGW 制作 openCV,所以请等到它完成!
    • 现在,让我们安装它 $ mingw32-make install 这将在 opencv_src_folder/Mingw_build/install/ 中安装 cmake
  • opencv_src_folder/Mingw_build/install/bin/ 添加到您的系统路径并重新启动会话。

  • 日食配置:

    • 转到 Eclipse CDT IDE,使用示例 OpenCV 代码创建一个 C++ 程序。

    • 转到 Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes,并添加源 OpenCV 文件夹“opencv_src_folder/Mingw_build/install/include/”

    • 转到 Project > Properties > C/C++ Build > Settings > MinGW C++ Linker > Libraries,并分别添加到库 (-l) 中:(不要忘记将 245 更改为它们用于 lib 文件的任何数字) opencv_calib3d245 opencv_contrib245 opencv_core245 opencv_features2d245 opencv_flann245 opencv_gpu245 opencv_highgui245 opencv_imgproc245 opencv_legacy245 opencv_ml245 opencv_nonfree245 opencv_objdetect245 opencv_photo245 opencv_stitching245 opencv_video245 opencv_videostab245

    • 将构建的 OpenCV 库文件夹 opencv_src_folder/Mingw_build/install/lib 添加到库搜索路径 (-L)。

测试:

您可以使用以下代码作为测试:

 #include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
  if( argc != 2)
  {
   cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
   return -1;
  }

  Mat image;
  image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

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

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

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

最后构建您的项目!

希望有帮助!

原文由 Vtik 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题