0

I would like to subtract a background of an image usine OpenCV on iOS with Objective-C++ and I am currently facing a termination due to memory issue, whenever I want to apply the BackgroundSubtractorMOG2 method to the input pictures.

I was researching on the topic, however I haven't find any useful answers. I am applying the BackgroundSubtractorMOG2 method properly, passing the right arguments. I also have checked the Diagnostics in my Edit Scheme and my Zombie Objects are disabled, so that shouldn't cause the problem. I am thinking, whether the processing power of the phone could be the problem.

I am passing in my function a background image and and the same background image with a person in it. I would like to subtract the person from the image somehow.

My code is:

// Subtracts two images: the image taken from the background image
-(cv::Mat) subtractBackground:(cv::Mat)backPic fromImage:(cv::Mat)inImage {
if (!inImage.data) {
    NSLog(@"Unable to the picture image");
} else if (!backPic.data) {
    NSLog(@"Unable to open background");
}

cv::Mat fgMaskMOG2;

int history = 2;
int distance_threshold = 16;
bool shadow_detection = true;

cv::Mat frame[2] = { backPic, inImage};

cv::Ptr<cv::BackgroundSubtractorMOG2> createSubtractor = new cv::BackgroundSubtractorMOG2(history, distance_threshold, shadow_detection);

for(int i = 0; i < 2; i++) {
    createSubtractor->operator()(frame[i], fgMaskMOG2);
}

return fgMaskMOG2;
}
  • BackgroundSubtractor is for videos. You should simply subtract your two images with "cv::absdiff" – Miki Dec 07 '15 at 22:06
  • Hi Miki. Thank you for your kind reply. I have already tried with that too, however it doesn't work properly, so it leaves some parts of the background on the final result. Also I want to subtract shadows too, that is why I wanted to use the BackgroundSubtractorMOG2. Isn't it possible to use it for pictures too? I have already checked this question http://stackoverflow.com/questions/19221877/opencv-how-to-use-createbackgroundsubtractormog where the second part of the code refers to pictures. – user3815403 Dec 08 '15 at 09:07

0 Answers0