模糊屏幕截图中的矩形
Blurring a Rect within a screenshot
我正在开发一个 Android 应用程序,它使用背景 Service
以编程方式捕获当前屏幕上的任何内容的屏幕截图。我将屏幕截图作为 Bitmap
.
接下来,我成功地将 OpenCV 导入到我的 Android 项目中。
我现在需要做的是模糊此图像的 子集 ,即不是整个图像本身,而是图像中的 [矩形] 区域或子区域。我有一个 Rect
对象数组,代表我需要在屏幕截图中模糊的矩形区域。
我一直在 Java 中寻找关于使用 OpenCV 执行此操作的教程,但我没有找到明确的答案。 Mat
and Imgproc
classes are obviously the ones of interest, and there's the Mat.submat()
方法,但我一直找不到清晰、直接的教程来完成此操作。
我用谷歌搜索了很多,none 我找到的示例是完整的。我需要在 Java 运行时 Android 中执行此操作。
What I need is: Bitmap
>>> Mat
>>> Imgproc
>>> Rect
>>> Bitmap
with ROI
blurred.
这里有任何有经验的 OpenCV 开发人员,您能为我指明正确的方向吗?这是我唯一坚持的事情。
相关:
Gaussian blurring with OpenCV: only blurring a subregion of an image?.
How to blur a rectagle with OpenCv.
.
您可以只实现自己的辅助函数,我们称它为 roi(感兴趣区域)。
由于 opencv 中的图像是 numpy ndarrays,你可以这样做:
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]
并且只需使用这个辅助函数来提取您感兴趣的图像的子区域,对它们进行模糊处理,然后将结果放回原始图片。
完成此任务的 C++ 代码在下面与评论和示例图像共享:
// 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相当于:
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));
我正在开发一个 Android 应用程序,它使用背景 Service
以编程方式捕获当前屏幕上的任何内容的屏幕截图。我将屏幕截图作为 Bitmap
.
接下来,我成功地将 OpenCV 导入到我的 Android 项目中。
我现在需要做的是模糊此图像的 子集 ,即不是整个图像本身,而是图像中的 [矩形] 区域或子区域。我有一个 Rect
对象数组,代表我需要在屏幕截图中模糊的矩形区域。
我一直在 Java 中寻找关于使用 OpenCV 执行此操作的教程,但我没有找到明确的答案。 Mat
and Imgproc
classes are obviously the ones of interest, and there's the Mat.submat()
方法,但我一直找不到清晰、直接的教程来完成此操作。
我用谷歌搜索了很多,none 我找到的示例是完整的。我需要在 Java 运行时 Android 中执行此操作。
What I need is:
Bitmap
>>>Mat
>>>Imgproc
>>>Rect
>>>Bitmap
with ROI blurred.
这里有任何有经验的 OpenCV 开发人员,您能为我指明正确的方向吗?这是我唯一坚持的事情。
相关:
Gaussian blurring with OpenCV: only blurring a subregion of an image?.
How to blur a rectagle with OpenCv.
您可以只实现自己的辅助函数,我们称它为 roi(感兴趣区域)。 由于 opencv 中的图像是 numpy ndarrays,你可以这样做:
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]
并且只需使用这个辅助函数来提取您感兴趣的图像的子区域,对它们进行模糊处理,然后将结果放回原始图片。
完成此任务的 C++ 代码在下面与评论和示例图像共享:
// 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相当于:
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));