Ask Your Question
0

How to do backgroundsub in the ROI of camera in android studio

asked 2019-10-07 11:57:02 -0600

wade wang gravatar image

updated 2019-10-08 03:00:51 -0600

berak gravatar image

I am going to do a backgroundsubtractor in a ROI area on my phone's camera, my camera resolution is 1280*960, the ROI is x(10:1270),y(400:960). After i run the code on my mobilephone, the result is that the whole screen just shows the gray scale , not having a backgroundsub effect in the ROI. How to change the code, or is there any other better methods to realize the backgroundsub in the ROI? Many thanks !here is part of the code:

public static final int x = 10;
public static final int y = 400;
public static final int width = 1260;
public static final int height = 560;

……

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

    Mat frame = inputFrame.rgba();
    Mat fgMask = new Mat();
    Mat framecopy = frame.clone();
    Imgproc.cvtColor(framecopy, framecopy, Imgproc.COLOR_RGBA2GRAY);
    Mat ROI = framecopy.submat(y, y+height, x, x+width);
    backSub.apply(ROI, fgMask, -1);
    Imgproc.threshold(fgMask, fgMask, 127, 255, Imgproc.THRESH_BINARY);
    Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,new Size( 3, 3));
    Imgproc.morphologyEx(fgMask, fgMask, Imgproc.MORPH_CLOSE, kernel);
    Imgproc.morphologyEx(fgMask, fgMask, Imgproc.MORPH_OPEN, kernel);
    return framecopy;}
edit retag flag offensive close merge delete

Comments

wade, please understand, that it's a bad idea here to close a question, and open another with (almost) the same text -- basically, you're wasting anyone's effort to understand the 1st (or the difference)

berak gravatar imageberak ( 2019-10-07 12:53:23 -0600 )edit

also, NO, not related to the imgproc module at all

berak gravatar imageberak ( 2019-10-07 12:54:53 -0600 )edit
1

I am sorry for that, last night i closed the first question is because i change the code afterwards, the error disappeared, that is to say, the question changed, in order not to let others answer my previous question, i choose closing question because i thank it is quicker than reedit. And the topic maybe relate to Core.Mat. Anyway, All is because last night i was too hurried to post my question and not familiar with our forum. I promise such case will not happen again.

wade wang gravatar imagewade wang ( 2019-10-07 18:57:44 -0600 )edit

return framecopy; -- didn't you want to return fgMask instead ?

berak gravatar imageberak ( 2019-10-08 02:08:19 -0600 )edit

Yes, previously i write "return fgMask", i thought the screen could just show the fgMask in the ROI region, but it will report errors in the debugging, that was the problem i had in the beginning, so i try 'return framecopy' to show the full size of the camera's preview, in this way, although it doesn't report errors, the screen just have gray scale effect , no backgroundsub effect.

wade wang gravatar imagewade wang ( 2019-10-08 02:31:11 -0600 )edit
1

hehe i see.

i thought the screen could just show the fgMask in the ROI region,

you could copy the fgMask to the grayscale image, but you have to specify the ROI, where you want it (it's the same ROI you used before !),

like: fgMask.copyTo(ROI); return framecopy;

berak gravatar imageberak ( 2019-10-08 02:46:43 -0600 )edit

Ok, i 'll try it when i get home, i feel you are right and i can make it this time, thanks ! Guru !

wade wang gravatar imagewade wang ( 2019-10-08 04:17:39 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2019-10-08 06:09:44 -0600

wade wang gravatar image

Sir, your suggestion really works ! Now you can post your last comment as answer, and i will select it as correct ! Hahaha

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-10-07 11:57:02 -0600

Seen: 480 times

Last updated: Oct 08 '19