将YUV字节数组图像调整为特定的宽度和高度

时间:2017-03-18 10:29:21

标签: java android arrays image image-processing

我正在尝试找到一种方法,方法或种类的算法来将YUV图像缩小到特定的宽度和高度值而不将其转换为RGB,只需操纵byte[] YUV image

我刚刚发现了另一个关于此问题的主题Resize (downsize) YUV420sp image

我看到实现这一目标的方法是去除像素,但我总是可以使用因子4,因为色度像素在4个亮度像素之间共享。

enter image description here

经过研究我只是实现了下一个方法,它将YUV image重新缩放比原始图像小四倍,但是我希望实现从Width x Height resolution自由转换为更小的{I}想要,而不是4的因素。有可能以某种方式实现这一目标吗?我在使用Renderscript或任何类型的库时都没有问题。

/**
     * Rescale a YUV image four times smaller than the original image for faster processing.
     *
     * @param data        Byte array of the original YUV image
     * @param imageWidth  Width in px of the original YUV image
     * @param imageHeight Height in px of the original YUV image
     * @return Byte array containing the downscaled YUV image
     */
    public static byte[] quadYuv420(byte[] data, int imageWidth, int imageHeight) {
        Log.v(TAG, "[quadYuv420] receiving image with " + imageWidth + "x" + imageHeight);
        long startingTime = System.currentTimeMillis();
        byte[] yuv = new byte[imageWidth / 8 * imageHeight / 8 * 3 / 2];
        // process Y component
        int i = 0;
        for (int y = 0; y < imageHeight; y += 8) {
            for (int x = 0; x < imageWidth; x += 8) {
                yuv[i] = data[y * imageWidth + x];
                i++;
            }
        }
        // process U and V color components
        for (int y = 0; y < imageHeight / 2; y += 8) {
            for (int x = 0; x < imageWidth; x += 16) {
                if (i < yuv.length) {
                    yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
                    i++;
                    yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x + 1)];
                    i++;
                }
            }
        }
        Log.v(TAG, "[quadYuv420] Rescaled YUV420 in " + (System.currentTimeMillis() - startingTime) + "ms");
        return yuv;
    }

1 个答案:

答案 0 :(得分:2)

看一下libyuv https://chromium.googlesource.com/libyuv/libyuv/

您可能需要编写一个jni包装器并使用项目中包含的转换函数将yuv转换为平面 - https://chromium.googlesource.com/libyuv/libyuv/+/master/include/libyuv/convert.h

相关问题