Ask Your Question
1

Problem background subtraction JAVA

asked 2017-09-30 11:10:17 -0600

mateusguilherme gravatar image

Hi

my background subtraction is not generating results...

@Override
public void run() {

    while (true) {


        Mat fgmask = new Mat();
        Mat copy = new Mat();
        frame.copyTo(copy); // webcam.read(frame)

        BackgroundSubtractorMOG2 bgsubtraction = Video.createBackgroundSubtractorMOG2();
        bgsubtraction.apply(copy, fgmask);

        int cols = fgmask.cols();
        int rows = fgmask.rows();
        int elemSize_mat = (int) fgmask.elemSize(); 

        byte[] data_mat = new byte[cols * rows * elemSize_mat];
        fgmask.get(0, 0, data_mat);

        BufferedImage imageBuffer = new BufferedImage(cols, rows, BufferedImage.TYPE_BYTE_GRAY);
        imageBuffer.getRaster().setDataElements(0, 0, cols, rows, data_mat);
        Icon icon = new ImageIcon(imageBuffer);
        this.jLabel_diferenca_entre_fotos.setIcon(icon);
        this.jLabel_diferenca_entre_fotos.repaint();

        try {
            Thread.sleep(30);
        } catch (InterruptedException ex) {

        }
    }
}

image description

what is the problem?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-09-30 11:50:03 -0600

berak gravatar image

updated 2017-09-30 12:11:47 -0600

please move this line in front of the while loop:

    BackgroundSubtractorMOG2 bgsubtraction = Video.createBackgroundSubtractorMOG2();

(it has to learn a model from several images, so it's useless, if you create a new instance for each frame)

edit flag offensive delete link more

Comments

hi

thanks

mateusguilherme gravatar imagemateusguilherme ( 2017-10-02 12:44:09 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-09-30 11:10:17 -0600

Seen: 1,035 times

Last updated: Oct 02 '17