Click here to Skip to main content
Click here to Skip to main content
Go to top

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.
Introduction

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++

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.

C:\opencv\build\include;

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_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_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 http://www.codeproject.com/Tips/741107/How-to-Display-Image-In-Picturebox-in-VCplusplus-f

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.

cvReleaseCapture(&capture);  

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";
        timer1->Start();
    }
    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";
         timer1->Start();
       }                
    }
   }
   else if(button2->Text == "Stop")
   {
     cvReleaseCapture(&capture);        
     button2->Text = "Start";
     timer1->Stop();
   }
}   

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);
pictureBox1->Refresh(); 

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) 
{
    try
    {        
        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);
            pictureBox1->Refresh();
    
            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);
        }
    }catch(...){}
} 

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.

References

[1] http://opencv.org/

[2] http://en.wikipedia.org/wiki/OpenCV

[3] http://docs.opencv.org/

[4] http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html

Sorry

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

History

  • 9 March 2014 - First release.

License

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

Share

About the Author

SOHAM_GANDHI
Software Developer
India India
I am Soham Gandhi from India. I studied BE in Information Technology at University of Pune. (www.unipune.ac.in). 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)
Follow on   Twitter   Google+   LinkedIn

You may also be interested in...

Comments and Discussions

 
GeneralThanks a lot! Pinmemberbetojasz12-Aug-14 19:46 
GeneralRe: Thanks a lot! PinmemberSOHAM_GANDHI17-Aug-14 19:07 
QuestionCan you show me how to save record video? Pinmember徐强16-Apr-14 18:55 
AnswerRe: Can you show me how to save record video? PinmemberSOHAM_GANDHI20-Apr-14 3:24 
QuestionHow do you resolve the problem with HD resolution? PinmemberEvgeny Pereguda9-Apr-14 3:28 
AnswerRe: How do you resolve the problem with HD resolution? PinmemberSOHAM_GANDHI20-Apr-14 3:14 
QuestionThe code is not compiling Pinmembermailtochandra2000@yahoo.com8-Apr-14 8:22 
AnswerRe: The code is not compiling PinmemberSOHAM_GANDHI20-Apr-14 3:08 
GeneralMy vote of 5 PinprofessionalVolynsky Alex23-Mar-14 10:07 
GeneralRe: My vote of 5 PinmemberSOHAM GANDHI24-Mar-14 7:49 
GeneralRe: My vote of 5 PinprofessionalVolynsky Alex24-Mar-14 8:15 
QuestionBad Zip file.. Pinmemberjoecousins18-Mar-14 12:35 
AnswerRe: Bad Zip file.. PinmemberSOHAM GANDHI18-Mar-14 19:31 
QuestionThanks... and looking forward.... PinmemberPhebous17-Mar-14 13:53 
QuestionWhat compression system did you use? PinmemberDave Cross13-Mar-14 6:52 
AnswerRe: What compression system did you use? PinmemberSOHAM GANDHI13-Mar-14 7:04 
GeneralRe: What compression system did you use? PinmemberDave Cross17-Mar-14 4:46 
GeneralRe: What compression system did you use? Pinmemberrather_b_sailing17-Mar-14 5:15 
GeneralRe: What compression system did you use? PinmemberSOHAM GANDHI17-Mar-14 7:05 
GeneralRe: What compression system did you use? Pinmemberrather_b_sailing17-Mar-14 5:22 
GeneralRe: What compression system did you use? PinmemberDave Cross17-Mar-14 6:32 
GeneralRe: What compression system did you use? PinmemberSOHAM GANDHI17-Mar-14 7:06 
GeneralRe: What compression system did you use? PinmemberEdwaM17-Mar-14 9:18 
GeneralRe: What compression system did you use? PinmemberSOHAM GANDHI17-Mar-14 19:05 
GeneralRe: What compression system did you use? PinmemberEdwaM18-Mar-14 4:37 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 7 Apr 2014
Article Copyright 2014 by SOHAM_GANDHI
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid