Thursday, December 20, 2012

image processing in opencv

http://xcodelovers.wordpress.com/2011/02/03/tutorial-integrating-codeblocks-and-opencv-2-1-0/




face detection
http://www.inf.unideb.hu/~sajolevente/papers/facedetection/




















http://myopencv.wordpress.com/2008/12/19/saving-a-part-of-a-video-in-another-video/#more-26
http://docs.opencv.org/doc/tutorials/highgui/video-write/video-write.html

http://opencv-srf.blogspot.in/2011/09/capturing-images-videos.html
http://learningopencv.wordpress.com/2011/05/29/example-2-2-playing-video-file/

http://answers.oreilly.com/topic/1366-how-to-write-to-an-avi-file-with-opencv/

http://bsd-noobz.com/opencv-guide/40-4-display-video-from-file-camera






Capturing Images & Videos


Simple OpenCV Program-Displaying an Image from File

Open Microsoft Visual Studio and create a new win32 console application. Then write the following program. If you have not install and configure OpenCV yet, please refer to Installing & Configuring with Visual Studio.

///////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <cv.h>
#include <highgui.h>

int main()
{
          IplImage* img = cvLoadImage("C:/MyPic.jpg");
          cvNamedWindow("MyWindow");
          cvShowImage("MyWindow", img);
          cvWaitKey(0);
          cvDestroyWindow("MyWindow");
          cvReleaseImage(&img);
          return 0;

}
///////////////////////////////////////////////////////////////////////////////////////

Before you run this program, put any image(MyPic.jpg) in C: drive.

You can download this visual c++ project from here(The downloaded file is a compressed .rar folder. So, you have to extract it using Winrar or other suitable software)

Result of above OpenCV Application


Explanation
  • #include "stdafx.h"
  • #include <cv.h>
  • #include <highgui.h>
These 3 lines are pre-processor directives to include some header files for your application. In almost every program you have to insert these 3 line at the top of your program.
  • IplImage* img = cvLoadImage("C:/MyPic.jpg");
IplImage is a data structure to store images in the memory.
cvLoadImage(...) is a function which loads an image from the location "C:/MyPic.jpg". Supported image formats of cvLoadImage function are BMP, DIB, JPEG, JPG, JPE, PNG, PBM, PGM, PPM, 
SR, RAS, TIFF and TIF.

By default, the loaded image is forced to be a 3-channel color image. This default can be modified by using a flag variable inside the cvLoadImage function. Then
IplImage* img = cvLoadImage("C:/MyPic.jpg" , flag);

if  flag: >0 the loaded image is forced to be a 3 channel color image
           =0 the loaded image is forced to be a 1 channel grayscale image
           <0 the loaded image is loaded with number of channels in the file

  • cvNamedWindow("MyWindow");
cvNamedWindow(...) function creates a window with the title of 'MyWindow'
  • cvShowImage("MyWindow", img); 
cvShowImage(..) function shows the image pointed by 'img' variable on the 'MyWindow' window.
  • cvWaitKey(0);
cvWaitKey(0) function wait for key press infinitely. If any key is pressed, this function returns the ASCII value of the key and your program will go to the next line. 
  • cvDestroyWindow("MyWindow");
This function destroy the opened window, with the title of "MyWindow"
  • cvReleaseImage(&img);
This function releases the memory allocated for the image , pointed by 'img' variable. After releasing the image, you cannot access the image, 'MyPic.jpg'. But you can use the variable 'img', for any other thing. If you create something, make sure you release it before terminating the program. This is probably the very first thing you should check when fixing memory leakage problems with OpenCV.

Summary
When running this program, the image of 'MyPic.jpg' is created in the memory and its memory address is loaded into the variable 'img'. Then a window named 'MyWindow' is opened. After that the image, pointed by 'img' variable is loaded to the window, 'MyWindow'. The window, 'MyWindow' with the image, 'MyPic.jpg' will be shown until any key is pressed.




Simple OpenCV Program-Creating a Blank Image

This program is also very much similar to the previous applictoion.

///////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <cv.h>
#include <highgui.h>

