OpenCV初体验

首先说下编译,现在用CMake工具很方便能生成对应的工程,这里我使用的是OpenCV2.4版本对应的开发环境是VS2019

OpenCV的下载地址:https://github.com/opencv/opencv

我这边使用的文件路径:

E:\OpenCV 包含下载的目录 branches 和 tags 两个文件夹,打开文件夹branches就可以看到稳定版本的2.4和3.4两个版本

通过这种方式可以生成对应的工程文件;文件目录:E:\OpenCV\branches\VS_openCV2.4

运行工程生成对应的lib和dll文件 在E:\OpenCV\branches\VS_openCV2.4 路径下

bin目录下生成对应的dll文件

lib目录下生成对应的lib文件

这两个文件夹 是后面开发opencv需要引入的文件;

第二步:我们根据上面的编译好的文件 来使用opencv来实现一个简单的功能

功能需求:打开本地的媒体文件(用这种方法也可以打开摄像头)

/*
2019-10-23 16:03:24
测试环境 测试Opencv的使用
*/

#include "opencv2/opencv.hpp"
using namespace cv;

int main()
{
	VideoCapture camera("F:\\MP4\\test01.3gp");

	while (true)
	{
		Mat frame;
		camera >> frame;
		if (frame.empty())//如果某帧为空则退出循环;如果不判断 当文件播放完后,程序会立即崩溃。
			break;

		imshow("读取视频", frame);
		waitKey(30);
	}

	//camera.release();
	//destroyAllWindows();
	system("pause");

	return 0;
}

这里需要说下 opencv.hpp 这个文件 

/*M///
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009-2010, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#ifndef __OPENCV_ALL_HPP__
#define __OPENCV_ALL_HPP__

#include "opencv2/opencv_modules.hpp"

#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#ifdef HAVE_OPENCV_FLANN
#include "opencv2/flann/miniflann.hpp"
#endif
#ifdef HAVE_OPENCV_IMGPROC
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#endif
#ifdef HAVE_OPENCV_PHOTO
#include "opencv2/photo/photo.hpp"
#endif
#ifdef HAVE_OPENCV_VIDEO
#include "opencv2/video/video.hpp"
#endif
#ifdef HAVE_OPENCV_FEATURES2D
#include "opencv2/features2d/features2d.hpp"
#endif
#ifdef HAVE_OPENCV_OBJDETECT
#include "opencv2/objdetect/objdetect.hpp"
#endif
#ifdef HAVE_OPENCV_CALIB3D
#include "opencv2/calib3d/calib3d.hpp"
#endif
#ifdef HAVE_OPENCV_ML
#include "opencv2/ml/ml.hpp"
#endif
#ifdef HAVE_OPENCV_HIGHGUI
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
#endif
#ifdef HAVE_OPENCV_CONTRIB
#include "opencv2/contrib/contrib.hpp"
#endif

#endif

根据这个文件包含 就可以知道 我们当前使用的这个头文件 包含了头文件 然后依次引入目录 见下面的设置内容

这样直接使用 会报很多的错误,下一步就是完善包含头文件和依赖文件 让程序能够跑起来

打开工程的属性页面,【配置属性】=》【C/C++】=》【常规】=》【附加包含目录】 设置对应的包含文件

附加包含目录:

 

 

下面设置 【配置属性】=》【链接器】=》【常规】=》【附加库目录】 设置对应的包含路径

E:\OpenCV\branches\VS_openCV2.4\lib\Debug (补充:如果是Release 修改debug为Release即可)

设置:【配置属性】=》【链接器】=》【输入】=》【附加依赖项】 设置对应的包含路径

这里我包含了所有的lib文件 后续根据包含对应的头文件 来界定这个包含的lib文件(简单来说 需要哪个模块就包含哪个)

opencv_calib3d2413d.lib
opencv_contrib2413d.lib
opencv_core2413d.lib
opencv_features2d2413d.lib
opencv_flann2413d.lib
opencv_gpu2413d.lib
opencv_haartraining_engined.lib
opencv_highgui2413d.lib
opencv_imgproc2413d.lib
opencv_legacy2413d.lib
opencv_ml2413d.lib
opencv_nonfree2413d.lib
opencv_objdetect2413d.lib
opencv_ocl2413d.lib
opencv_photo2413d.lib
opencv_stitching2413d.lib
opencv_superres2413d.lib
opencv_ts2413d.lib
opencv_video2413d.lib
opencv_videostab2413d.lib

设置完成这些之后,就可以运行起来程序了;

运行效果:(强调:代码中的循环语句中的空帧判断是一定要加的,否则当程序播放完后一定崩溃;)

Guess you like

Origin blog.csdn.net/Wuzm_/article/details/102715937