【问题标题】:Android Renderscript - Rotate YUV data in RenderscriptAndroid Renderscript - 在 Renderscript 中旋转 YUV 数据
【发布时间】:2019-09-01 23:15:32
【问题描述】:

根据我在Camera2 api Imageformat.yuv_420_888 results on rotated image 的讨论,我想知道如何调整通过 rsGetElementAt_uchar 方法完成的查找,以便 YUV 数据旋转 90 度。 我也有一个类似谷歌提供的HdrViewfinder 的项目。问题是输出是横向的,因为用作目标表面的输出表面连接到 yuv 分配,它不关心设备是处于横向还是纵向模式。但我想调整代码使其处于纵向模式。 因此,我采用了自定义 YUVToRGBA 渲染脚本,但我不知道要更改什么来旋转输出。 有人可以帮我将以下自定义 YUVtoRGBA 脚本调整 90 度,因为我想在纵向模式下使用输出:

// Needed directive for RS to work
#pragma version(1)

// The java_package_name directive needs to use your Activity's package path
#pragma rs java_package_name(net.hydex11.cameracaptureexample)

rs_allocation inputAllocation;

int wIn, hIn;
int numTotalPixels;

// Function to invoke before applying conversion
void setInputImageSize(int _w, int _h)
{
    wIn = _w;
    hIn = _h;
    numTotalPixels = wIn * hIn;
}

// Kernel that converts a YUV element to a RGBA one
uchar4 __attribute__((kernel)) convert(uint32_t x, uint32_t y)
{

    // YUV 4:2:0 planar image, with 8 bit Y samples, followed by
    // interleaved V/U plane with 8bit 2x2 subsampled chroma samples
    int baseIdx = x + y * wIn;
    int baseUYIndex = numTotalPixels + (y >> 1) * wIn + (x & 0xfffffe);

    uchar _y = rsGetElementAt_uchar(inputAllocation, baseIdx);
    uchar _u = rsGetElementAt_uchar(inputAllocation, baseUYIndex);
    uchar _v = rsGetElementAt_uchar(inputAllocation, baseUYIndex + 1);
    _y = _y < 16 ? 16 : _y;

    short Y = ((short)_y) - 16;
    short U = ((short)_u) - 128;
    short V = ((short)_v) - 128;

    uchar4 out;
    out.r = (uchar) clamp((float)(
        (Y * 298 + V * 409 + 128) >> 8), 0.f, 255.f);   
    out.g = (uchar) clamp((float)(
        (Y * 298 - U * 100 - V * 208 + 128) >> 8), 0.f, 255.f); 
    out.b = (uchar) clamp((float)(
        (Y * 298 + U * 516 + 128) >> 8), 0.f, 255.f); // 
    out.a = 255;

    return out;
}

我在 https://bitbucket.org/cmaster11/rsbookexamples/src/tip/CameraCaptureExample/app/src/main/rs/customYUVToRGBAConverter.fs 找到了自定义脚本。

Here有人放了Java代码来旋转YUV数据。但我想在 Renderscript 中进行,因为这样更快。

任何帮助都会很棒。

最好的问候,

【问题讨论】:

  • 您是否尝试过仅将纵向模式图像输入脚本以查看会发生什么?
  • 不,我没有。为什么 ?是否已经针对纵向模式进行了调整?
  • 好像有设置输入图像大小的方法,所以是的。
  • @Haem:不,使用输入图像大小,输出仍然是横向的。

标签: android renderscript yuv rgba


【解决方案1】:

我假设您希望输出采用 RGBA 格式,就像在转换脚本中一样。您应该能够使用this answer 中使用的方法;也就是在转换内核的第一步中简单地修改 x 和 y 坐标:

//Rotate 90 deg clockwise during the conversion
uchar4 __attribute__((kernel)) convert(uint32_t inX, uint32_t inY)
{
    uint32_t x = wIn - 1 - inY;
    uint32_t y = inX;

    //...rest of the function

注意参数名称的变化。

这假定您已正确设置输出尺寸(请参阅链接答案)。 270度旋转也可以用类似的方式完成。

【讨论】:

  • 注意:我没有测试过这个解决方案。
  • 那么,我应该把uint32_t x = wIn - 1 - inYuint32_t y = inX 这两行放在convert() 内核函数的开头吗?
猜你喜欢
  • 2020-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
  • 2023-03-29
  • 2013-10-25
  • 1970-01-01
相关资源
最近更新 更多