Click here to Skip to main content
13,795,928 members
Click here to Skip to main content
Add your own
alternative version


81 bookmarked
Posted 13 Mar 2014
Licenced CPOL

Video Capture using OpenCV with VC++

, 7 Apr 2014
Rate this:
Please Sign up or sign in to vote.
It is simple video capture application using Windows form with VC++. This project demonstrate, Configuration VC++ for Opencv as well as create windows form application.

It is simple video capture application using Windows form with Visual C++. This project demonstrate the Visual C++ Configuration for OpenCV as well as create windows form application.

OpenCV is the Open Computer Vision. It is open source library for image processing. This article helps programmers to configure the visual studio for visual c++ to run OpenCV application using windows controls. OpenCV library can be integrated into visual studio. It easy to configure. Following six step shows the how to configure the visual studio 2010. This article having the video capture project, which is made in Visual Studio 2010 version.

Configuration Visual C++


Part I

Part II

Prepare Visual Studio 2010

Step 1:

Go to Property Manager of project Select "Debug | Win32" Right click on it. then Select Properties then follow second step.


Step 2:

Select the General subcategory from C/C++ category. Select the Additional Include Directories, add following path.


For 64bit :

C:\opencv\build\x86\vc10\bin ;

For 32bit :

C:\opencv\build\x64\vc10\bin ;

Step 3:

Set the library path to "C:\opencv\build\x86\vc10\lib" if the system is 64 bit, else set library path to "C:\opencv\build\x64\vc10\lib". then click on Ok button.

Step 4:

In this step, Need to add the OpenCV library. These are located at "C:\opencv\build\x86\vc10\lib" path location.

For Debug


opencv_contrib248d.lib opencv_core248d.lib




opencv_imgproc248d.lib opencv_legacy248d.lib


opencv_nonfree248d.lib opencv_objdetect248d.lib


opencv_photo248d.lib opencv_stitching248d.lib


opencv_video248d.lib opencv_videostab248d.lib










For Release


Remove the last 'd' from file name. e.g. opencv_calib3d248d.lib to opencv_calib3d248.lib

Step 5:

Change Common Language Runtime Support, Pure MSIL Common Language RunTime Support (/clr:pure) to Common Language RunTime Support (/clr). then click on "Ok" to finish configuration.

Step 6:

Add following Environmental Path.

For 64bit : C:\opencv\build\x86\vc10\bin ;

For 32bit : C:\opencv\build\x64\vc10\bin ;

Before go to the code refer following tip, to understand that, how to use windows picture box control with OpenCV. All query frame displayed in picture box.

How to Display Image In Picturebox in VC++ from Iplimage and Mat

Using the code

Before going to write the code, need to include the header files as listed follow. capture and frame variable declared outside the namespace follow by the header files. These variable are the pointer variables.

#pragma once
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"

CvCapture* capture;
IplImage* frame;  

cvCaptureFromCAM function initialized the capture with first camera (index 0). If index passed as 1 then, it initialized with second camera and so on.

capture = cvCaptureFromCAM(0); 

Track bar initialized with total number of video frames. CV_CAP_PROP_FRAME_COUNT is a constant which passed to cvGetCaptureProperty to get total number of frames works with video file, not for cameras.

trackBar1->Maximum = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT); 

Releases the capture from resources.


Following function capture the video from camera and also from video file like *.avi, *.mp4 .

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
   if(comboBox1->Text == "")
    MessageBox::Show(this,"Select Capture Method","Error!!!");
   if(button2->Text == "Start")
    if (comboBox1->Text == "Capture From Camera")
        capture = cvCaptureFromCAM(0);
        trackBar1->Minimum = 0;
        trackBar1->Maximum = 0;
        button2->Text = "Stop";
    else if (comboBox1->Text == "Capture From File")
        openFileDialog1->Filter = "AVI files (*.avi)|*.txt|All files (*.*)|*.*";
        openFileDialog1->FilterIndex = 2;
        openFileDialog1->RestoreDirectory = true;
        openFileDialog1->FileName ="";
        if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
         char *fileName = (char*) Marshal::StringToHGlobalAnsi(openFileDialog1->FileName).ToPointer();                    
         capture = cvCaptureFromFile(fileName);
         trackBar1->Minimum = 0;
         trackBar1->Maximum = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);
         button2->Text = "Stop";
   else if(button2->Text == "Stop")
     button2->Text = "Start";

cvQueryFrame function query the frame current video capture. and assigned to frame variable.

frame = cvQueryFrame(capture); 

Following statement is the replacement for the imshow( "windowname" , frame ) function of OpenCV which shows the image frame.

pictureBox1->Image  = gcnew System::Drawing::Bitmap(frame->width,frame->height,frame->widthStep,System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) frame->imageData);

Timer initialized with 30 intervals. It executes the function after 30 interval of time. Following function Query the frame , and extract the video properties. Following constants are used to extract video properties

CV_CAP_PROP_POS_FRAMES : Get current position of video frame.

CV_CAP_PROP_FOURCC : Get video codec information.

CV_CAP_PROP_POS_MSEC : Get Time information.

CV_CAP_PROP_FRAME_HEIGHT : Get video frame height.

CV_CAP_PROP_FRAME_WIDTH : Get video frame width.

