Background subtraction not working in IOS

asked 2017-08-11 01:49:21 -0500

vijai gravatar image

This is my code to do the Background Subtraction, The looks like nothing happens, it shows what the camera shows and I dont see any subtracted foreground. And I am using version 3.2.0

My Code:

#import "CVWrapper_bs.hpp"
#include "stdio.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#import "opencv2/videoio/cap_ios.h"
#import "opencv2/video/background_segm.hpp"

@interface CVWrapper_bs () <CvVideoCameraDelegate>


@implementation CVWrapper_bs

UIImageView * imageView_i;
CvVideoCamera * videoCamera;
cv::Mat previousFrame;

cv::Ptr<cv::BackgroundSubtractor> bs;
cv::Mat fgmask1;

-(void)startCamera:(UIImageView *) cameraImageView
    //[GMMImageProcessingModule createMixtureModelWithType:GMMModelTypeKNN];
    bs = cv::createBackgroundSubtractorKNN();

    imageView_i = cameraImageView;
    videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView_i];
    videoCamera.delegate = self;
    videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
    videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
    videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
    videoCamera.defaultFPS = 20;
    videoCamera.rotateVideo = true;
    [videoCamera start];


- (void)processImage:(cv::Mat &)image

    bs->apply(image, fgmask1);
    dispatch_async(dispatch_get_main_queue(), ^{
        imageView_i.image = [self UIImageFromCVMat:fgmask1];

-(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
    NSData *data = [NSData length:cvMat.elemSize()*];
    CGColorSpaceRef colorSpace;

    if (cvMat.elemSize() == 1) {
        colorSpace = CGColorSpaceCreateDeviceGray();
    } else {
        colorSpace = CGColorSpaceCreateDeviceRGB();

    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);

    // Creating CGImage from cv::Mat
    CGImageRef imageRef = CGImageCreate(cvMat.cols,                                 //width
                                        cvMat.rows,                                 //height
                                        8,                                          //bits per component
                                        8 * cvMat.elemSize(),                       //bits per pixel
                                        cvMat.step[0],                            //bytesPerRow
                                        colorSpace,                                 //colorspace
                                        kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
                                        provider,                                   //CGDataProviderRef
                                        NULL,                                       //decode
                                        false,                                      //should interpolate
                                        kCGRenderingIntentDefault                   //intent

    // Getting UIImage from CGImage
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];

    return finalImage;

edit retag flag offensive close merge delete


Actually what makes you believe that this issue is due to OpenCV code. The background subtraction code is working just fine here. That makes me believe the issue is in your iOS specific code, and that is completely unrelated to the actual OpenCV source code.

StevenPuttemans gravatar imageStevenPuttemans ( 2017-08-11 02:48:53 -0500 )edit

Agreed, Till now I never thought the problem is with opencv , I was actually hoping that the way I might be using BS is in some way wrong. Please if anyone can point it to me would be very useful. I believe its something very tiny I am not able to see.

vijai gravatar imagevijai ( 2017-08-11 03:48:35 -0500 )edit