opencv安装配置教程
1. 安装visual studio 2019
Visual Studio 2022 IDE - 适用于软件开发人员的编程工具 安装企业版,不要安装社区版(功能不全)
blob:https://visualstudio.microsoft.com/fa0456c4-9a1c-4e6d-8103-11159db7a838
企业版:BF8Y8-GN2QH-T84XB-QVY3B-RC4DF
专业版:NYWVH-HT4XC-R2WYW-9Y3CM-X4V3Y
2. 安装git for windows
https://github.com/git-for-windows/git/releases/download/v2.32.0.windows.2/Git-2.32.0.2-64-bit.exe
3. 下载opencv代码或者安装包
链接:https://pan.xunlei.com/s/VMfT4RZJ2O0R6FAgABo8bJZXA1
提取码:p5ht
复制这段内容后打开手机迅雷App,查看更方便
4. cmake编译opencv
OpenCV: Installation in Windows
mkdir /c/lib
cd /c/lib
#!/bin/bash -e
myRepo=$(pwd)
CMAKE_GENERATOR_OPTIONS=-G"Visual Studio 16 2019"
#CMAKE_GENERATOR_OPTIONS=-G"Visual Studio 15 2017 Win64"
#CMAKE_GENERATOR_OPTIONS=(-G"Visual Studio 16 2019" -A x64) # CMake 3.14+ is required
if [ ! -d "$myRepo/opencv" ]; then
echo "cloning opencv"
git clone https://github.com/opencv/opencv.git
else
cd opencv
git pull --rebase
cd ..
fi
if [ ! -d "$myRepo/opencv_contrib" ]; then
echo "cloning opencv_contrib"
git clone https://github.com/opencv/opencv_contrib.git
else
cd opencv_contrib
git pull --rebase
cd ..
fi
RepoSource=opencv
mkdir -p build_opencv
pushd build_opencv
CMAKE_OPTIONS=(-DBUILD_PERF_TESTS:BOOL=OFF -DBUILD_TESTS:BOOL=OFF -DBUILD_DOCS:BOOL=OFF -DWITH_CUDA:BOOL=OFF -DBUILD_EXAMPLES:BOOL=OFF -DINSTALL_CREATE_DISTRIB=ON)
set -x
cmake "${CMAKE_GENERATOR_OPTIONS[@]}" "${CMAKE_OPTIONS[@]}" -DOPENCV_EXTRA_MODULES_PATH="$myRepo"/opencv_contrib/modules -DCMAKE_INSTALL_PREFIX="$myRepo/install/$RepoSource" "$myRepo/$RepoSource"
echo "************************* $Source_DIR -->debug"
cmake --build . --config debug
echo "************************* $Source_DIR -->release"
cmake --build . --config release
cmake --build . --target install --config release
cmake --build . --target install --config debug
popd
In this script I suppose you use VS 2015 in 64 bits CMAKE_GENERATOR_OPTIONS=-G"Visual Studio 14 2015 Win64"
and opencv will be installed in c:/lib/install/opencv-DCMAKE_INSTALL_PREFIX="$myRepo/install/$RepoSource"
with no Perf tests, no tests, no doc, no CUDA and no exampleCMAKE_OPTIONS=(-DBUILD_PERF_TESTS:BOOL=OFF -DBUILD_TESTS:BOOL=OFF -DBUILD_DOCS:BOOL=OFF -DBUILD_EXAMPLES:BOOL=OFF)
./installOCV.sh
----------- 引用结束 ---------------
1 解压好的文件如下:
2 配置path环境变量
64位操作系统对应x64, vs2019 对应版本vc15
3. VS2019中的配置
下面是Debug x64的配置,release x86/x64版需要再配置include, lib目录, 还有link位置
(1)右键项目属性
(2)VC++目录,平台选择X64,右侧包含目录和库目录
包含目录和库目录都是你之前OpenCV的安装路径,将下面两个路径添加进去
D:\opencv\build\include\opencv2 我试过了这个可以不加
D:\opencv\build\include
将下面的路径添加进去
(3)修改附加依赖项
将下面的.lib手动打上去,在D:\你的OpenCV安装目录\build\x64\vc15\lib中找
Linker -> Input -> Aditional Dependencies
在安装路径下找到 D:\opencv\build\x64\vc15\lib
所以这么配置, 453这个版本根据自己下载的版本实际情况填
额外的依赖.lib文件 后面的版本号在opencv bin路径下找 opencv_world453d.lib
4. 测试
#include
#include
#include
#include
int main(int argc, char** argv)
{
/*
if (argc != 2)
{
cout << " Usage: " << argv[0] << " ImageToLoadAndDisplay" << endl;
return -1;
}
*/
cv::Mat image;
// image = imread(argv[1], IMREAD_COLOR); // Read the file
image = cv::imread("C:/Users/mingz/Pictures/Saved Pictures/cat.png", cv::IMREAD_COLOR);
if (image.empty()) // Check for invalid input
{
std::cout << "Could not open or find the image" << std::endl;
return -1;
}
cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE); // Create a window for display.
cv::imshow("Display window", image); // Show our image inside it.
int k = cv::waitKey(0); // Wait for a keystroke in the window
// save as ...
if (k == 's') {
cv::imwrite("cat2.png", image);
}
return 0;
}
Solution Explorer中右键 项目名称
Rebuild
接下来看opencv的教程
OpenCV: OpenCV Tutorials
在项目根目录下打开 [项目名]. vcxprj文件,看到刚才在菜单栏配置的编译依赖规则在xml配置文件里,作用类似Linux环境的Makefile。
true
D:\opencv\build\include;$(IncludePath)
$(ReferencePath)
D:\opencv\build\x64\vc15\lib;$(LibraryPath)
false
true
D:\opencv\build\include\opencv2;D:\opencv\build\include;$(IncludePath)
D:\opencv\build\x64\vc15\lib;$(LibraryPath)
Level3
true
WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
true
Console
true
opencv_world453d.lib;%(AdditionalDependencies)
微软的软件中xml文件都不用手动改,项目中的xml文件都有图形界面配置,用工具软件生成xml配置文件。
Clion IDE配置比Visual Studio简单,但是调试工具没有Visual Studio强
需要更多资料可以去eMule上搜索
配置服务器列表serverlist