OpenCV--入门

环境配置

1.操作系统:Win10 x64
2.Visual Studio : 2015
3.OpenCV : 3.2.0

下载

1.OpenCV-3.2.0程序;
2.官方文档;

安装与配置

说明:本文档只讲安装官方预编译好的程序,需自行编译的请参考官方文档;
1.下载后右键管理员权限运行安装,一路下一步即可;
2.管理员权限运行cmd窗口,执行以下命令:

setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc14

3.编辑环境变量”PATH”,加入”%OPENCV_DIR%\bin”,重启电脑(这一步我没做,运行时报错“程序无法启动,系统找不到opencv_world320.dll”);
4.打开Visual Studio,新建一个空项目:

这里写图片描述

5.项目属性页中设定好以下几个选项(官方文档有说明,图片也大,具体内容不多说了):

这里写图片描述

这里写图片描述

这里写图片描述

6.新建cpp文件,将官方提供的代码粘贴进去;

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/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.data) // 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;
}

7.如果是在Visual Studio中运行,则需配置参数:

这里写图片描述

参数内容为你想显示的图片的文件名,配置好后,将该图片放置在cpp文件同级目录下,按ctrl+F5运行即可看到结果。

8.如果是发布成exe文件,可在cmd中引用并带上图片名称作为参数(注意路径)即可,不写了。

发布了34 篇原创文章 · 获赞 14 · 访问量 5万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章