安卓系统:视频的Chroma键性能



我有一个视频流,喜欢在上面应用色度键效果。我尝试了这个GPU库,但它比我自己的代码更慢:

public class ChromaKey {
    int[] pix;
    Bitmap bm;
    int picw, pich;
    int index, cur_pix, red2, green2, blue2;
    public Bitmap replaceIntervalColor(Bitmap bitmap,int red, int green, int blue)
    {
        if (bitmap != null)
        {
            picw = bitmap.getWidth();
            pich = bitmap.getHeight();
            if (pix == null)
            {
                pix = new int[picw * pich];
            }
            bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
            double distance;
            for (int y = 0; y < pich; y++) {
                for (int x = 0; x < picw ; x++) {
                    index = y * picw + x;
                    cur_pix = pix[index];
                    red2 = (int)((cur_pix & 0x00FF0000) >>> 16); // Color.red(cur_pix);
                    green2 = (int)((cur_pix & 0x0000FF00) >>> 8); //Color.green(cur_pix);
                    blue2 = (int)(cur_pix & 0x000000FF); //Color.blue(cur_pix);
                    // faster Math.sqrt
                    // Source: http://stackoverflow.com/a/13264441/956397
                    /* distance = Math.sqrt(
                            (red2 - red) * (red2 - red)
                                    + (green2 - green) * (green2 - green)
                                    + (blue2 - blue) * (blue2 - blue)
                    ); */
                    distance = Double.longBitsToDouble(((Double.doubleToRawLongBits( (red2 - red) * (red2 - red)
                            + (green2 - green) * (green2 - green)
                            + (blue2 - blue) * (blue2 - blue) ) >> 32) + 1072632448 ) << 31);
                    if (distance < 190)
                    {
                        pix[index] = Color.TRANSPARENT;
                    }
                }
            }
            if (bm == null)
            {
                bm = Bitmap.createBitmap(picw, pich, Bitmap.Config.ARGB_4444);
            }
            bm.setPixels(pix, 0, picw, 0, 0, picw, pich);
            return bm;
        }
        return null;
    }
}

如何提高此代码的性能?

我已经把所有的对象创建都移到了重用内存的位置,但在高端平板电脑上仍然很慢。

建议您将此操作移到本机库(C/C++)中。您可以将整个Bitmap对象传递给本机库函数,并修改Bitmap的内容,而无需来回复制像素。甚至可以应用汇编程序中的优化。

或者更简单地尝试优化代码。

最新更新