Blurring a Rect within a screenshot

蹲街弑〆低调 提交于 2020-08-04 05:36:29

问题


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?.


回答1:


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:

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

subimg:

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

blurred_subimage:

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

img:

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));



回答2:


You could just implement your own helper function, let's call it roi (region of interest). Since images in opencv are numpy ndarrays, you can do something like this:

def roi(image: np.ndarray, region: QRect) -> np.ndarray:
    a1 = region.upperLeft().x()
    b1 = region.bottomRight().y()
    a2 = region.upperLeft().x()
    b2 = region.bottomRight().y()
    return image[a1:a2, b1:b2]

And just use this helper function to extract the subregions of the image that you are interested, blur them and put the result back on the original picture.



来源:https://stackoverflow.com/questions/59966768/blurring-a-rect-within-a-screenshot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!