我正在尝试在项目中使用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
目录。
我的问题是:
感谢您的帮助。
修改:
这似乎是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
答案 0 :(得分:0)
经过很多尝试之后,这就是我要做的工作。奇怪的是,即使我有一台Windows计算机,遵循LINUX指南来安装opencv的操作也比WINDOWS指南要好。
注意:这是一个多步骤过程,需要3个单独的工具。准备一段时间。
posix
线程系统,而标准的MinGW编译器仅支持win32
线程系统。 OpenCV依赖于posix
线程系统,因此需要MinGW-w64编译器。 PortableGit/opt/MinGW-w64
MingGW-w64/mingw32/bin
文件夹添加到您的路径中。 (假设这不会引起任何冲突。)如果这样做,则不必不断指定g++
可执行目录来运行它。这由您决定。PortableGit/opt/opencv-4.3.0
opencv_contrib
源文件。PortableGit/opt/opencv-4.3.0/opencv_contrib
。PortableGit/opt/cmake-3.17.1-win32-x86
cmake-3.17.1-win32-x86/bin
文件夹添加到您的路径中。 (假设这不会引起任何冲突。)如果这样做,则不必不断指定cmake
可执行目录来运行它。这由您决定。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
命令使用正确的编译器。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
/PortableGit/opt/MinGW-w64/mingw32/bin/mingw32-make.exe -j7
mingw32-make.exe
与Windows make
命令等效。-j7
选项最多以7个线程运行该进程。make
命令以错误结尾,请确保在继续任何故障排除之前重设构建目录。这是通过一系列命令完成的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
或者您可以下载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
路径