How to do backgroundsub in the ROI of camera in android studio
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;}
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)
also, NO, not related to the imgproc module at all
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.
return framecopy;
-- didn't you want to returnfgMask
instead ?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.
hehe i see.
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;
Ok, i 'll try it when i get home, i feel you are right and i can make it this time, thanks ! Guru !