从命令行安装opencv(Windows)

时间:2020-04-13 21:36:33

标签: c++ windows opencv command-line

我正在尝试在项目中使用opencv,并且遇到了“安装”它的问题。我已经提取了opencv文件并创建了一个小型测试程序:

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

int main(int argc, char **argv){
    cv::Mat im=cv::imread((argc==2)? argv[1]: "testing.jpg",1);
    if (im.empty()){
        std::cout << "Cannot open image." << std::endl;
    } else {
        cv::imshow("image",im);
        cv::waitKey(0);
    }
    return 0;
}

要编译程序,我使用了以下命令:

g++ -I"../../PortableGit/opt/opencv/build/include/" -L"../../PortableGit/opt/opencv/build/x64/vc15/lib" main.cpp -lopencv_core -lopencv_highgui -o main

我收到以下错误:

In file included from ../../PortableGit/opt/opencv/build/include/opencv2/core.hpp:3293:0,
                 from ../../PortableGit/opt/opencv/build/include/opencv2/highgui.hpp:46,
                 from ../../PortableGit/opt/opencv/build/include/opencv2/highgui/highgui.hpp:48,
                 from main.cpp:1:
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:714:14: error: 'recursive_mutex' in namespace 'std' does not name
 a type
 typedef std::recursive_mutex Mutex;
              ^~~~~~~~~~~~~~~
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:25: error: 'Mutex' is not a member of 'cv'
 typedef std::lock_guard<cv::Mutex> AutoLock;
                         ^~
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:25: error: 'Mutex' is not a member of 'cv'
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:34: error: template argument 1 is invalid
 typedef std::lock_guard<cv::Mutex> AutoLock;

我相信它与mingw二进制文件无关,不再包含在opencv中。我缺少opencv/build/x86/mingw目录。

我的问题是:

  • 如何在不安装某种IDE和/或CMake的情况下“安装” opencv并使用它? (我更喜欢使用vim和命令行。)
  • 安装后,我将使用什么命令来编译程序并将其与opencv链接?

感谢您的帮助。

修改

这似乎是GCC在Windows上实施threads的问题。使用mingw-w64而不是mingw解决了std::recursive_mutex问题,但是现在链接程序无法找到正确的文件。

/i686-w64-mingw32/bin/ld.exe: cannot find -lopencv_core
/i686-w64-mingw32/bin/ld.exe: cannot find -lopencv_highgui

2 个答案:

答案 0 :(得分:0)

经过很多尝试之后,这就是我要做的工作。奇怪的是,即使我有一台Windows计算机,遵循LINUX指南来安装opencv的操作也比WINDOWS指南要好。

在没有VS的Windows上安装OpenCV的指南

注意:这是一个多步骤过程,需要3个单独的工具。准备一段时间。

第1部分:做好一切准备

  • 下载MinGW-w64
    • 在下载页面上,单击“ MinGW-w64-builds”选项。不要单击“成功构建”选项。
    • MinGW-w64 builds option
    • 之所以必须使用MinGW-w64,是因为它是MinGW编译器套件的更新版本,已针对Windows进行了改进。这意味着它支持posix线程系统,而标准的MinGW编译器仅支持win32线程系统。 OpenCV依赖于posix线程系统,因此需要MinGW-w64编译器。
    • 将MinGW-w64 zip文件夹提取到目录中。就我而言,其PortableGit/opt/MinGW-w64
    • 这时,您可以将MingGW-w64/mingw32/bin文件夹添加到您的路径中。 (假设这不会引起任何冲突。)如果这样做,则不必不断指定g++可执行目录来运行它。这由您决定。
  • 下载opencv release.
    • 请勿下载Windows软件包,请单击显示“源”的按钮
    • Sources, not the windows package
    • 将opencv源zip文件夹提取到目录中。就我而言,其PortableGit/opt/opencv-4.3.0
    • 也可以直接从repository下载opencv_contrib源文件。
    • 解压缩该文件夹并将其放置在顶层opencv文件夹中:PortableGit/opt/opencv-4.3.0/opencv_contrib
  • 下载CMake
    • 我下载了zip文件夹,但您可以根据需要下载安装程序。
    • 如果下载了CMake zip文件夹,请解压缩它,或者运行安装程序。我将我的CMake文件夹放在这里:PortableGit/opt/cmake-3.17.1-win32-x86
    • 这时,您可以将cmake-3.17.1-win32-x86/bin文件夹添加到您的路径中。 (假设这不会引起任何冲突。)如果这样做,则不必不断指定cmake可执行目录来运行它。这由您决定。

