首先說下編譯,現在用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
設置完成這些之後,就可以運行起來程序了;
運行效果:(強調:代碼中的循環語句中的空幀判斷是一定要加的,否則當程序播放完後一定崩潰;)