1. 安装visual studio 2019
https://visualstudio.microsoft.com/zh-hans/vs/?安装企业版,不要安装社区版(功能不全)
blob:https://visualstudio.microsoft.com/fa0456c4-9a1c-4e6d-8103-11159db7a838
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
https://docs.opencv.org/master/d3/d52/tutorial_windows_install.html#tutorial_windows_install_path
Installation by Using git-bash (version>=2.14.1) and cmake (version >=3.9.1)
- You must download?cmake (version >=3.9.1)?and install it. You must add cmake to PATH variable during installation
- You must install?git-bash (version>=2.14.1). Don't add git to PATH variable during installation
- Run git-bash. You observe a command line window. Suppose you want to build opencv and opencv_contrib in c:/lib
- In git command line enter following command (if folder does not exist) :
mkdir /c/lib
cd /c/lib - save this script with name installOCV.sh in 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 example CMAKE_OPTIONS=(-DBUILD_PERF_TESTS:BOOL=OFF -DBUILD_TESTS:BOOL=OFF -DBUILD_DOCS:BOOL=OFF -DBUILD_EXAMPLES:BOOL=OFF) - In git command line enter following command :
./installOCV.sh - Drink a coffee or two... opencv is ready : That's all!
- Next time you run this script, opencv and opencv_contrib will be updated and rebuild
----------- 引用结束 ---------------
1 解压好的文件如下:
?2 配置path环境变量
?3. VS2019中的配置
?(1)右键项目属性
?(2)VC++目录,平台选择X64,右侧包含目录和库目录
包含目录和库目录都是你之前OpenCV的安装路径,将下面两个路径添加进去
?D:\opencv\build\include\opencv2? 我试过了这个可以不加 D:\opencv\build\include
将下面的路径添加进去
?D:\opencv\build\x64\vc15\lib
(3)修改附加依赖项
将下面的.lib手动打上去,在D:\你的OpenCV安装目录\build\x64\vc15\lib中找
?
?Linker -> Input -> Aditional Dependencies
在安装路径下找到?D:\opencv\build\x64\vc15\lib
?所以这么配置
?额外的依赖.lib文件 后面的版本号在opencv bin路径下找 opencv_world453d.lib
4. 测试
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
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的教程
https://docs.opencv.org/master/d9/df8/tutorial_root.html
|