PaddlePaddle / Paddle-Lite-Demo

lib, demo, model, data
Apache License 2.0
684 stars 286 forks source link

Android中人物分割的mask和原图融合问题 #342

Closed badboy-tian closed 1 year ago

badboy-tian commented 1 year ago

问题确认 Search before asking

请提出你的问题 Please ask your question

原图: 298be9d8ba445c79d641256df1622a8 推算图: 3645199d11f82c15543b677018131e3

合成后: 8ad7f8e73f296ce50463c9f317c6c33

合成的代码:

private Bitmap synthetizeBitmap(Bitmap front, Bitmap alpha) {
        int width = front.getWidth();
        int height = front.getHeight();
        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        int[] frontPixels = new int[width * height];
        //int[] backgroundPixels = new int[width * height];
        int[] alphaPixels = new int[width * height];
        front.getPixels(frontPixels, 0, width, 0, 0, width, height);
        //background.getPixels(backgroundPixels,0,width,0,0,width,height);
        alpha.getPixels(alphaPixels, 0, width, 0, 0, width, height);
        float frontA = 0, frontR = 0, frontG = 0, frontB = 0;
        float backgroundR = 0, backgroundG = 0, backgroundB = 0, backgroundA = 0;
        float alphaR = 0, alphaG = 0, alphaB = 0, alphaA = 0;
        int index = 0;

        //逐个像素赋值(这种写法比较耗时,后续可以优化)
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                index = width * row + col;

                //取出前景图像像素值
                frontA = (frontPixels[index] >> 24) & 0xff;
                frontR = (frontPixels[index] >> 16) & 0xff;
                frontG = (frontPixels[index] >> 8) & 0xff;
                frontB = frontPixels[index] & 0xff;

                //取出alpha像素值
                alphaA = (alphaPixels[index] >> 24) & 0xff;
                alphaR = (alphaPixels[index] >> 16) & 0xff;
                alphaG = (alphaPixels[index] >> 8) & 0xff;
                alphaB = alphaPixels[index] & 0xff;

                frontR = frontR * alphaR / 255;
                frontG = frontG * alphaG / 255;
                frontB = frontB * alphaB / 255;

                frontPixels[index] = (int) frontA << 24 | ((int) frontR << 16) | ((int) frontG << 8) | (int) frontB;
            }
        }
        result.setPixels(frontPixels, 0, width, 0, 0, width, height);
        return result;
    }

请问如何做才能把mask和原图合成后,只保留mask中人物抠图的部分,保留png呢 谢谢

代码来源: https://github.com/PaddlePaddle/PaddleSeg/blob/release%2F2.7/Matting/deploy/human_matting_android_demo/app/src/main/java/com/paddle/demo/matting/visual/Visualize.java