1

我尝试使用 renderscript 中的 ScriptIntrinsicYuvToRGB 类进行 YUV 到 RGB 的转换,其中源为 YUV420 格式。我有 3 个原始平面,我从文件中读取并尝试将它们输入 YUV 类型的分配,然后通过 ScriptIntrinsicYuvToRGB.forEach 传递它。

它可以正确转换亮度(Y 平面),但在颜色上失败,因为色度通道似乎从 buf[w*h] 位置读取所有值 - 请参阅代码示例中的注释部分。当分配没有正确处理 UV 平面时,它看起来像错误。我假设是因为我在脚本中使用rsGetElementAtYuv_uchar_U分配函数进行了测试,并且它为任何坐标提供了相同的值(来自 buf[w*h])。

如果我可以进一步指定 YUV 格式(例如步幅/偏移等),我搜索了所有地方,但没有找到更多的设置Element.DataKind.PIXEL_YUVType.Builder.setYuvFormat(ImageFormat.YUV_420_888).

有人可以帮忙吗?

{
      int w = 320, h = 172;
      ScriptIntrinsicYuvToRGB yc = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
      {
         Element elemYUV = Element.createPixel(rs, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV);
         Type typeYUV = new Type.Builder(rs, elemYUV).setX(w).setY(h).setYuvFormat(ImageFormat.YUV_420_888).create();
         Allocation yuv = Allocation.createTyped(rs, typeYUV);
         byte[] buf = new byte[yuv.getBytesSize()];
         int offs = 0;
         for(int i=0; i<3; i++){
            int sz = w*h;
            if(i>0)
               sz /= 4;
            InputStream is = new FileInputStream("/sdcard/yuv/"+(i==0 ? 'y' : i==1 ? 'u' : 'v'));
            int n = is.read(buf, offs, sz);
            if(n!=sz)
               throw new AssertionError("!");
            offs += sz;
            is.close();
         }
//               buf[w*h] = 0x40;
         yuv.copyFrom(buf);
         yc.setInput(yuv);
      }

      Type outType = new Type.Builder(rs, Element.U8_4(rs)).setX(w).setY(h).create();
      Allocation out = Allocation.createTyped(rs, outType);
      yc.forEach(out);

      int[] buf = new int[out.getBytesSize()/4];
      out.copy1DRangeToUnchecked(0, w*h, buf);

      bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
      bm.setPixels(buf, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight());
      iv.setImageBitmap(bm);

      yc.destroy();
   }
4

2 回答 2

0

我相信你需要setYuvFormat()你的Type。这是我用来构建分配的两行:

Type typeYUV = Type.Builder(rs, Element.YUV(rs)).setYuvFormat(ImageFormat.NV21).create();
Allocation yuv = Allocation.createSized(rs, typeYUV.element, width*height*3/2);
于 2020-09-18T15:34:03.923 回答
-1

一种解决方案是仅填写 U8 分配并在自定义脚本中自己进行索引:

#pragma rs_fp_relaxed
rs_allocation yuv_in;
uint32_t width;
uint32_t offset_to_u;
uint32_t offset_to_v;
uchar4 RS_KERNEL yuv_to_rgba(uint32_t x, uint32_t y) {
    uint32_t index = y * width + x;
    uint32_t uv_index = (y >> 1) * width + (x >> 1);
    float Y = (float)rsGetElementAt_uchar(yuv_in, index);
    float U = (float)rsGetElementAt_uchar(yuv_in, uv_index + offset_to_u);
    float V = (float)rsGetElementAt_uchar(yuv_in, uv_index + offset_to_v);
    float3 f_out;
    f_out.r = Y + 1.403f * V;
    f_out.g = Y - 0.344f * U - 0.714f * V;
    f_out.b = Y + 1.770f * U;
    f_out = clamp(f_out, 0.f, 255.f);
    uchar4 out;
    out.rgb = convert_uchar3(f_out);
    out.a = 255;
    return out;
}

爪哇:

sc.set_yuv_in(yuv_allocation);
sc.set_width(width);
sc.set_offset_to_u(width * height);
sc.set_offset_to_v(width * height + (width/2 * height/2));
sc.forEach_yuv_to_rba(out);

YUV_420_888 更像是一个通用的 YUV 类型,用于从 android 中的其他 YUV 资源导入。我认为没有一种方法可以设置 u/v 平面的步幅/偏移值以使其对自定义转换有用。

于 2016-12-16T02:16:00.823 回答