android - 将 Android 上使用 ML Kit 进行自拍分割的分割结果保存为具有透明背景的位图

标签 android android-bitmap google-mlkit

Android端ML Kit自拍分割结果保存为背景透明的Bitmap

我正在按照本教程和代码进行自拍分割

Here

我从教程中引用了这段代码

ByteBuffer mask = segmentationMask.getBuffer();
int maskWidth = segmentationMask.getWidth();
int maskHeight = segmentationMask.getHeight();

for (int y = 0; y < maskHeight; y++) {
  for (int x = 0; x < maskWidth; x++) {
    // Gets the confidence of the (x,y) pixel in the mask being in the foreground.
    float foregroundConfidence = mask.getFloat();
  }
}

生成掩码

然后我引用了生成紫色背景 mask 的示例应用

Here

使用这段代码

@ColorInt
  private int[] maskColorsFromByteBuffer(ByteBuffer byteBuffer) {
    @ColorInt int[] colors = new int[maskWidth * maskHeight];
    for (int i = 0; i < maskWidth * maskHeight; i++) {
      float backgroundLikelihood = 1 - byteBuffer.getFloat();
      if (backgroundLikelihood > 0.9) {
        colors[i] = Color.argb(128, 255, 0, 255);
      } else if (backgroundLikelihood > 0.2) {
        // Linear interpolation to make sure when backgroundLikelihood is 0.2, the alpha is 0 and
        // when backgroundLikelihood is 0.9, the alpha is 128.
        // +0.5 to round the float value to the nearest int.
        int alpha = (int) (182.9 * backgroundLikelihood - 36.6 + 0.5);
        colors[i] = Color.argb(alpha, 255, 0, 255);
      }
    }
    return colors;
  }

现在我想用检测到的原始图像生成一个图像蒙版并将其覆盖在透明图像上并为此保存该位图我正在使用此代码

 public Bitmap generateMaskBgImage(Bitmap image, Bitmap bg) {
//Bg is Transparent Png Image.
            Bitmap bgBitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), image.getConfig());
            for (int y = 0; y < maskHeight; y++) {
                for (int x = 0; x < maskWidth; x++) {
                    int bgConfidence = (int) ((1.0 - maskBuffer.getFloat()) * 255);
                    int bgPixel = bg.getPixel(x, y);
                    bgPixel = ColorUtils.setAlphaComponent(bgPixel, bgConfidence);
                    bgBitmap.setPixel(x, y, bgPixel);
                }
            }
            maskBuffer.rewind();
            return bitmapUtils.mergeBitmaps(image, bgBitmap);
        }

然而,它会生成一个具有所需蒙版但背景为黑色的图像,我们如何才能将该图像保存为透明背景。

最佳答案

你可以试试这个(color1 是你在 mask 中设置的颜色):

private Bitmap performBW(Bitmap originBitmap,Bitmap maskBitmap) {
    Bitmap bmOut = Bitmap.createBitmap(originBitmap.getWidth(), originBitmap.getHeight(),
            originBitmap.getConfig());
    int w = originBitmap.getWidth();
    int h = originBitmap.getHeight();
    int[] colors = new int[w * h];
    int[] colorsMask=new int[maskBitmap.getWidth() * maskBitmap.getHeight()];

    originBitmap.getPixels(colors, 0, w, 0, 0, w, h);
    maskBitmap.getPixels(colorsMask, 0, w, 0, 0, w, h);

    int pos;

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            pos = i * w + j;

            if (colorsMask[pos] == color1) colors[pos]=Color.TRANSPARENT; 
        }
    }
    bmOut.setPixels(colors, 0, w, 0, 0, w, h);
    return bmOut;
}

关于android - 将 Android 上使用 ML Kit 进行自拍分割的分割结果保存为具有透明背景的位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70955374/

相关文章:

android - 如何授予用户读取 Parse.com 表上对象的权限?

android - DrawerLayout 卡在滑动上

java - 在 x 和 y 方向重复时设置 BitmapDrawable 的偏移量?

android - ML Kit Vision 设备端文本识别未下载模型 : Waiting for the text recognition model to be downloaded. 请稍候

swift - 从 Firebase 下载/保存远程 ML 模型时出错

android - 如何在不弹出 fragment 的情况下清除 fragmentManager 返回堆栈?

java - 未检测到闪光灯

android - 如何在android中获取捕获图像的路径

android - Android中位图压缩后如何保存Exif数据

swift/MLKit : how to initialize TextRecognizer?