1

I'm quite new to OpenCV and having trouble using BackGround substration.

I want to recognize the figure of a person, having a picture of the background and the same picture with the person.

I tried the grab cut algorithm, the substraction also and none of it worked. Now I'm trying to use the background substraction, but I can't seem to understand it. Here's my code :

public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat PersonImage=Highgui.imread("boardWithMe.jpg");
    Mat Image = Highgui.imread("boardWithoutMe.jpg");
    BackgroundSubtractorMOG b = new BackgroundSubtractorMOG();
    Mat output = new Mat();
    b.apply(PersonImage, output,0.0);
    Highgui.imwrite("outputImage.jpg", output);
}

I always get a black picture. What should I change to make it work ? (By the way, I'm using openCV 2.4.13 so there is a constructor for BackgroundSubstractorMOG)

Dmitriy
  • 3,305
  • 7
  • 44
  • 55
  • Off the top of my head, I think it's detecting the whole image as the background. Have you tried playing around with the parameters in the constructor of `BackgroundSubtractorMOG`? More specifically, `nmixtures` and `backgroundRatio`. If I recall correctly, MOG subtractor is very data dependent and depending on the data, you'd need to tune the parameters to get the best results. Try `nmixtures = 4` and `backgroundRatio = 0.8` and see how it goes. – Maghoumi Apr 12 '17 at 13:49
  • Hey ! Thanks for your answer ! If I read the right documentation, using your advice, I should call the constructor as follow : new BackgroundSubtractorMOG(history,nmixtures, backgroundratio); what is the history parameter ? I tried : BackgroundSubtractorMOG b = new BackgroundSubtractorMOG(0,4, 0.8); And it didn't change a thing – Agnes Printemps Apr 12 '17 at 13:58
  • No problem. `history` is used when processing video frames. I think you can set it to 1. Here's a description of all the parameters: http://docs.opencv.org/2.4/modules/video/doc/motion_analysis_and_object_tracking.html#backgroundsubtractormog-backgroundsubtractormog – Maghoumi Apr 12 '17 at 14:01
  • It still isn't working. But I will try to play with the parameters you gave me. In the webpage you gave me, I saw that there exist the method $operator$, is there any difference with $apply$ that I'm using ? Could it be the root of the problem ? – Agnes Printemps Apr 12 '17 at 14:07
  • Since Java does not support operator overloading, my guess is that they replaced C++'s `operator()` with a regular method called `apply`. I don't think that's the problem. – Maghoumi Apr 12 '17 at 14:31

0 Answers0