简体   繁体   English

YUV图像处理

[英]YUV image processing

I'm developing an android app that for every YUV image passed from camera, it randomly pick 10 pixels from it and check if they are red or blue. 我正在开发一个Android应用程序,对于从相机传递的每个YUV图像,它都会从其中随机选取10个像素,并检查它们是红色还是蓝色。 I know how to do this for RGB images, but not for YUV format. 我知道如何对RGB图像执行此操作,但不对YUV格式执行此操作。 I cannot convert it pixel by pixel into a RGB image because of the run time constrains. 由于运行时间的限制,我无法将其逐像素转换为RGB图像。

I'm assuming you're using the Camera API's preview callbacks, where you get a byte[] array of data for each frame. 我假设您使用的是Camera API的预览回调,其中每个帧都有一个byte []数据数组。

First, you need to select which YUV format you want to use. 首先,您需要选择要使用的YUV格式。 NV21 is required to be supported, and YV12 is required since Android 3.0. 从Android 3.0开始,必须支持NV21,并且需要YV12。 None of the other formats are guaranteed to be available. 其他任何格式均不能保证可用。 So NV21 is the safest choice, and also the default. 因此,NV21是最安全的选择,也是默认设置。

Both of these formats are YUV 4:2:0 formats; 这两种格式均为YUV 4:2:0格式; the color information is subsampled by 2x in both dimensions, and the layout of the image data is fairly different from the standard interleaved RGB format. 颜色信息在两个维度上均按2倍进行二次采样,并且图像数据的布局与标准的交错RGB格式完全不同。 FourCC.org's NV21 description , as one source, has the layout information you want - first the Y plane, then the UV data interleaved. 作为一个来源, FourCC.org的NV21描述具有您想要的布局信息-首先是Y平面,然后是UV数据交错。 But since the two color planes are only 1/4 of the size of the Y plane, you'll have to decide how you want to upsample them - the simplest is nearest neighbor. 但是,由于两个颜色平面仅是Y平面大小的1/4,因此您必须决定如何对它们进行上采样-最简单的是最近的邻居。 So if you want pixel (x,y) from the image of size (w, h), the nearest neighbor approach is: 因此,如果要从大小为(w,h)的图像中选择像素(x,y),则最接近的邻居方法是:


Y = image[ y * w + x];
U = image[ w * h + floor(y/2) * (w/2) + floor(x/2) + 1]
V = image[ w * h + floor(y/2) * (w/2) + floor(x/2) + 0]

More sophisticated upsampling (bilinear, cubic, etc) for the chroma channels can be used as well, but what's suitable depends on the application. 也可以对色度通道使用更复杂的上采样(双线性,三次等),但是合适的方法取决于应用。

Once you have the YUV pixel, you'll need to interpret it. 获得YUV像素后,您需要对其进行解释。 If you're more comfortable operating in RGB, you can use these JPEG conversion equations at Wikipedia to get the RGB values. 如果您更习惯使用RGB,可以在Wikipedia上使用这些JPEG转换方程式来获取RGB值。

Or, you can just use large positive values of V (Cr) to indicate red, especially if U (Cb) is small. 或者,您可以使用大的正V值(Cr)表示红色,特别是如果U(Cb)小时。

From the answer of Reuben Scratton in Converting YUV->RGB(Image processing)->YUV during onPreviewFrame in android? 从Reuben Scratton在Android的onPreviewFrame期间转换 YUV- > RGB(图像处理)-> YUV的答案中?

You can make the camera preview use RGB format instead of YUV. 您可以使相机预览使用RGB格式而不是YUV。

Try this: 尝试这个:

Camera.Parameters.setPreviewFormat(ImageFormat.RGB_565); Camera.Parameters.setPreviewFormat(ImageFormat.RGB_565);

YUV is just another colour space. YUV只是另一个色彩空间。 You can define red in YUV space just as you can in RGB space. 您可以在YUV空间中定义红色,就像在RGB空间中一样。 A simple calculator suggests an RGB value of 255,0,0 (red) should appear as something like 76,84,255 in YUV space so just look for something close to that. 一个简单的计算器建议RGB值255,0,0(红色)在YUV空间中应显示为76,84,255,所以只需要寻找与之接近的值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM