gpt4 book ai didi

ios - 如何使用openCV检测实时摄像机中的移动物体

转载 作者:可可西里 更新时间:2023-11-01 06:20:35 26 4
gpt4 key购买 nike

我正在为我的 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 的新手,所以我不知道该怎么做。

最佳答案

将此代码添加到 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);
}

您需要将以下声明为全局变量

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了解更多信息。

关于ios - 如何使用openCV检测实时摄像机中的移动物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30952072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com