31

我已经做了一些谷歌搜索,但找不到关于这种格式的足够信息。它是相机预览的默认格式。谁能建议有关它的良好信息来源以及如何从具有该格式的照片/预览图像中提取数据?更具体地说,我需要提取的黑白图像。

编辑:似乎该格式也称为 YCbCr 420 Semi Planar

4

6 回答 6

83

我开发了以下代码来将 NV21 转换为 RGB,它正在工作。

/**
 * Converts YUV420 NV21 to RGB8888
 * 
 * @param data byte array on YUV420 NV21 format.
 * @param width pixels width
 * @param height pixels height
 * @return a RGB8888 pixels int array. Where each int is a pixels ARGB. 
 */
public static int[] convertYUV420_NV21toRGB8888(byte [] data, int width, int height) {
    int size = width*height;
    int offset = size;
    int[] pixels = new int[size];
    int u, v, y1, y2, y3, y4;

    // i percorre os Y and the final pixels
    // k percorre os pixles U e V
    for(int i=0, k=0; i < size; i+=2, k+=2) {
        y1 = data[i  ]&0xff;
        y2 = data[i+1]&0xff;
        y3 = data[width+i  ]&0xff;
        y4 = data[width+i+1]&0xff;

        u = data[offset+k  ]&0xff;
        v = data[offset+k+1]&0xff;
        u = u-128;
        v = v-128;

        pixels[i  ] = convertYUVtoRGB(y1, u, v);
        pixels[i+1] = convertYUVtoRGB(y2, u, v);
        pixels[width+i  ] = convertYUVtoRGB(y3, u, v);
        pixels[width+i+1] = convertYUVtoRGB(y4, u, v);

        if (i!=0 && (i+2)%width==0)
            i+=width;
    }

    return pixels;
}

private static int convertYUVtoRGB(int y, int u, int v) {
    int r,g,b;

    r = y + (int)(1.402f*v);
    g = y - (int)(0.344f*u +0.714f*v);
    b = y + (int)(1.772f*u);
    r = r>255? 255 : r<0 ? 0 : r;
    g = g>255? 255 : g<0 ? 0 : g;
    b = b>255? 255 : b<0 ? 0 : b;
    return 0xff000000 | (b<<16) | (g<<8) | r;
}

这张图有助于理解。 YUV420 NV21

如果你只想灰度图像更容易。您可以丢弃所有 U 和 V 信息,而只获取 Y 信息。代码可能是这样的:

/**
 * Converts YUV420 NV21 to Y888 (RGB8888). The grayscale image still holds 3 bytes on the pixel.
 * 
 * @param pixels output array with the converted array o grayscale pixels
 * @param data byte array on YUV420 NV21 format.
 * @param width pixels width
 * @param height pixels height
 */
public static void applyGrayScale(int [] pixels, byte [] data, int width, int height) {
    int p;
    int size = width*height;
    for(int i = 0; i < size; i++) {
        p = data[i] & 0xFF;
        pixels[i] = 0xff000000 | p<<16 | p<<8 | p;
    }
}

要创建您的位图,只需:

Bitmap bm = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);

pixel 是您的 int [] 数组。

于 2012-10-03T06:18:17.900 回答
12

NV21 基本上是 YUV420,但不是 Y、U 和 V 具有独立平面的平面格式,NV21 有一个用于亮度的平面和第二个用于色度的平面。格式看起来像

YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY。. . .

VUVUVUVUVUVUVUVUVUVUVUVUVUVUVU VUVUVUVUVUVUVUVUVUVUVUVUVUVUVU。. . . .

于 2012-06-20T18:53:40.187 回答
7

由于这种预览格式,我也很头疼。

我能找到的最好的是这些:

http://www.fourcc.org/yuv.php#NV21

http://v4l2spec.bytesex.org/spec/r5470.htm

似乎 Y 分量是您获得的数组中的第一个 width*height 字节。

更多信息链接:

http://msdn.microsoft.com/en-us/library/ms867704.aspx#yuvformats_yuvsampling

http://msdn.microsoft.com/en-us/library/ff538197(v=vs.85).aspx

希望这可以帮助。

于 2011-04-27T10:52:33.893 回答
4

数据为 YUV420 格式。
如果您只对单色通道感兴趣,即“黑白”,那么这是width x height您已经拥有的数据缓冲区的第一个字节。通道是第
一个图像平面。它正是灰度/强度/亮度等通道。Y

于 2012-05-01T09:46:05.657 回答
3

这是仅提取灰度图像数据的代码:

private int[] decodeGreyscale(byte[] nv21, int width, int height) {
    int pixelCount = width * height;
    int[] out = new int[pixelCount];
    for (int i = 0; i < pixelCount; ++i) {
        int luminance = nv21[i] & 0xFF;
        out[i] = Color.argb(0xFF, luminance, luminance, luminance);
    }
    return out;
}
于 2014-12-08T06:51:58.230 回答
1

当你只需要一个灰度相机预览时,你可以使用一个非常简单的渲染脚本:

# pragma version(1)
# pragma rs java_package_name(com.example.name)
# pragma rs_fp_relaxed

rs_allocation gIn;   // Allocation filled with camera preview data (byte[])
int previewwidth;    // camera preview width (int)

// the parallel executed kernel
void root(uchar4 *v_out, uint32_t x,uint32_t y){
   uchar c = rsGetElementAt_uchar(gIn,x+y*previewwidth); 
   *v_out = (uchar4){c,c,c,255};
}

注意:这并不比 ScriptIntrinsicYuvToRGB(以及执行 RGBA-> gray 的后续 ScriptIntrinsicColorMatrix)快,但它使用 API 11+ 运行(其中 Intrinsic 需要 Api 17+)。

于 2017-04-21T15:18:15.160 回答