4

I am doing motion recognition of walking using openCV and C++ and I would like to create a mask or copied image in order to achieve the effect seen in the picture provided. extracted human.The following is an explanation of the images The resulting blob of the human walking is seen. Then, a mask image or copied image of the original frame is created, the binary human blob is now masked and the non-masked pixels are now set to zero. The result is the extracted human body with a black background. The diagram below shows how the human blob is extracted and then masked. This is to be done for every 5th frame of a video sequence. My code so far consists of getting every 5th frame, grayscaling it, finding the areas of all the blobs, and applying a threshold value to get a binary image where more or less, only the human blob is white and the rest of the image is black. Now, I am trying to extract the human body but I have no clue how to proceed. Please help me.

#include "cv.h"
#include "highgui.h"
#include "iostream"

using namespace std;
int main( int argc, char* argv ) {

CvCapture *capture = NULL;
capture = cvCaptureFromAVI("C:\\walking\\lady walking.avi");
if(!capture){
    return -1;
}

IplImage* color_frame = NULL;
IplImage* gray_frame = NULL ;

int thresh_frame = 28;
CvMoments moments;

int frameCount=0;//Counts every 5 frames
cvNamedWindow( "walking", CV_WINDOW_AUTOSIZE );

while(1) {
    color_frame = cvQueryFrame( capture );//Grabs the frame from a file
    if( !color_frame ) break;
    gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);
    if( !color_frame ) break;// If the frame does not exist, quit the loop


    frameCount++;
    if(frameCount==5)
    {
        cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
        cvThreshold(gray_frame, gray_frame, thresh_frame, 255, CV_THRESH_BINARY);
        cvErode(gray_frame, gray_frame, NULL, 1);
        cvDilate(gray_frame, gray_frame, NULL, 1);

        cvMoments(gray_frame, &moments, 1);
        double m00;
        m00 = cvGetCentralMoment(&moments, 0,0);

        cvShowImage("walking", gray_frame);
        frameCount=0;
    }
    char c = cvWaitKey(33);
    if( c == 27 ) break;
}

double m00 = (double)cvGetCentralMoment(&moments, 0,0);
cout << "Area - : " << m00 << endl;
//area of lady walking = 39696. Therefore, using new threshold area as 30 for this video
//area of walking man = 67929

cvReleaseImage(&color_frame);
cvReleaseImage(&gray_frame);
cvReleaseCapture( &capture );
cvDestroyWindow( "walking" );

return 0;
}

I would also like to upload the video that I am using in the code but I don't know how to upload it here, so if anyone can help me out with that too. I want to provide as much info as possible w.r.t. my question.

jilles de wit
  • 7,060
  • 3
  • 26
  • 50
sue-ling
  • 399
  • 2
  • 11
  • 21
  • 2
    On a side note, break should never be used that way! use do{} while and make your checks there. – Martin Kristiansen Feb 21 '12 at 12:21
  • @MartinKristiansen I'm new to openCV and C++ so once the code worked, I was happy with that. is that break not acceptabble for use there? – sue-ling Feb 21 '12 at 16:12
  • it makes the controlflow allot less transparent, figuring out when the while loop is exited is really not clear and will get a lot less clear if the code grows. – Martin Kristiansen Feb 22 '12 at 09:18

1 Answers1

1

the easiest way is to look for the biggest blob in the image (cvfind contours can be the function you need), then you set to blac all the other blobs (scannig all the contours and using cvfloadfill). finally you scan the entire binary image if the considered pixel is white you do nothing, if the pixel is black you set to black the corresponding pixel of the 5th frame

andrea
  • 1,326
  • 7
  • 31
  • 60
  • so I wouldn't need to install a library called cvBlobLib? When researching how to extract the blob, I came across the mention of it several times but neither the O'Reilly nor the openCV reference manual had anything about it so I was a bit doubtful as to using it. I would really prefer your method of contours instead of resorting to that cvBlobLib. I really hope this works – sue-ling Feb 21 '12 at 16:18
  • 1
    I'm not sure about that, but I think CvBlob has been included in Opencv in the last versions. (still check it) – andrea Feb 21 '12 at 19:50