第2部分:构建OpenCV

  • 导航到opencv目录并创建一个build文件夹,然后将其cd进入。
    • mkdir build && cd build
  • 运行以下导出命令。
    • export CC=/PortableGit/MinGW-w64/mingw32/bin/gcc.exe
    • export CXX=/PortableGit/MinGW-w64/mingw32/bin/g++.exe
    • 这是为了确保下一个cmake命令使用正确的编译器。
  • 从该文件夹中运行以下cmake命令: PortableGit/opt/cmake-3.17.1-win32-x86/cmake.exe -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DOPENCV_VS_VERSIONINFO_SKIP=1 -DOPENCV_EXTRA_MODULES_PATH="/PortableGit/opt/opencv-4.3.0/opencv_contrib/modules/" ..
    • -G标志指定我们正在为MinGW编译器创建构建文件
    • -DCMAKE_BUILD_TYPE=Release指定我们正在构建opencv的发行版本,而不是调试版本。
    • 需要将DOPENCV_EXTRA_MODULES_PATH设置为opencv_contrib文件夹内的modules文件夹。对我来说是PortableGit/opt/opencv-4.3.0/opencv_contrib/modules
    • DOPENCV_VS_VERSIONINFO_SKIP指定不包括版本信息。如果未设置,则编译器将抱怨没有版本文件而引发错误。 (如下所示,以供参考。)
gcc: error: long: No such file or directory
mingw32-make[2]: *** [modules\core\CMakeFiles\opencv_core.dir\build.make:1341:
modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1
  • 如果成功,则cmake命令将像这样完成:
    • Successfull build
  • 现在再次从构建文件夹运行此命令:/PortableGit/opt/MinGW-w64/mingw32/bin/mingw32-make.exe -j7
    • mingw32-make.exe与Windows make命令等效。
    • -j7选项最多以7个线程运行该进程。
    • 这需要一段时间!我的笔记本电脑花了大约20分钟才能完成
    • 如果make命令以错误结尾,请确保在继续任何故障排除之前重设构建目录。这是通过一系列命令完成的
rm -rf build
mkdir build
cd build

第3部分:使用OpenCV

  • 要使用刚在自己的项目中编译的opencv库,请使用来自项目主目录的这些标志来编译项目。
  • 请记住,您的编译器现在必须设置为mingw-w64编译器才能支持opencv。
  • 我添加了缩进和换行符以提高可读性,但是在终端中输入时不包括换行符或缩进符。
  • 链接器选项末尾的数字可能会更改,具体取决于您下载的opencv版本。我下载了opencv-4.3.0,编号为430,但您的编号可能有所不同。
PortableGit/opt/MinGW-w64/bin/g++.exe
    -I../../PortableGit/opt/opencv-4.3.0/include/
    -I../../PortableGit/opt/opencv-4.3.0/build/
    -I../../PortableGit/opt/opencv-4.3.0/modules/core/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/calib3d/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/dnn/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/features2d/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/flann/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/gapi/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/highgui/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/imgcodecs/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/imgproc/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/ml/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/objdetect/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/photo/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/stitching/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/ts/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/video/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/videoio/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/world/include/
    -L../../PortableGit/opt/opencv-4.3.0/build/lib/
    *.hpp
    *.cpp
    -lopencv_calib3d430
    -lopencv_core430
    -lopencv_dnn430
    -lopencv_features2d430
    -lopencv_flann430
    -lopencv_highgui430
    -lopencv_imgcodecs430
    -lopencv_imgproc430
    -lopencv_ml430
    -lopencv_objdetect430
    -lopencv_photo430
    -lopencv_stitching430
    -lopencv_video430
    -lopencv_videoio430
    -o
    main

或者您可以下载VS。由你决定。希望这会有所帮助。

答案 1 :(得分:0)

更正 JackCamichael 的回答 这两个命令在 Windows 中不起作用

export CC=/PortableGit/MinGW-w64/mingw32/bin/gcc.exe
export CXX=/PortableGit/MinGW-w64/mingw32/bin/g++.exe

应该是

setx -m CC C:\msys64\mingw64\bin\gcc.exe
setx -m CXX C:\msys64\mingw64\bin\g++.exe 

C:\msys64\mingw64\bin 是我机器上的 mingw64 路径