【问题标题】:Converting YUV->RGB(Image processing)->YUV during onPreviewFrame in android?在android的onPreviewFrame期间转换YUV-> RGB(图像处理)-> YUV?
【发布时间】:2012-03-08 17:04:59
【问题描述】:

我正在使用 SurfaceView 捕获图像并在 public void onPreviewFrame4(byte[] data, Camera camera)中获取 Yuv Raw 预览数据

我必须在 onPreviewFrame 中执行一些图像预处理,因此我需要将 Yuv 预览数据转换为 RGB 数据,而不是图像预处理并返回到 Yuv 数据。

我已经使用这两个函数将 Yuv 数据编码和解码为 RGB,如下所示:

public void onPreviewFrame(byte[] data, Camera camera) {
    Point cameraResolution = configManager.getCameraResolution();
    if (data != null) {
        Log.i("DEBUG", "data Not Null");

                // Preprocessing
                Log.i("DEBUG", "Try For Image Processing");
                Camera.Parameters mParameters = camera.getParameters();
                Size mSize = mParameters.getPreviewSize();
                int mWidth = mSize.width;
                int mHeight = mSize.height;
                int[] mIntArray = new int[mWidth * mHeight];

                // Decode Yuv data to integer array
                decodeYUV420SP(mIntArray, data, mWidth, mHeight);

                // Converting int mIntArray to Bitmap and 
                // than image preprocessing 
                // and back to mIntArray.

                // Encode intArray to Yuv data
                encodeYUV420SP(data, mIntArray, mWidth, mHeight);
                    }
}

    static public void decodeYUV420SP(int[] rgba, byte[] yuv420sp, int width,
        int height) {
    final int frameSize = width * height;

    for (int j = 0, yp = 0; j < height; j++) {
        int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
        for (int i = 0; i < width; i++, yp++) {
            int y = (0xff & ((int) yuv420sp[yp])) - 16;
            if (y < 0)
                y = 0;
            if ((i & 1) == 0) {
                v = (0xff & yuv420sp[uvp++]) - 128;
                u = (0xff & yuv420sp[uvp++]) - 128;
            }

            int y1192 = 1192 * y;
            int r = (y1192 + 1634 * v);
            int g = (y1192 - 833 * v - 400 * u);
            int b = (y1192 + 2066 * u);

            if (r < 0)
                r = 0;
            else if (r > 262143)
                r = 262143;
            if (g < 0)
                g = 0;
            else if (g > 262143)
                g = 262143;
            if (b < 0)
                b = 0;
            else if (b > 262143)
                b = 262143;

            // rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) &
            // 0xff00) | ((b >> 10) & 0xff);
            // rgba, divide 2^10 ( >> 10)
            rgba[yp] = ((r << 14) & 0xff000000) | ((g << 6) & 0xff0000)
                    | ((b >> 2) | 0xff00);
        }
    }
}


    static public void encodeYUV420SP_original(byte[] yuv420sp, int[] rgba,
        int width, int height) {
    final int frameSize = width * height;

    int[] U, V;
    U = new int[frameSize];
    V = new int[frameSize];

    final int uvwidth = width / 2;

    int r, g, b, y, u, v;
    for (int j = 0; j < height; j++) {
        int index = width * j;
        for (int i = 0; i < width; i++) {
            r = (rgba[index] & 0xff000000) >> 24;
            g = (rgba[index] & 0xff0000) >> 16;
            b = (rgba[index] & 0xff00) >> 8;

            // rgb to yuv
            y = (66 * r + 129 * g + 25 * b + 128) >> 8 + 16;
            u = (-38 * r - 74 * g + 112 * b + 128) >> 8 + 128;
            v = (112 * r - 94 * g - 18 * b + 128) >> 8 + 128;

            // clip y
            yuv420sp[index++] = (byte) ((y < 0) ? 0 : ((y > 255) ? 255 : y));
            U[index] = u;
            V[index++] = v;
        }
    }

问题是编码和解码 Yuv 数据可能有一些错误,因为如果我跳过预处理步骤,那么编码的 Yuv 数据也与 PreviewCallback 的原始数据不同。

请帮我解决这个问题。我必须在 OCR 扫描中使用这段代码,所以我需要实现这种类型的逻辑。

如果有任何其他方式做同样的事情,请提供给我。

提前致谢。 :)

