Ask Your Question
0

Unexpected behavior of the program

asked 2016-07-01 01:12:06 -0600

Maxim Krd gravatar image

updated 2016-07-01 01:37:09 -0600

I try to execute this fragment of code. On my reason he must do next: Find bg mask, count white pixels on it and if this count return <5 update stat_backgnd image. But if i run this code imshow show me that stat_backgnd update always. Whats wrong?

UMat small_raw_frame, tmp_backgnd, stat_backgnd;

capture.read(small_raw_frame)

stat_backgnd = small_raw_frame;

for(;;){

capture.read(small_raw_frame)

pMOG2->apply(small_raw_frame, tmp_backgnd);

int cnt =countNonZero(tmp_backgnd);

if ( cnt<= 5) stat_backgnd = small_raw_frame;

imshow("back", stat_backgnd);

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-07-01 03:18:53 -0600

berak gravatar image

this is an "aliasing" problem. if you assign like Mat b; Mat a=b; , this is only a "shallow" copy, both Mat's now point to the same pixels.

solution a, clone it:

if ( cnt<= 5) stat_backgnd = small_raw_frame.clone();

(btw, 5 seems to be far too small)

solution b, use local Mat's, not global ones:

UMat stat_backgnd;
capture.read(stat_backgnd);

for(;;){
    UMat small_raw_frame, fgmask;
    capture.read(small_raw_frame);

    pMOG2->apply(small_raw_frame, fgmask); // say what you mean, mean what you say ..

    int cnt =countNonZero(fgmask);
    if ( cnt<= 5000 ) stat_backgnd = small_raw_frame;

    imshow("fg", fgmask);
    imshow("back", stat_backgnd);
    if (waitKey(10) == 27) break;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-07-01 01:12:06 -0600

Seen: 62 times

Last updated: Jul 01 '16