From the code below you can notice details in the code. Also, I implement the background Subtraction
using a two stage of erosion and dilation in the end of the processing.
#ifdef _CH_
#pragma package <opencv>
#endif
#define CV_NO_BACKWARD_COMPATIBILITY
#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <ctype.h>
#endif
IplImage *image = 0, *frameTime1=0, *frameTime2=0, *frameForeground=0, *img1=0, *img2=0;
int main( int argc, char** argv )
{
printf("Press ESC to Close.\n");
CvCapture* capture = 0; //Video capturing structure
capture = cvCaptureFromCAM( -1 ); //Initializes capturing a video from a camera
if( !capture )
{
fprintf(stderr,"Could not initialize capturing...\n");
return -1;
}
cvNamedWindow( "Camera", 1 ); //create the window for the Camera Output (Directly)
cvNamedWindow( "frameForeground", 1 );
while(1)
{
IplImage* frame = 0; //every time create/initialize an image (which name is frame) to process
int c; //integer to exit program
frame = cvQueryFrame( capture ); //grabs and returns a frame from a camera input
if( !frame ) //if there is no frame exit the while(1)
break;
if( !image ) //if there is no image, do the followings
{
/* allocate all the buffers */
image = cvCreateImage( cvGetSize(frame), 8, 3 );
frameTime1 = cvCreateImage( cvGetSize(frame), 8, 1 );
frameTime2 = cvCreateImage( cvGetSize(frame), 8, 1 );
frameForeground = cvCreateImage( cvGetSize(frame), 8, 1 );
img1 = cvCreateImage( cvGetSize(frame), 8, 1 );
img2 = cvCreateImage( cvGetSize(frame), 8, 1 );
}
cvCopy( frame, image, 0 );
cvCvtColor( image, img1, CV_BGR2GRAY );
cvCopy( img1, frameTime1, 0 ); //currently frame in grayscale
cvAbsDiff(
frameTime1,
frameTime2,
frameForeground
);
cvThreshold(
frameForeground,
frameForeground,
10,
255,
CV_THRESH_BINARY);
cvErode(
frameForeground,
frameForeground,
0,
1);
cvDilate(
frameForeground,
frameForeground,
0,
1);
cvDilate(
frameForeground,
frameForeground,
0,
1);
cvErode(
frameForeground,
frameForeground,
0,
1);
cvShowImage( "Camera", image ); //displays the image in the specified window
cvShowImage( "frameForeground", frameForeground );
cvCopy( frameTime1, frameTime2, 0 );
c = cvWaitKey(10); //waits for a pressed key
if( (char) c == 27 ) //if key==ESC (27 ESC button) then break
break;
}
cvReleaseCapture( &capture ); //Releases the CvCapture structure
cvDestroyWindow("Camera");
cvDestroyWindow("frameForeground");
return 0;
}
#ifdef _EiC
main(1,"camshiftdemo.c");
#endif
Snapshots of the processed video:
Thanks man this is awesome!!
ReplyDeletehow can i get the subtracted display in original RGB format?
ReplyDeleteYou have to search a little in the format conversion, I just now remember that is a little tricky these part of conversions. Great guide is the OpenCV book...
ReplyDeletethank u so much.......it was really help full
ReplyDeleteHey Guy,
ReplyDeleteyour code is very nice and works without any Problem.
please can you tell me how i can load a Video from file.
or how can i modify this code to build a difference between two pictures.
Thank you in advannce
I hope you rot in fucking hell.
ReplyDeletecan u tell me why u allocate the buffers
ReplyDeleteHi,
ReplyDeleteI want to apply this code for an image.What changes will I have to do? Please suggest me.
Thanks and I have a swell present: Who Repair House Windows house renovation tv shows
ReplyDelete