在VS2019安装OpenCV库

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

Installation by Using git-bash (version>=2.14.1) and cmake (version >=3.9.1)

  1. You must download cmake (version >=3.9.1) and install it. You must add cmake to PATH variable during installation
  2. You must install git-bash (version>=2.14.1). Don't add git to PATH variable during installation
  3. Run git-bash. You observe a command line window. Suppose you want to build opencv and opencv_contrib in c:/lib
  4. In git command line enter following command (if folder does not exist) :
    mkdir /c/lib
    cd /c/lib

  5. 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)

  6. In git command line enter following command :

    ./installOCV.sh

  7. Drink a coffee or two... opencv is ready : That's all!
  8. Next time you run this script, opencv and opencv_contrib will be updated and rebuild

----------- 引用结束 ---------------

1 解压好的文件如下:

在VS2019安装OpenCV库_第1张图片

 2 配置path环境变量

64位操作系统对应x64, vs2019 对应版本vc15

在VS2019安装OpenCV库_第2张图片

 3. VS2019中的配置

在VS2019安装OpenCV库_第3张图片

下面是Debug x64的配置,release x86/x64版需要再配置include, lib目录, 还有link位置

 (1)右键项目属性

在VS2019安装OpenCV库_第4张图片

在VS2019安装OpenCV库_第5张图片

 (2)VC++目录,平台选择X64,右侧包含目录和库目录

  • include头文件目录

包含目录和库目录都是你之前OpenCV的安装路径,将下面两个路径添加进去

在VS2019安装OpenCV库_第6张图片

 D:\opencv\build\include\opencv2  我试过了这个可以不加
D:\opencv\build\include在VS2019安装OpenCV库_第7张图片

  • 库目录

将下面的路径添加进去

 在VS2019安装OpenCV库_第8张图片D:\opencv\build\x64\vc15\lib

(3)修改附加依赖项

将下面的.lib手动打上去,在D:\你的OpenCV安装目录\build\x64\vc15\lib中找

 在VS2019安装OpenCV库_第9张图片

 Linker -> Input -> Aditional Dependencies

在安装路径下找到 D:\opencv\build\x64\vc15\lib

在VS2019安装OpenCV库_第10张图片

 所以这么配置, 453这个版本根据自己下载的版本实际情况填

在VS2019安装OpenCV库_第11张图片

 额外的依赖.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

在VS2019安装OpenCV库_第12张图片

接下来看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

在VS2019安装OpenCV库_第13张图片

你可能感兴趣的:(C++,opencv,git,人工智能)