Search code examples
androidopencvandroid-bitmapgaussianblur

Blurring a Rect within a screenshot


I'm developing an Android app which uses a background Service to programmatically capture a screenshot of whatever is on the screen currently. I obtain the screenshot as a Bitmap.

Next, I successfully imported OpenCV into my Android project.

What I need to do now is blur a subset of this image, i.e. not the entire image itself, but a [rectangular] area or sub-region within the image. I have an array of Rect objects representing the rectangular regions that I need to blur within the screenshot.

I've been looking around for a tutorial on doing this with OpenCV in Java, and I haven't found a clear answer. The Mat and Imgproc classes are obviously the ones of interest, and there's the Mat.submat() method, but I've been unable to find a clear, straightforward tutorial on getting this done.

I've googled a lot, and none of the examples I've found are complete. I need to do this in Java, within the Android runtime.

What I need is: Bitmap >>> Mat >>> Imgproc>>> Rect >>> Bitmap with ROI blurred.

Any experienced OpenCV devs out here, can you point me in the right direction? This is the only thing I'm stuck at.

Related:

Gaussian blurring with OpenCV: only blurring a subregion of an image?.

How to blur a rectagle with OpenCv.

How to blur some portion of Image in Android?.


Solution

  • The C++ code to achieve this task is shared below with comments and sample images:

    // load an input image
    Mat img = imread("C:\\elon_tusk.png");
    

    img:

    enter image description here

    // extract subimage
    Rect roi(113, 87, 100, 50);
    Mat subimg = img(roi);
    

    subimg:

    enter image description here

    // blur the subimage
    Mat blurred_subimage;
    GaussianBlur(subimg, blurred_subimage, Size(0, 0), 5, 5);
    

    blurred_subimage:

    enter image description here

    // copy the blurred subimage back to the original image
    blurred_subimage.copyTo(img(roi));
    

    img:

    enter image description here

    Android equivalent:

    Mat img = Imgcodecs.imread("elon_tusk.png");
    Rect roi = new Rect(113, 87, 100, 50);
    Mat subimage = img.submat(roi).clone();
    Imgproc.GaussianBlur(subimg, subimg, new Size(0,0), 5, 5);
    subimg.copyTo(img.submat(roi));