how to detect moving object in live video camera using openCV

4.7k views Asked by At

i am using openCV for my ios application to detect moving object in live video camera, but i am not familiar with use of openCV please help me. any other way to do this also welcome .

- (void)viewDidLoad {
    [super viewDidLoad];

    self.videoCamera.delegate = self;
    self.videoCamera = [[CvVideoCamera alloc] initWithParentView:self.view];

    self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
    self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
    self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
    self.videoCamera.defaultFPS = 30;
    self.videoCamera.grayscaleMode = NO;
    [self.videoCamera start];
//    cv::BackgroundSubtractorMOG2 bg;

}
- (void)processImage:(cv::Mat&)image
{
    //process here
    cv::cvtColor(image, img, cv::COLOR_BGRA2RGB);
    int fixedWidth = 270;
    cv::resize(img, img, cv::Size(fixedWidth,(int)((fixedWidth*1.0f)*   (image.rows/(image.cols*1.0f)))),cv::INTER_NEAREST);

    //update the model
   bg_model->operator()(img, fgmask, update_bg_model ? -1 : 0);


    GaussianBlur(fgmask, fgmask, cv::Size(7, 7), 2.5, 2.5);
    threshold(fgmask, fgmask, 10, 255, cv::THRESH_BINARY);

    image = cv::Scalar::all(0);
    img.copyTo(image, fgmask);
}

i am new at openCV so, i don't know what to do.

2

There are 2 answers

1
SemperFi On BEST ANSWER

Add this code to processImage delegate :

- (void)processImage:(cv::Mat&)image
{
      //process here
      cv::cvtColor(image, img, cv::COLOR_BGRA2RGB);
      int fixedWidth = 270;
     cv::resize(img, img, cv::Size(fixedWidth,(int)((fixedWidth*1.0f)*   (image.rows/(image.cols*1.0f)))),cv::INTER_NEAREST);

    //update the model
    bg_model->apply(img, fgmask, update_bg_model ? -1 : 0);

    GaussianBlur(fgmask, fgmask, cv::Size(7, 7), 2.5, 2.5);
    threshold(fgmask, fgmask, 10, 255, cv::THRESH_BINARY);

    image = cv::Scalar::all(0);
    img.copyTo(image, fgmask);
}

You'll need to declare following as global variable

cv::Mat img, fgmask;
cv::Ptr<cv::BackgroundSubtractor> bg_model;
bool update_bg_model;
Where, img <- smaller image fgmask <- the mask denotes that where motion is happening update_bg_model <- if you want to fixed your background;

Please refer this URL for further information.

0
Lilith Daemon On

I am assuming that you are wanting something of a similar nature to this demonstration: https://www.youtube.com/watch?v=UFIVCDDnrmM This gives you motion and removes the background of an image.

Anytime you are wanting to detect motion you often times must start by determining what is moving and what it still. This task is completed by taking several frames, and comparing them to determine if sections are different enough to be regarded as not being in the background. (Generally over a certain threshold of consistency.)

For finding the background in an image, check out: http://docs.opencv.org/master/d1/dc5/tutorial_background_subtraction.html

Afterwords you will need an implementation (potentially an edge detector like canny) to be able to recognize the object you are wanting and track its motion. (Determine velocity, direction, etc.) This will be specific to your use-case, and there isn't enough information in the original question to say specifically what you need here.

Another really good resource to look at would be: http://www.intorobotics.com/how-to-detect-and-track-object-with-opencv/ This even has a section dedicated to utilizing mobile devices with opencv. (The use scenario that they are using is robotics, one of the largest applications of computer vision, but you should be able to use it for what you need; albeit with a few modifications. )

Please let me know if this helps, and if I can do anything else to assist you.