Thursday, October 3, 2013

Reading Image and Video OpenCV code


In the name of Allah who is the most beneficent and merciful.

IMAGE AND VIDEO READING CODE IN OPENCV2.3 WITH MICROSOFT VISUAL STUDIO 2010 C++

Before writing this code for image and video reading in OpenCV, you must first link openCV with Visual Studio 2010. For linking please follow the instructions on the following link:

Here is the code for reading an image and a video in Opencv 2.3 Microsoft Visual Studio 2010 C++:

#include "stdafx.h"
#include "highgui.h"
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>

#include <iostream>
#include <conio.h>

using namespace cv;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
       //Image Reading

       IplImage* img = cvLoadImage( "imagename.jpg" );
       cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
       cvShowImage( "Example1", img );
       cvWaitKey(0);
       cvReleaseImage( &img );
       cvDestroyWindow( "Example1" );

       //Video Reading
      
       cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
      
       CvCapture* capture = cvCreateFileCapture( "videoname.avi" );
       IplImage* frame;

       while(1) {
              frame = cvQueryFrame( capture );
              if( !frame ) break;
              cvShowImage( "Example2", frame );
              char c = cvWaitKey(33);
              if( c == 27 ) break;//if user enter escape key then exit
       }

       cvReleaseCapture( &capture );
       cvDestroyWindow( "Example2" );
}