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