【问题讨论】:

    标签: android image-processing ocr android-camera yuv


    【解决方案1】:

    尽管文档建议您可以设置图像数据应以哪种格式从相机到达,但实际上您通常可以选择一种格式:NV21,一种 YUV 格式。有关此格式的大量信息,请参阅http://www.fourcc.org/yuv.php#NV21,有关将其转换为 RGB 背后的理论信息,请参阅http://www.fourcc.org/fccyvrgb.phpExtract black and white image from android camera's NV21 format 有图片说明。维基百科页面上有一个关于该主题的 android 特定部分(感谢@AlexCohn):YUV#Y'UV420sp (NV21) to RGB conversion (Android)

    但是,一旦您设置了 onPreviewFrame 例程,从它发送给您的字节数组到有用数据的机制有点,嗯,不清楚。从 API 8 开始,以下解决方案可用,以获取保存图像 JPEG 的 ByteStream(compressToJpeg 是 YuvImage 提供的唯一转换选项):

    // pWidth and pHeight define the size of the preview Frame
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    
    // Alter the second parameter of this to the actual format you are receiving
    YuvImage yuv = new YuvImage(data, ImageFormat.NV21, pWidth, pHeight, null);
    
    // bWidth and bHeight define the size of the bitmap you wish the fill with the preview image
    yuv.compressToJpeg(new Rect(0, 0, bWidth, bHeight), 50, out);
    

    然后可能需要将此 JPEG 转换为您想要的格式。如果你想要一个位图:

    byte[] bytes = out.toByteArray();
    Bitmap bitmap= BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    

    如果由于某种原因您无法执行此操作,您可以手动进行转换。这样做需要克服的一些问题:

    1. 数据以字节数组的形式到达。根据定义,字节是有符号数字,这意味着它们从 -128 到 127。但是,数据实际上是无符号字节(0 到 255)。如果不处理,结果注定会产生一些奇怪的削波效果。

    2. 数据的顺序非常具体(根据前面提到的网页),每个像素都需要仔细提取。

    3. 例如,每个像素都需要放在位图上的正确位置。这也需要一种相当混乱(在我看来)的方法来构建数据缓冲区,然后从中填充位图。

    4. 原则上,值应该存储[16..240],但在发送到onPreviewFrame的数据中,它们似乎存储[0..255]

    5. 几乎每个关于此事的网页都提出了不同的系数,甚至允许 [16..240] 与 [0..255] 选项。

    6. 如果您确实有 NV12(YUV420 上的另一个变体),那么您需要将读取交换为 U 和 V。

    我提出了一个解决方案(似乎可行),要求进行更正、改进以及降低整个运行成本的方法。我已经把它设置为希望能弄清楚发生了什么,而不是为了速度而优化它。它创建一个预览图像大小的位图:

    数据变量来自对onPreviewFrame的调用

    // Define whether expecting [16..240] or [0..255]
    boolean dataIs16To240 = false;
    
    // the bitmap we want to fill with the image
    Bitmap bitmap = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
    int numPixels = imageWidth*imageHeight;
    
    // the buffer we fill up which we then fill the bitmap with
    IntBuffer intBuffer = IntBuffer.allocate(imageWidth*imageHeight);
    // If you're reusing a buffer, next line imperative to refill from the start,
    // if not good practice
    intBuffer.position(0);
    
    // Set the alpha for the image: 0 is transparent, 255 fully opaque
    final byte alpha = (byte) 255;
    
    // Holding variables for the loop calculation
    int R = 0;
    int G = 0;
    int B = 0;
    
    // Get each pixel, one at a time
    for (int y = 0; y < imageHeight; y++) {
        for (int x = 0; x < imageWidth; x++) {
            // Get the Y value, stored in the first block of data
            // The logical "AND 0xff" is needed to deal with the signed issue
            float Y = (float) (data[y*imageWidth + x] & 0xff);
    
            // Get U and V values, stored after Y values, one per 2x2 block
            // of pixels, interleaved. Prepare them as floats with correct range
            // ready for calculation later.
            int xby2 = x/2;
            int yby2 = y/2;
    
            // make this V for NV12/420SP
            float U = (float)(data[numPixels + 2*xby2 + yby2*imageWidth] & 0xff) - 128.0f;
    
            // make this U for NV12/420SP
            float V = (float)(data[numPixels + 2*xby2 + 1 + yby2*imageWidth] & 0xff) - 128.0f;
    
            if (dataIs16To240) {
                // Correct Y to allow for the fact that it is [16..235] and not [0..255]
                Y = 1.164*(Y - 16.0);
    
                // Do the YUV -> RGB conversion
                // These seem to work, but other variations are quoted
                // out there.
                R = (int)(Yf + 1.596f*V);
                G = (int)(Yf - 0.813f*V - 0.391f*U);
                B = (int)(Yf            + 2.018f*U);
            }
            else {
                // No need to correct Y
                // These are the coefficients proposed by @AlexCohn
                // for [0..255], as per the wikipedia page referenced
                // above
                R = (int)(Yf + 1.370705f*V);
                G = (int)(Yf - 0.698001f*V - 0.337633f*U);
                B = (int)(Yf               + 1.732446f*U);
            }
                  
            // Clip rgb values to 0-255
            R = R < 0 ? 0 : R > 255 ? 255 : R;
            G = G < 0 ? 0 : G > 255 ? 255 : G;
            B = B < 0 ? 0 : B > 255 ? 255 : B;
    
            // Put that pixel in the buffer
            intBuffer.put(alpha*16777216 + R*65536 + G*256 + B);
        }
    }
    
    // Get buffer ready to be read
    intBuffer.flip();
    
    // Push the pixel information from the buffer onto the bitmap.
    bitmap.copyPixelsFromBuffer(intBuffer);
    

    正如@Timmmm 在下面指出的那样,您可以通过将比例因子乘以 1000(即 1.164 变为 1164)然后将最终结果除以 1000 来进行 int 转换。

    【讨论】:

    • 你的剪裁线有点奇怪。您可以删除每行的第一个或最后两个 R=。还有什么是numPixelsfloat 会很慢;我很确定你可以只用整数来做到这一点。
    • 只有我一个人认为转换为 JPEG 并返回位图有点奇怪吗?
    • @AlexCohn 你是正确的 Y 是 [16..235],我很抱歉没有具体说明。然而,代码确实包含了它:Yf 的计算(“Do the YUV ...”注释之后的第一行处理它)。我将更新我的答案以明确这一点,谢谢。
    • 问题是/不是代码错误或不相关。 BT.601 色彩空间是完全合法的;这是您通常从视频解码器获得的。让我重申一下,问题在于,在非常具体的情况下,当我们需要转换到达 Android 相机 onPreviewFrame 回调的 N21 帧时,颜色空间是完整的 [0..255],就像在 Jpeg 中一样,并且可以通过以下方式轻松验证比较第一个和第二个代码sn-ps的结果。 8 年后,新的 Camera2 和 cameraX API 仍然如此。
    • @AlexCohn 检查我已经理解:您是说代码对于一般转换是正确的,但是对于 onPreviewFrame 回调的特定情况,这是原始问题的含义, Yf 转换线应该是 Yf=(float)Y ?
    【解决方案2】:

    为什么不指定相机预览应该提供 RGB 图像?

    Camera.Parameters.setPreviewFormat(ImageFormat.RGB_565);

    【讨论】:

    • 谢谢@Reuben,:) 我已经设置了 PreviewFormat,现在不需要转换了。
    • 请注意,这不适用于所有设备。默认格式为 YUV,不支持预览的设备,因为 RGB 仍会为您提供 YUV 格式的图像。
    • YUV21 和 YUV12 是所有相机支持的图像格式,用于使用其他格式检查您的相机是否支持该功能(如果不支持,回调仍将提供 YUV21 格式的数据)。 mCamera = Camera.open(); Camera.Parameters 参数 = mCamera.getParameters(); for(int i: params.getSupportedPreviewFormats()) { Log.e(TAG, "支持的预览格式为 = "+i);}
    【解决方案3】:

    你可以使用 RenderScript -> ScriptIntrinsicYuvToRGB

    Kotlin 示例

    val rs = RenderScript.create(CONTEXT_HERE)
    val yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs))
    
    val yuvType = Type.Builder(rs, Element.U8(rs)).setX(byteArray.size)
    val inData = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT)
    
    val rgbaType = Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height)
    val outData = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT)
    
    inData.copyFrom(byteArray)
    
    yuvToRgbIntrinsic.setInput(inData)
    yuvToRgbIntrinsic.forEach(outData)
    
    val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    outData.copyTo(bitmap)
    

    【讨论】:

    • 通过快速基准测试,我发现这比 Neil 使用 compressToJpeg 提出的解决方案快 4 倍
    • 不幸的是,内部转换针对视频处理进行了调整,而不是针对相机图像流(请参阅the formulae)。我有一个fix 可以解决这个问题。
    【解决方案4】:

    在对三星 S4 mini 进行一些测试后,最快的代码是(比 Neil 的 [floats!] 快 120%,比原始 Hitesh 的快 30%):

    static public void decodeYUV420SP(int[] rgba, byte[] yuv420sp, int width,
                                      int height) {
    
    
        final int frameSize = width * height;
    // define variables before loops (+ 20-30% faster algorithm o0`)
    int r, g, b, y1192, y, i, uvp, u, v;
            for (int j = 0, yp = 0; j < height; j++) {
                uvp = frameSize + (j >> 1) * width;
                u = 0;
            v = 0;
            for (i = 0; i < width; i++, yp++) {
                y = (0xff & ((int) yuv420sp[yp])) - 16;
                if (y < 0)
                    y = 0;
                if ((i & 1) == 0) {
                    v = (0xff & yuv420sp[uvp++]) - 128;
                    u = (0xff & yuv420sp[uvp++]) - 128;
                }
    
                    y1192 = 1192 * y;
                    r = (y1192 + 1634 * v);
                    g = (y1192 - 833 * v - 400 * u);
                    b = (y1192 + 2066 * u);
    
    // Java's functions are faster then 'IFs'
                        r = Math.max(0, Math.min(r, 262143));
                    g = Math.max(0, Math.min(g, 262143));
                    b = Math.max(0, Math.min(b, 262143));
    
                    // rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) &
                    // 0xff00) | ((b >> 10) & 0xff);
                    // rgba, divide 2^10 ( >> 10)
                    rgba[yp] = ((r << 14) & 0xff000000) | ((g << 6) & 0xff0000)
                            | ((b >> 2) | 0xff00);
                }
            }
        }
    

    速度与使用 ByteArrayOutputStream 作为输出的 YuvImage.compressToJpeg() 相当(640x480 图像需要 30-50 毫秒)。

    结果:三星 S4 mini (2x1.7GHz) 无法实时压缩为 JPEG/将 YUV 转换为 RGB (640x480@30fps)

    【讨论】:

    • 最后一行对吗? ((b >> 2) | 0xff00) 不应该是 ((b >> 2) & 0xff00) 吗?
    • 我在 Nexus 7 (Android 6.0.1) 上使用上述 NV21 到 RGB 转换算法进行了一些测量。转换一个 1600x1200 像素的预览帧需要 250 毫秒到 300 毫秒。注意:使用 if/else 代替 Math.min/max 会更快。然而,BoofCV 库的使用带来了巨大的惊喜。从 NV21 转换为 BoofCV 的图像类需要 100ms-145ms 进行颜色转换,如果预览帧也转换为灰度则需要 230ms-260ms。在使用预览帧来分析场景的时间紧迫的情况下,BoofCV 可能值得考虑。
    【解决方案5】:

    Java实现比c版本慢10倍,建议你使用GPUImage库或者只是移动这部分代码。

    GPUImage 有安卓版本: https://github.com/CyberAgent/android-gpuimage

    如果你使用 gradle,你可以包含这个库,并调用方法: GPUImageNativeLibrary.YUVtoRBGA(inputArray, WIDTH, HEIGHT, outputArray);

    我比较了时间,对于一个960x540的NV21图像,使用上面的java代码,它花费200ms+,使用GPUImage版本,只需10ms~20ms。

    【讨论】:

      【解决方案6】:

      修复上面的代码sn-p

      static public void decodeYUV420SP(int[] rgba, byte[] yuv420sp, int width,
                                    int height) {
          final int frameSize = width * height;
          int r, g, b, y1192, y, i, uvp, u, v;
          for (int j = 0, yp = 0; j < height; j++) {
              uvp = frameSize + (j >> 1) * width;
              u = 0;
              v = 0;
              for (i = 0; i < width; i++, yp++) {
                  y = (0xff & ((int) yuv420sp[yp])) - 16;
                  if (y < 0)
                      y = 0;
                  if ((i & 1) == 0) {
                  // above answer is wrong at the following lines. just swap ***u*** and ***v*** 
                      u = (0xff & yuv420sp[uvp++]) - 128;
                      v = (0xff & yuv420sp[uvp++]) - 128;
                  }
      
                  y1192 = 1192 * y;
                  r = (y1192 + 1634 * v);
                  g = (y1192 - 833 * v - 400 * u);
                  b = (y1192 + 2066 * u);
      
                  r = Math.max(0, Math.min(r, 262143));
                  g = Math.max(0, Math.min(g, 262143));
                  b = Math.max(0, Math.min(b, 262143));
      
                  // combine ARGB
                  rgba[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00)
                          | ((b >> 10) | 0xff);
              }
          }
      }
      

      【讨论】:

      • 最后一行好像有问题: ((g >>2 6) & 0xff00 | ((b >> 10) | 0xff) 不应该是:((g > >2) & 0xff00) | ((b >> 10) & 0xff)?
      • 如何使用int[]加载位图?
      • @SePröbläm 你说得对,我在检查了这段代码后得出了同样的结论。而不是按位或|,在((b &gt;&gt; 10) | 0xff)中应该有按位和&amp;
      【解决方案7】:

      尝试使用 JellyBean 4.2 (Api 17+) 附带的 RenderScript ScriptIntrinsicYuvToRGB。

      https://developer.android.com/reference/android/renderscript/ScriptIntrinsicYuvToRGB.html

      在 Nexus 7(2013,JellyBean 4.3)上,1920x1080 图像转换(全高清摄像头预览)大约需要 7 毫秒。

      【讨论】:

      • 不幸的是,内部转换针对视频处理进行了调整,而不是针对相机图像流(请参阅the formulae)。我有一个fix 可以解决这个问题。
      【解决方案8】:

      您可以直接从 TextureView 获取位图。这真的很快。

      Bitmap bitmap = textureview.getBitmap()
      

      【讨论】:

        【解决方案9】:

        在阅读了许多建议的链接、文章等之后,我发现了以下出色的 Android 示例应用程序,它从相机捕获 YUV 图像并将其转换为 RGB 位图:

        https://github.com/android/camera-samples/tree/main/CameraXTfLite

        关于这个的好东西:

        • 它使用了前面提到的 RenderScript 框架,并且代码可以很容易地重用 - 查看 YuvToRgbConverter.kt
        • 根据他们的文档,此代码在 Pixel 3 手机上实现了“~30 FPS @ 640x480”

        切换到这段代码后(尤其是 YUV 到 RGB 的转换部分)我的帧率翻了一番!我总体上还没有达到 30 FPS,因为我在捕获图像后做了更多的事情,但速度提升非常显着!

        【讨论】:

          猜你喜欢
          • 2017-04-23
          • 1970-01-01
          • 2012-11-22
          • 1970-01-01
          • 2021-05-28
          • 2014-09-18
          • 2014-01-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多