How to detect a moving object in a live video camera using openCV

I am using openCV for my ios application to detect a moving object in a live video camera, but I am not familiar with using openCV, please help me. any other way of doing this is 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 to openCV so I don't know what to do.

+3


source to share


2 answers


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 need to declare the following 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;

      

See this url for more information .

+10


source


I assume you want something similar to this demo: https://www.youtube.com/watch?v=UFIVCDDnrmM This gives you motion and removes the background of the image.

Anytime you want to detect movement, you often have to start by identifying what is moving and what else is. This task is completed by taking multiple frames and comparing them to determine if the partitions are different so that they are considered not in the background. (Usually above a certain consistency threshold.)

To find the background in an image check: http://docs.opencv.org/master/d1/dc5/tutorial_background_subtraction.html



Afterword you'll need an implementation (maybe an edge detector like canny) to recognize the object you want and track its movement. (Determine speed, direction, etc.) This will be specific to your use case and there is not enough information in the original question to tell you specifically what you need here.

Another really good resource to look out for: http://www.intorobotics.com/how-to-detect-and-track-object-with-opencv/ This even has a section dedicated to using mobile devices with opencv ... (The use case used is robotics, one of the largest computer vision applications, but you should be able to use it for whatever you want it to do, albeit with a few modifications.)

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

+1


source







All Articles