opencv安裝配置教程
1. 安裝visual studio 2019
https://visualstudio.microsoft.com/zh-hans/vs/ 安裝企業版,不要安裝社區版(功能不全)
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
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
In this script I suppose you use VS 2015 in 64 bits#!/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 popdCMAKE_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)
- 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環境變數
64位作業系統對應x64, vs2019 對應版本vc15

3. VS2019中的配置

下面是Debug x64的配置,release x86/x64版需要再配置include, lib目錄, 還有link位置
(1)右鍵專案屬性


(2)VC++目錄,平臺選擇X64,右側包含目錄和庫目錄
- include頭檔案目錄
包含目錄和庫目錄都是你之前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

所以這么配置, 453這個版本根據自己下載的版本實際情況填

額外的依賴.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
在專案根目錄下打開 [專案名]. vcxprj檔案,看到剛才在選單欄配置的編譯依賴規則在xml組態檔里,作用類似Linux環境的Makefile,
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<ExecutablePath>
</ExecutablePath>
<IncludePath>D:\opencv\build\include;$(IncludePath)</IncludePath>
<ReferencePath>$(ReferencePath)</ReferencePath>
<LibraryPath>D:\opencv\build\x64\vc15\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>D:\opencv\build\include\opencv2;D:\opencv\build\include;$(IncludePath)</IncludePath>
<LibraryPath>D:\opencv\build\x64\vc15\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<!-- ... -->
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>opencv_world453d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
微軟的軟體中xml檔案都不用手動改,專案中的xml檔案都有圖形界面配置,用工具軟體生成xml組態檔,
Clion IDE配置比Visual Studio簡單,但是除錯工具沒有Visual Studio強
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/290533.html
標籤:其他
上一篇:資料增強——python opencv 找出影像中的最大輪廓并填充(生成mask)——python 輪廓內部填充
