Click here to Skip to main content
11,721,385 members (82,417 online)
Click here to Skip to main content

Video Capture using OpenCV with VC++

, 7 Apr 2014 CPOL 56.4K 3.6K 66
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++

Video

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.

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 Opulent Infotech
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)


You Tube Channel

You may also be interested in...

Comments and Discussions

 
QuestionDebug Assertion Failed! Pin
Member 1192438021-Aug-15 18:02
memberMember 1192438021-Aug-15 18:02 
GeneralMy vote of 5 Pin
mini.mi2-Jun-15 19:34
membermini.mi2-Jun-15 19:34 
GeneralRe: My vote of 5 Pin
SOHAM_GANDHI16-Jun-15 0:54
professionalSOHAM_GANDHI16-Jun-15 0:54 
QuestionHow to pause/resume video in your code? Pin
Member 111664429-Nov-14 5:15
memberMember 111664429-Nov-14 5:15 
AnswerRe: How to pause/resume video in your code? Pin
SOHAM_GANDHI10-Nov-14 18:30
memberSOHAM_GANDHI10-Nov-14 18:30 
Question/clr??? Pin
code_junkie4-Nov-14 8:06
membercode_junkie4-Nov-14 8:06 
Generalthanks Pin
Member 1117469523-Oct-14 2:19
memberMember 1117469523-Oct-14 2:19 
GeneralThanks a lot! Pin
betojasz12-Aug-14 19:46
memberbetojasz12-Aug-14 19:46 
GeneralRe: Thanks a lot! Pin
SOHAM_GANDHI17-Aug-14 19:07
memberSOHAM_GANDHI17-Aug-14 19:07 
QuestionCan you show me how to save record video? Pin
徐强16-Apr-14 18:55
member徐强16-Apr-14 18:55 
AnswerRe: Can you show me how to save record video? Pin
SOHAM_GANDHI20-Apr-14 3:24
memberSOHAM_GANDHI20-Apr-14 3:24 
QuestionHow do you resolve the problem with HD resolution? Pin
Evgeny Pereguda9-Apr-14 3:28
memberEvgeny Pereguda9-Apr-14 3:28 
AnswerRe: How do you resolve the problem with HD resolution? Pin
SOHAM_GANDHI20-Apr-14 3:14
memberSOHAM_GANDHI20-Apr-14 3:14 
QuestionThe code is not compiling Pin
mailtochandra2000@yahoo.com8-Apr-14 8:22
membermailtochandra2000@yahoo.com8-Apr-14 8:22 
AnswerRe: The code is not compiling Pin
SOHAM_GANDHI20-Apr-14 3:08
memberSOHAM_GANDHI20-Apr-14 3:08 
GeneralMy vote of 5 Pin
Volynsky Alex23-Mar-14 10:07
professionalVolynsky Alex23-Mar-14 10:07 
GeneralRe: My vote of 5 Pin
SOHAM GANDHI24-Mar-14 7:49
memberSOHAM GANDHI24-Mar-14 7:49 
GeneralRe: My vote of 5 Pin
Volynsky Alex24-Mar-14 8:15
professionalVolynsky Alex24-Mar-14 8:15 
QuestionBad Zip file.. Pin
joecousins18-Mar-14 12:35
memberjoecousins18-Mar-14 12:35 
AnswerRe: Bad Zip file.. Pin
SOHAM GANDHI18-Mar-14 19:31
memberSOHAM GANDHI18-Mar-14 19:31 
QuestionThanks... and looking forward.... Pin
Phebous17-Mar-14 13:53
memberPhebous17-Mar-14 13:53 
QuestionWhat compression system did you use? Pin
Dave Cross13-Mar-14 6:52
memberDave Cross13-Mar-14 6:52 
AnswerRe: What compression system did you use? Pin
SOHAM GANDHI13-Mar-14 7:04
memberSOHAM GANDHI13-Mar-14 7:04 
GeneralRe: What compression system did you use? Pin
Dave Cross17-Mar-14 4:46
memberDave Cross17-Mar-14 4:46 
GeneralRe: What compression system did you use? Pin
rather_b_sailing17-Mar-14 5:15
memberrather_b_sailing17-Mar-14 5:15 

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 | Terms of Use | Mobile
Web01 | 2.8.150901.1 | Last Updated 7 Apr 2014
Article Copyright 2014 by SOHAM_GANDHI
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid