如何获得YUV值的特定区域

时间:2014-09-18 16:47:12

标签: android image-processing camera yuv rect

我为show image stream form camera创建了surfaceview,我有特定区域的矩形来检测。我需要在矩形中只得到Y值我该怎么办。我发现代码形式的网站,代码将YUV改为RGB,但我只通过返回Y值来适应它。 请告诉我如何在特定区域仅获取矩形的Y值。

我尝试改变i,j,宽度,高度。是收集吗? 举个例子。

我是新的android程序员。 谢谢

public void decodeYUV420(int[] rgb, byte[] yuv420, 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) yuv420[yp])) - 16;
        if (y < 0) y = 0;
        if ((i & 1) == 0) {
            v = (0xff & yuv420[uvp++]) - 128;
            u = (0xff & yuv420[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);

          rgb[yp] = y;     // i need only y value
    }
}
}

1 个答案:

答案 0 :(得分:0)

这样的事情:

public int[] extractY(byte[] yuv420, Size szYuv, Rect rcY) 
{
    rcY.intersect(0, 0, szYuv.width, szYuv.height);
    final int[] res = int[rcY.width()*rcY.height()];

    int yCount = 0;
    for (int row=rcY.left; row<rcY.right; row++)
    {
        for (int col=rcY.top; col<rcY.bottom; col++) 
        {
            res[yCount++] = yuv420[col+rcY.left+(row+rcY.top)*szYuv.width];
        }
    }
    return res;
}
相关问题