C++ 从命令行(Windows)安装opencv
我试图在一个项目中使用opencv,但在“安装”它时遇到了问题。我提取了opencv文件并创建了一个小测试程序:C++ 从命令行(Windows)安装opencv,c++,windows,opencv,command-line,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()){ s
#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;
}
我得到以下错误:
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;
经过相当多的尝试,这就是我的工作。奇怪的是,尽管我有一台WINDOWS电脑,但按照LINUX指南安装opencv比WINDOWS指南更有效 在没有VS的Windows上安装OpenCV指南 提醒:这是一个多步骤的过程,需要3个单独的工具。准备好这需要一段时间 第一部分:准备好一切
- 下载。
- 在下载页面上,单击“MinGW-w64-builds”选项。不要点击“赢得构建”选项
- 必须使用MinGW-w64的原因是,它是针对windows改进的MinGW编译器套装的较新版本。这意味着它支持
线程系统,其中作为标准的MinGW编译器只支持posix
线程系统。OpenCV依赖于win32
线程系统,因此需要使用MinGW-w64编译器posix
- 将MinGW-w64 zip文件夹解压缩到一个目录。在我的例子中,它是
PortableGit/opt/MinGW-w64
- 此时,您可以将
文件夹添加到路径中。(假设这不会导致任何冲突。)如果这样做,则不必经常指定MingGW-w64/mingw32/bin
可执行目录来运行它。这由你决定g++
- 下载
- 不要下载windows软件包,请单击显示“源”的按钮
- 将opencv sources zip文件夹解压缩到一个目录。在我的例子中,它是
PortableGit/opt/opencv-4.3.0
- 还可以直接从下载
源文件opencv\u contrib
- 提取该文件夹并将其放在顶层opencv文件夹中:
PortableGit/opt/opencv-4.3.0/opencv_contrib
- 下载。
- 我下载了zip文件夹,但是如果您愿意,可以下载安装程序
- 如果下载了CMake zip文件夹,请解压缩该文件夹,或者运行安装程序。我把我的CMake文件夹放在这里:
PortableGit/opt/CMake-3.17.1-win32-x86
- 此时,您可以将
文件夹添加到路径中。(假设这不会导致任何冲突。)如果这样做,则不必经常指定cmake-3.17.1-win32-x86/bin
可执行目录来运行它。这由你决定cmake
- 导航到opencv目录,创建一个
文件夹并将cd放入其中。build
mkdir构建和cd构建
- 运行以下导出命令。
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\u BUILD\u TYPE=Release-DOPENCV\u VS\u VERSIONINFO\u SKIP=1-DOPENCV\u EXTRA\u MODULES\u PATH=“/PortableGit/opt/opencv-4.3.0/opencv\u contrib/MODULES/”。
标志指定我们正在为MinGW编译器创建构建文件-G
指定我们正在构建opencv的发布版本,而不是调试版本-DCMAKE\u BUILD\u TYPE=Release
- 需要将
DOPENCV\u EXTRA\u MODULES\u路径设置为
文件夹中的MODULES文件夹。对我来说,它是opencv\u contrib
PortableGit/opt/opencv-4.3.0/opencv\u contrib/modules
指定不包含版本信息。如果未设置,编译器将抛出一个错误,抱怨没有版本文件。(如下所示仅供参考。)DOPENCV\u VS\u VERSIONINFO\u SKIP
- 如果成功,cmake命令将按以下方式完成:
- 现在再次从构建文件夹运行此命令:
/PortableGit/opt/MinGW-w64/mingw32/bin/mingw32-make.exe-j7
是Linuxmingw32 make.exe
命令的windows等价物make
选项以最多7个线程运行进程-j7
- 这需要一段时间!我的笔记本电脑花了大约20分钟才完成
- 如果
命令以错误结束,请确保在继续任何故障排除之前重置生成目录。这是通过这一系列命令完成的make
- 要使用刚刚在自己的项目中编译的opencv库,请从项目主目录中使用这些标志编译项目李>
- 请记住,您的编译器现在必须设置为mingw-w64编译器,以支持opencv
- 为了可读性,我添加了缩进和换行,但在终端中输入时,不包括换行或缩进
- 链接器选项末尾的数字可能会根据您下载的opencv版本而变化。我下载了opencv-4.3.0,我的号码是430,但你的可能不同
或者你也可以下载VS,由你决定。希望这能有所帮助。更正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
路径似乎OpenCV至少需要C++11。尝试使用-std=c++11
标志进行编译(或者至少将编译器升级到GCC 6.1,默认启用c++14)我有GCC版本6.3.0-1,我尝试过使用-std=c++11
但没有任何更改。奇怪的是,std::recursive_mutex
在c++1中可用
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
rm -rf build
mkdir build
cd build
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
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