int main()
{

       IplImage* img = cvCreateImage(cvSize(800,500),IPL_DEPTH_8U,1);
       cvZero(img);
       cvNamedWindow("MyWindow");
       cvShowImage("MyWindow", img);
       cvWaitKey(0);
       cvDestroyWindow("MyWindow");
       cvReleaseImage(&img);
       return 0;
}

///////////////////////////////////////////////////////////////////////////////////////

You can download this visual c++ project from here. (The downloaded file is a compressed .rar folder. So, you have to extract it using Winrar or other suitable software)


Result of above OpenCV Application




New functions which are not found earlier are explained here
  • cvCreateImage(cvSize(800,500),IPL_DEPTH_8U,1);
This function create a 8 bit depth unsigned single-channel image which has a hight of 800 pixels and width of 500 pixels.

The first argument,  cvSize(800,500) defines the height and width of the image.

The 2nd argument, IPL_DEPTH_8U defines the bit_depth of the image. Here bit_depth is unsigned 8 bits.  Here 'U'is stands for 'unsigned'. For more clarification, visit OpenCV Basics.

The 3rd argument defines the number of channels. Here it is single channel. 

  • cvZero(img);
This function assigns zero to all pixel values of the image which is pointed by the 'img variable.

All other lines are same as the previous application.

Summary
In this application, it is created an image and assigned zero to all pixel values. Therefore we can see a black image as the result of the application.




Simple OpenCV Program-Playing a Video From a File

///////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>

int main()
{
     //load the video file to the memory
     CvCapture *capture =     cvCaptureFromAVI("C:/Wildlife.avi");

     if( !capture ) return 1;

     //obtain the frames per seconds of that video
     int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );

    //create a window with the title "Video"
    cvNamedWindow("Video");

    while(true) {
             //grab and retrieve each frames of the video sequencially 
             IplImage* frame = cvQueryFrame( capture );

             if( !frame ) break;

             //show the retrieved frame in the "Video" window
             cvShowImage( "Video", frame );


             int c; 


             
if(fps!=0){  
                     //wait for 1000/fps milliseconds
                     c = cvWaitKey(1000/fps);
            }else{
                     //wait for 40 milliseconds
                      c = cvWaitKey(40);
            } 



          //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27) 
            if((char)c==27 ) break;
   }

   //destroy the opened window
   cvDestroyWindow("Video");   
   //release memory

   cvReleaseCapture( &capture );

    
return 0;

}
///////////////////////////////////////////////////////////////////////////////////////

You can download this visual c++ project from here. (The downloaded file is a compressed .rar folder. So, you have to extract it using Winrar or other suitable software)
 


New functions which are not found earlier are explained here
  • cvCaptureFromAVI("C:/Wildlife.avi");
The function allocates and initializes the CvCapture structure for reading the video stream from the specified AVI file, "C:/Wildlife.avi" (Other video formats also can be used, but they are not optimized e.g.- wmv,dat,mpg).
  • cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
Returns the frames per second of the captured video. 
If  this function fails to get frames per second, it will return zero

For the first argument, it should be given a video capturing structure.

For the 2nd argument, there are few options.
They are
    • CV_CAP_PROP_FPS                      - gives the frame rate
    • CV_CAP_PROP_FRAME_WIDTH      - gives the width of frames in the video steam
    • CV_CAP_PROP_FRAME_HEIGHT     - gives the height of frames in the video stream
    • CV_CAP_PROP_FRAME_COUNT      - gives the number of frames in the video stream
    • CV_CAP_PROP_POS_MSEC            - gives the current position in miliseconds in the running video stream
    • CV_CAP_PROP_POS_FRAMES         - gives the 0-based index of the frame to be captured next  
This method can be used only for capturing from file. It cannot be used for capturing from cameras.
  •  cvQueryFrame( capture );
The function grabs the next frame from the captured video and returns the pointer. The returned image should not be released by the user.

  • cvWaitKey(1000/fps);
The function waits for 1000/fps milliseconds. If a key was pressed before the specified time, it returns the ASCII value of the pressed key and the program will go to the next line. If any key is not pressed, the function returns -1, after waiting for 1000/fps milliseconds.
If cvGetCaptureProperty( capture, CV_CAP_PROP_FPS ) function returns zero, cvWaitKey(40) statement will execute. 

  • cvReleaseCapture( &capture ); 
