android - 在 Android 上模糊图像中的区域(在 Java 中)

标签 android opencv

在我的 Android 应用程序中,我正在以编程方式从后台服务捕获屏幕截图。我以位图的形式获取它。

接下来,我使用以下 Android 框架 API 获取感兴趣区域 (ROI) 的坐标:

Rect ROI = new Rect();
viewNode.getBoundsInScreen(ROI);

在这里,getBoundsInScreen()在 Android 中相当于 Javascript 函数 getBoundingClientRect() .

一个Rect在Android中具有以下属性:

rect.top
rect.left
rect.right
rect.bottom
rect.height()
rect.width()
rect.centerX()    /* rounded off to integer */
rect.centerY()
rect.exactCenterX()    /* exact value in float */
rect.exactCenterY()

What does top, left, right and bottom mean in Android Rect object

Rect在OpenCV中具有以下属性

rect.width
rect.height
rect.x    /* x coordinate of the top-left corner  */
rect.y    /* y coordinate of the top-left corner  */

现在,在执行任何 OpenCV 相关操作之前,我们需要将 Android Rect 转换为 OpenCV Rect

Understanding how actually drawRect or drawing coordinates work in Android

有两种方法可以将 Android 矩形 转换为 OpenCV 矩形(如 Karl Phillip 在他的回答中所建议的)。两者生成相同的值并产生相同的结果:

/* Compute the top-left corner using the center point of the rectangle. */
int x = androidRect.centerX() - (androidRect.width() / 2);
int y = androidRect.centerY() - (androidRect.height() / 2);

// OR simply use the already available member variables:
x = androidRect.left;
y = androidRect.top;

int w = androidRect.width();
int h = androidRect.height();

org.opencv.core.Rect roi = new org.opencv.core.Rect(x, y, w, h);

现在我正在执行的 OpenCV 操作之一是 blurring the ROI within the screenshot :

Mat originalMat = new Mat();
Bitmap configuredBitmap32 = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(configuredBitmap32, originalMat);
Mat ROIMat = originalMat.submat(roi).clone();
Imgproc.GaussianBlur(ROIMat, ROIMat, new org.opencv.core.Size(0, 0), 5, 5);
ROIMat.copyTo(originalMat.submat(roi));

Bitmap blurredBitmap = Bitmap.createBitmap(originalMat.cols(), originalMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(originalMat, blurredBitmap);

This brings us very close to the desired result. Almost there, but not quite. The area just BENEATH the targeted region is blurred.

例如,如果目标感兴趣区域是密码字段,则上述代码会产生以下结果:

左侧为Microsoft Live 投资返回率,右侧为Pinterest 投资返回率:

可以看出,ROI下方的区域变得模糊。

So my question is, finally, why isn't the exact region of interest blurred?

  • The co-ordinates obtained through the Android API getBoundsInScreen() appear to be correct.
  • Converting an Android Rect to an OpenCV Rect also appears to be correct. Or is it?
  • The code for blurring a region of interest also appears to be correct. Is there another way to do the same thing?

注意:我已经提供了实际的全尺寸屏幕截图。为了适应这篇文章,它们已经缩小了 50%,但除此之外,它们与我在 Android 设备上得到的一模一样。

最佳答案

如果我没记错的话,OpenCV的Rect假设xy指定矩形的左上角:

/* Compute the top-left corner using the center point of the rectangle
 * TODO: take care of float to int conversion
 */
int x = androidRect.centerX() - (androidRect.width() / 2);
int y = androidRect.centerY() - (androidRect.height() / 2);

// OR simply use the already available member variables:
x = androidRect.left;
y = androidRect.top;

int w = androidRect.width();
int h = androidRect.height();

org.opencv.core.Rect roi = new org.opencv.core.Rect(x, y, w, h);

关于android - 在 Android 上模糊图像中的区域(在 Java 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60333766/

相关文章:

python - OpenCV 的 label2rgb 实现

android - Android 版 OpenCV 中的不兼容垫

opencv - 指定openCV中的读取路径

java - android 计费 :4. 0.0 - queryPurchases(INAPP) 和 purchase.getSku()

java - Android如何按顺序为对象设置动画

java - 从非存储库目录将更改加载到 Git

android - Xamarin.Android 更改单个 EditText 下划线颜色

java - 如果调用取消或停止相同的功能,然后重新开始(如果当前在 Android 中运行)

objective-c - 如何在 objective-c 中将 cvMat 转换为 UIImage?

c - 如何将 IplImage 转换为 char 数组