Windows OpenCV 环境配置

系统版本

  • Windows 10 1703
  • OpenCV 3.3.1
  • Visual Studio 2017 (v141)

配置过程

  1. 下载 OpenCV 3.3.1 安装包,双击自解压到 D 盘,即放在 D:\OpenCV 目录下。进入该文件夹后,可以看到 sources 和 build 两个文件夹,分别存放着源代码和编译好的文件。设置一个环境变量 OPENCV_DIR(也可以不设置),指向 D:\OpenCV\build
  2. 创建一个新的 C++ 工程,比如叫做 CV_Demo
  3. 右键 CV_Demo 这个 Project,进入 Property Page
  4. 在 C/C++ 中的 General 下的 Additional Include Directories 添加 $(OPENCV_DIR)\include
  5. 在 Linker 中的 General 下的 Additional Library Directories 添加 $(OPENCV_DIR)\x64\vc14\lib
  6. 在 Linker -> Input -> Additional Dependencies 下,Debug 模式添加 opencv_world331d.lib,Release 模式添加 opencv_world331.lib
  7. 将 $(OPENCV_DIR)\x64\vc14\bin 目录下的 opencv_world331d.dll 和 opencv_world331.dll 分别放入 CV_Demo\x64(输出目录) 目录的 Debug 和 Release 目录下,并同时放入 CV_Demo\CV_Demo (源代码文件)目录下

至此配置工作完成,运行一个 Demo 试试吧。

#include "stdafx.h"
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
        return -1;
    }
    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file
    if (image.empty()) // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl;
        return -1;
    }
    namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
    imshow("Display window", image); // Show our image inside it.
    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注