The function releases the CvCapture structure allocated by cvCaptureFromAVI function.

Important Note:
User should never release images, returned by cvQueryFrame function because it will cause a run time error.


Summary
At first, this program captures a video from a file. Then program enters into a infinite loop. In that loop, it grabs frames from the captured video sequentially, shows in a window and wait for 1000/fps milliseconds. 
Using cvWaitKey function is very important because cvShowImage function need time to show the image and cvWaitkey will give that necessary time. And waiting 1000/fps milliseconds will make sure the frame rate of output video is same as the original frame rate.

 



Simple OpenCV Program-Playing a Video From a Webcam or Camera

The only difference of the following program from the above program is the first line after the main function.

///////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <cv.h>
#include <highgui.h>

int main()
{
     //initialize and allocate memory to load the video stream from camera 
     CvCapture *capture = cvCaptureFromCAM(0);

     if( !capture ) return 1;

    //create a window with the title "Video"
    cvNamedWindow("Video");

    while(true) {
             //grab and retrieve each frames of the video sequentially 
             IplImage* frame = cvQueryFrame( capture );

             if( !frame ) break;

             //show the retrieved frame in the "Video" window
             cvShowImage( "Video", frame );


             //wait for 40 milliseconds
             int c = cvWaitKey(40);
       
          //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27) 
            if((char)c==27 ) break;
   }

   //destroy the opened window
   cvDestroyWindow("Video");   
   //release memory

   cvReleaseCapture( &capture );

    
return 0;

}
///////////////////////////////////////////////////////////////////////////////////////

You can download this visual c++ project from here(The downloaded file is a compressed .rar folder. So, you have to extract it using Winrar or other suitable software)


New functions which are not found earlier are explained here 
  • cvCaptureFromCAM(0);
The function  allocates and initialized the CvCapture structure for reading a video stream from the camera. Here the '0' means the index of the camera to be used. You can use 1,2,3.. instead of 0, if your computer is attached to more than 1 camera. If there is only one camera or it does not matter what camera to use,  -1 can be passed as the index.
  
  • cvWaitKey(40) 
cvGetCaptureProperty( capture, CV_CAP_PROP_FPS ) cannot be used for capturing from cameras. Therefore we cannot obtain the frame rate of video stream. Therefore we have to put a custom value for the parameter ofcvWaitKey function. You can use any value more than 20 for that parameter. But you must try different values for that parameter and see the difference.




No comments:


Post a Comment









http://myopencv.wordpress.com/2008/12/19/saving-a-part-of-a-video-in-another-video/#more-26
http://docs.opencv.org/doc/tutorials/highgui/video-write/video-write.html
http://learningopencv.wordpress.com/2011/05/29/example-2-2-playing-video-file/

http://www.daniweb.com/software-development/cpp/threads/289072/capturing-and-displaying-video-with-opencv

http://www.codeproject.com/Articles/339206/An-Introduction-to-OpenCV-Displaying-and-Manipulat

http://nashruddin.com/how_to_play_avi_files_with_opencv

http://www.codeproject.com/Articles/23191/Face-and-Eyes-Detection-Using-OpenCV

http://www.cognotics.com/opencv/servo_2007_series/part_2/sidebar.html

http://www.cs.princeton.edu/courses/archive/fall08/cos429/CourseMaterials/Precept1/facedetect.pdf

https://gist.github.com/231696

http://stackoverflow.com/questions/13155298/face-detection-sample-with-opencv-2-4-2-visual-studio-2012-windows-7-64-bit


Example 2.2 – Playing video file

