Ios 如何利用openCV检测实时摄像机中的运动目标

Ios 如何利用openCV检测实时摄像机中的运动目标,ios,iphone,opencv,background-subtraction,Ios,Iphone,Opencv,Background Subtraction,我正在为我的ios应用程序使用openCV来检测实时摄像机中的移动对象,但我不熟悉openCV的使用,请帮助我。任何其他方法也欢迎 - (void)viewDidLoad { [super viewDidLoad]; self.videoCamera.delegate = self; self.videoCamera = [[CvVideoCamera alloc] initWithParentView:self.view]; self.videoCamera

我正在为我的ios应用程序使用openCV来检测实时摄像机中的移动对象,但我不熟悉openCV的使用,请帮助我。任何其他方法也欢迎

- (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);
}

我是openCV的新手,所以我不知道该怎么办。

我假设您想要与本演示类似的东西: 这将提供运动并删除图像的背景

每当你们想要检测运动时,你们常常必须首先确定什么是运动的,什么是静止的。这项任务是通过获取多个帧来完成的,并对它们进行比较,以确定各部分是否有足够的差异,从而被视为不在背景中。(通常超过一定的一致性阈值。)

要查找图像中的背景,请查看:

之后,您将需要一个实现(可能是像canny这样的边缘检测器),以便能够识别您想要的对象并跟踪其运动。(确定速度、方向等)这将特定于您的用例,而原始问题中没有足够的信息来具体说明您需要什么

另一个非常好的资源是: 这甚至有一节专门讨论如何利用opencv的移动设备。(他们使用的使用场景是机器人技术,这是计算机视觉的最大应用之一,但您应该能够根据需要使用它;尽管需要进行一些修改。)


请让我知道这是否有帮助,如果我可以做任何其他事情来帮助你

将此代码添加到
processImage
委托:

- (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);
}
您需要将以下内容声明为全局变量

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;
cv::Mat img,fgmask;
cv::Ptr bg_模型;
bool-update\u-bg\u模型;

如果你没有答案,请不要投反对票。你的摄像机在移动吗?没有像CCTV摄像机那样固定位置的摄像机@Micka运动检测的简单方法是帧差分和光流。这里有一个很棒的教程,其中包含代码:。我不确定这是否有用。祝你好运更新bg模型的逻辑是什么?。何时何地更新值(&W)