Question

I am new to OpenCV/JavaCV and I am trying to implement MOG2 using JavaCV. However, I can't seem to get the foreground image. The background returns just fine. However, when I run apply() I do not get the foreground image. The code below is my current implementation. In order to compare the frame to the background, I compare each pixel from the background image to the current frame. However, I know MOG2 is supposed to return a foreground image.

import java.awt.Color;
import java.awt.image.BufferedImage;

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.FrameGrabber.Exception;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_video.BackgroundSubtractorMOG2;



public class Mog2Test {

/**
 * @param args
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    BackgroundSubtractorMOG2 mog=new BackgroundSubtractorMOG2(10, 10, false);

    FrameGrabber grabber= FrameGrabber.createDefault(0);
    grabber.start();

    IplImage foreground;
    IplImage background = null;
    IplImage crrntFrame;

    CanvasFrame frame = new CanvasFrame("Output");

    while(frame.isVisible() && (crrntFrame=grabber.grab())!=null){
        foreground=crrntFrame.clone();
        background=crrntFrame.clone();

        mog.apply(crrntFrame, foreground,.01);
        mog.getBackgroundImage(background);
        BufferedImage bfiBack=background.getBufferedImage();
        BufferedImage bficrrnt = crrntFrame.getBufferedImage();




        for(int x=0; x<bficrrnt.getWidth(); x++){
            for(int y=0; y<bficrrnt.getHeight(); y++){
            //  System.out.println(x+" "+y);
                Color cc = new Color(bficrrnt.getRGB(x, y));
                Color bgc = new Color(bfiBack.getRGB(x, y));

                int bgGray= (bgc.getRed()+bgc.getGreen()+bgc.getBlue())/3;
                int cGray=(cc.getRed()+cc.getBlue()+cc.getGreen())/3;

                if(!(cGray<bgGray+5 && cGray> bgGray-5)){
                    bficrrnt.setRGB(x, y, 0);
                }
            }
        }

        frame.showImage(bficrrnt);


    }

    grabber.stop();
    frame.dispose();

}

}

As you can see, I initialize the foreground variable by cloning the grabbed image. After running it through apply(), the foreground variable does not show any sign of editing. It displays the current frame as it was captured. What am I doing wrong? Thanks

Was it helpful?

Solution

Could you provide more information? What do you mean by you "can't seem to get the foreground image"? How do you realize that you "don't have it"? Is it empty? Is it just black when you try to display it?

I'm using C++, but also had an issue with the foreground image when I tried to display it along other images in one window. I first had to convert the image from gray- to rgb-colorspace (in C++ there is the method cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0) for this). Maybe trying this could help you, but maybe not since I don't know what your precise problem is ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top