CV_CAP_PROP_FPS : Get video frame rate (Fame Per Seconds) .

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) 
        frame = cvQueryFrame(capture);    
        if(frame != NULL)
            pictureBox1->Image  = gcnew System::Drawing::Bitmap(frame->width,frame->height,frame->widthStep,System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) frame->imageData);
            trackBar1->Value = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES);            
            double codec_double = cvGetCaptureProperty(capture,CV_CAP_PROP_FOURCC);
            label6->Text = "Codec: " + System::Text::Encoding::UTF8->GetString(BitConverter::GetBytes((int)codec_double));
            label7->Text = "Time: " + (TimeSpan::FromMilliseconds( cvGetCaptureProperty(capture,CV_CAP_PROP_POS_MSEC) ).ToString())->Substring(0, 8);
            label8->Text = "Frame No.: " + (int)cvGetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES);
            label9->Text = "Video Resolution: " + (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT) + " X " + (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH);
            label11->Text = "Video Frame Rate: " + (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);

Track Bar initialized with position of current video frame. CV_CAP_PROP_POS_FRAMES is a constant which is used for getting the current position of video frame.

private: System::Void trackBar1_Scroll(System::Object^  sender, System::EventArgs^  e) 
    cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES, trackBar1->Value);

Points of Interest

  • Learn how to configure visual c++.
  • Learn how to capture the video from video file as well as cameras.
  • Learn how to extract the video properties.







Sorry for my English. If you notice errors or can suggest a more correct version, please let me know.


  • 9 March 2014 - First release.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Opulent Infotech
India India
I am Soham Gandhi from India. I studied BE in Information Technology at University of Pune. ( I have been learning OpenCV, Enjoying it.
My research interest in Image Processing, Artificial Intelligence, Security Systems, Security Metrics. I work on OpenCV, EmguCV, Visual Studio 2008/10, Java platform.

Home Page (Blog)

You Tube Channel

You may also be interested in...


Comments and Discussions

Questionno live video Pin
Member 1328891522-Apr-18 14:11
memberMember 1328891522-Apr-18 14:11 
QuestionWarning 1 warning LNK4248: token typeref non résolu (0100003E) pour 'CvCapture' Pin
Member 1332036320-Jul-17 23:26
memberMember 1332036320-Jul-17 23:26 
PraiseMy vote of 5 Pin
Premnath Mali4-Apr-17 0:54
professionalPremnath Mali4-Apr-17 0:54 
Questionvideo not playing Pin
Member 1296478823-Jan-17 20:35
memberMember 1296478823-Jan-17 20:35 
QuestionHow to run this project in QT creator? Pin
Member 1284171013-Nov-16 5:34
memberMember 1284171013-Nov-16 5:34 
QuestionChanging the output of the Capture Card Pin
Member 121235798-Jun-16 22:15
memberMember 121235798-Jun-16 22:15 
Questionvideocapture initialized from screen Pin
Member 1246526916-Apr-16 21:13
memberMember 1246526916-Apr-16 21:13 
Questionabout capture.exe Pin
Member 1230868528-Mar-16 3:34
memberMember 1230868528-Mar-16 3:34 
QuestionVideo Capture using OpenCV 2.4.11with VC++2010 Pin
Member 1221965021-Mar-16 1:17
memberMember 1221965021-Mar-16 1:17 
Questionhow to run it with openCV 2.4.10? Pin
Member 1149176229-Sep-15 1:37
memberMember 1149176229-Sep-15 1:37 
AnswerRe: how to run it with openCV 2.4.10? Pin
SOHAM_GANDHI22-Dec-15 0:34
professionalSOHAM_GANDHI22-Dec-15 0:34 
QuestionDebug Assertion Failed! Pin
Member 1192438021-Aug-15 19:02
memberMember 1192438021-Aug-15 19:02 
AnswerRe: Debug Assertion Failed! Pin
SOHAM_GANDHI22-Dec-15 0:32
professionalSOHAM_GANDHI22-Dec-15 0:32 
GeneralMy vote of 5 Pin
mini.mi2-Jun-15 20:34
professionalmini.mi2-Jun-15 20:34 
GeneralRe: My vote of 5 Pin
SOHAM_GANDHI16-Jun-15 1:54
professionalSOHAM_GANDHI16-Jun-15 1:54 
QuestionHow to pause/resume video in your code? Pin
Member 111664429-Nov-14 6:15
memberMember 111664429-Nov-14 6:15 
AnswerRe: How to pause/resume video in your code? Pin
SOHAM_GANDHI10-Nov-14 19:30
professionalSOHAM_GANDHI10-Nov-14 19:30 
Question/clr??? Pin
code_junkie4-Nov-14 9:06
membercode_junkie4-Nov-14 9:06 
Generalthanks Pin
Member 1117469523-Oct-14 3:19
memberMember 1117469523-Oct-14 3:19 
GeneralThanks a lot! Pin
betojasz12-Aug-14 20:46
memberbetojasz12-Aug-14 20:46 
GeneralRe: Thanks a lot! Pin
SOHAM_GANDHI17-Aug-14 20:07
professionalSOHAM_GANDHI17-Aug-14 20:07 
QuestionCan you show me how to save record video? Pin
徐强16-Apr-14 19:55
member徐强16-Apr-14 19:55 
AnswerRe: Can you show me how to save record video? Pin
SOHAM_GANDHI20-Apr-14 4:24
professionalSOHAM_GANDHI20-Apr-14 4:24 
QuestionHow do you resolve the problem with HD resolution? Pin
Evgeny Pereguda9-Apr-14 4:28
memberEvgeny Pereguda9-Apr-14 4:28 
AnswerRe: How do you resolve the problem with HD resolution? Pin
SOHAM_GANDHI20-Apr-14 4:14
professionalSOHAM_GANDHI20-Apr-14 4:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.181207.3 | Last Updated 7 Apr 2014
Article Copyright 2014 by SOHAM_GANDHI
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid