VS2013 configure opencv

These days, I am busy carrying out my last experiments named “video anomaly detection” for my thesis. For extracting features of video, I would use some functions of opencv, so I configured opencv in VS2013, here are some steps.

Environment

  • C++ IDE: VS2013 Ultimate
  • opencv version: 2.4.8
  • operating system: win8

Step 1: install opencv and configure

download opencv and extract to your file folders. Here I extracted it to F:/program files.

Then we need to configure environment variables for opencv.

1
2
3
4
5
6
7
#add system variable -- Path
F:\program files\opencv\build\x86\vc12\bin

#add user variable -- opencv
F:\program files\opencv\build
#add it to PATH
F:\program files\opencv\build\x86\vc12\bin

pay attention:
please choose X86, whether your system is 32bit or 64bit, otherwise some errors will occur when you compiles.

Step2: Configure opencv in VS2013

Firstly, we create a new visual C++ project.
Then follow the pic below. We need to change three places.
Alt text

  1. VC++目录

    1
    2
    3
    4
    #add:
    F:\program files\opencv\build\include
    F:\program files\opencv\build\include\opencv
    F:\program files\opencv\build\include\opencv2
  2. 库目录

    1
    2
    #add:
    F:\program files\opencv\build\x86\vc12\lib
  3. 连接器->输入->附加依赖项

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #add:
    opencv_calib3d248d.lib
    opencv_contrib248d.lib
    opencv_core248d.lib
    opencv_features2d248d.lib
    opencv_flann248d.lib
    opencv_gpu248d.lib
    opencv_highgui248d.lib
    opencv_imgproc248d.lib
    opencv_legacy248d.lib
    opencv_ml248d.lib
    opencv_nonfree248d.lib
    opencv_objdetect248d.lib
    opencv_ocl248d.lib
    opencv_photo248d.lib
    opencv_stitching248d.lib
    opencv_superres248d.lib
    opencv_ts248d.lib
    opencv_video248d.lib
    opencv_videostab248d.lib

These files are in F:\program files\opencv\build\x86\vc12\lib.

Test: Convert Video to Image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*实现视频的读取以及每一帧图像的保存*/

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace std;

int main(int argc, char **argv)
{

printf("------------- video to image ... ----------------n\n");
//初始化一个视频文件捕捉器
CvCapture* capture = cvCaptureFromAVI("F:/VS2012_workspace/OpticalFlowFarneback/qmul_junction.avi");
//获取视频信息
//定义和初始化变量
int i = 0;
IplImage* img = 0;
char image_name[20];
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
//读取和显示
int num = 0;
char command[100];
char path[100];
int n = 0;
while (1)
{
img = cvQueryFrame(capture); //获取一帧图片
if (!img) break;
cvShowImage("mainWin", img); //将其显示
cvWaitKey(20);
sprintf(image_name, "%s%d%s", "image", ++i, ".jpg");//保存的图片名
char filepath[100];
sprintf(filepath, "%s%s%s", path, "\\", image_name);
cvSaveImage(filepath, img); //保存一帧图片
}
cvReleaseCapture(&capture);
cvDestroyWindow("mainWin");
return 0;
}