OK, let’s move on to next program example in book “Learning OpenCV“. After simple program to display image, now we write simple program to play a video file.
The idea of video player is we grab each frame/image then display it in certain rate. In this example we just give certain fix rate, not read the rate information from file. We not grab audio also, just image.
We also want a way to terminate the playing, so we will use cvWaitKey as delay (in term of rate) and evaluate the return value to check whether stop key (we chose ESC button, ascii 27 decimal) was pressed.
01/* play a video file */
02 
03#include <highgui.h>
04 
05int main(int argc, char** argv) {
06    /* Create a window */
07    cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
08    /* capture frame from video file */
09    CvCapture* capture = cvCreateFileCapture( argv[1]);
10    /* Create IplImage to point to each frame */
11    IplImage* frame;
12    /* Loop until frame ended or ESC is pressed */
13    while(1) {
14        /* grab frame image, and retrieve */
15        frame = cvQueryFrame(capture);
16        /* exit loop if fram is null / movie end */
17        if(!frame) break;
18        /* display frame into window */
19        cvShowImage("Example2", frame);
20        /* if ESC is pressed then exit loop */
21        char c = cvWaitKey(33);
22        if(c==27) break;
23    }
24    /* destroy pointer to video */
25    cvReleaseCapture(&capture);
26    /* delete window */
27    cvDestroyWindow("Example2");
28 
29    return EXIT_SUCCESS;
30}

Saving a part of a video in another video

Filed under: OpenCV — Abhijeet @ 1:02 pm 
Tags: , 
 
 
 
 
 
 
1 Votes

Before you come and read this post i sincerely request you to first read the other  post which speaks how to convert a collection of images into a video….if you already know the same read on….
This post speaks about how to save a part of a video in another video….

Here we will use the same concept as with the other post which used the concept of the “fps”
01#include "cv.h"
02#include "highgui.h"
03#include “stdio.h”
04 
05int main( ) {/*As i have already told you that you can include the arguments to the main
06if you wish but i want to program without the arguments*/
07/*Here instead of initializing capture from the camera we initialize the capture from the file...video file..
08..everything else is the same...
09we grab the frames serially one after the
10other and then we treat everything else to be the same...*/
11 
12CvCapture* capture = cvCreateFileCapture( "filename.avi" );
13if (!capture){printf("unable to load a video");cvWaitKey(0);
14exit(0);
15 
16}
17IplImage* frame;
18double fps = cvGetCaptureProperty (/*Here we are trying
19 to acquire the frame rate of the video*/
20capture,
21CV_CAP_PROP_FPS);
22 
23printf("fps=%d n",(int)fps);
You may copy and past the code once again from the next highlighting..
Here with the help cvGetCaptureproperty we are trying to acquire the widht and height of the video… and store it in CvSize structure….Please do not get overwhelmed…
treat CvSize structure as a normal easy to understand structure and read on…
When we drive a car we do not care what part of the Engine is doing what…? we just want to go somewhere
turn on the Engine and move…similarly…just pass some time with the programs
you will get to understand them soon
When we were in kindergaten ABCD’s used to scare us but now we use them liberally…we will get to the same position in any field provided we spend some time with the same…..
01CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
02(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)
03);
04printf("frame (w, h) = (%d, %d)n",size.width,size.height);
05 
06/*Creating the Video writer If you do not know about the Video writer study this post and come back to tis line*/
07CvVideoWriter* writer = cvCreateVideoWriter(
08"partofvideo.avi",/*filename*/
09 
10CV_FOURCC('D','I','V','X'), /*codec type*/
11 
12fps,/*frames per second*/
13 
14size/*Cvsize struct...width and height*/
15 
16);/*This is the most important loop...here we initialize the
17counter variable which has something to do with the fps*/
18int counter=0;
19while( (frame=cvQueryFrame(capture)) != NULL ) { /*While frames are not null keep capturiung*/ counter++;
20/*discard all the frames before a certain time and accept the frames in a certain time period and write the frames in the new video*/
21 
22if(counter>230 && counter<=700){printf("entered");
23cvWriteFrame( writer, frame );/*create the video in that time period*/}
24}
25 
26/*Releasing all the structures.....*/
27cvReleaseVideoWriter( &writer );
28cvReleaseImage( &frame );
29 
30cvReleaseCapture( &capture );
31 
32}
33 
34/*This program was as simple as practicing programs for C language*/
35/*always enjoy programming in OpenCV*/







Tutorial integrating Code::Blocks and OpenCV 2.1.0

No comments:

Post a Comment