从YUV转换为RGB在C ++(Android的NDK) [英] Convert from YUV to RGB in c++ (android-ndk)

查看:1367
本文介绍了从YUV转换为RGB在C ++(Android的NDK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯在Android的发展,并希望将字节数组从相机的previewCallback,这是YUV格式,以RGB-格式转换。

Im developing in android, and want to convert the byte-array from the camera's previewCallback, which is in YUV-format, to rgb-format.

我已经用在这个答案中给出的函数:从视频图像获取的帧在Android的

I have used the function given in this answer: Getting frames from Video Image in Android

它完全在Java中,但我的问题是,我想使函数在C ++中(我使用的NDK,而不是非常熟悉C ++)。

It works perfectly in java, but my problem is that I want to make the function in c++ (I'm using the ndk, and not very familiar with c++).

我曾尝试创建C ++中的功能,但它总是让人奇怪​​的结果(例如图片全部是绿色)。

I have tried to create the function in c++, but it always makes strange results (eg the picture is all green).

有没有人有类似的功能或此功能在C ++的工作?

Does anyone have a similar function or this function working in c++?

感谢。

推荐答案

从YUYV转换为RGB在C ++:

Conversion from YUYV to RGB in C++:

unsigned char* rgb_image = new unsigned char[width * height * 3]; //width and height of the image to be converted

int y;
int cr;
int cb;

double r;
double g;
double b;

for (int i = 0, j = 0; i < width * height * 3; i+=6 j+=4) {
    //first pixel
    y = yuyv_image[j];
    cb = yuyv_image[j+1];
    cr = yuyv_image[j+3];

    r = y + (1.4065 * (cr - 128));
    g = y - (0.3455 * (cb - 128)) - (0.7169 * (cr - 128));
    b = y + (1.7790 * (cb - 128));

    //This prevents colour distortions in your rgb image
    if (r < 0) r = 0;
    else if (r > 255) r = 255;
    if (g < 0) g = 0;
    else if (g > 255) g = 255;
    if (b < 0) b = 0;
    else if (b > 255) b = 255;

    rgb_image[i] = (unsigned char)r;
    rgb_image[+1] = (unsigned char)g;
    rgb_image[i+2] = (unsigned char)b;

    //second pixel
    y = yuyv_image[j+2];
    cb = yuyv_image[j+1];
    cr = yuyv_image[j+3];

    r = y + (1.4065 * (cr - 128));
    g = y - (0.3455 * (cb - 128)) - (0.7169 * (cr - 128));
    b = y + (1.7790 * (cb - 128));

    if (r < 0) r = 0;
    else if (r > 255) r = 255;
    if (g < 0) g = 0;
    else if (g > 255) g = 255;
    if (b < 0) b = 0;
    else if (b > 255) b = 255;

    rgb_image[i+3] = (unsigned char)r;
    rgb_image[+4] = (unsigned char)g;
    rgb_image[i+5] = (unsigned char)b;
}

该方法假定您yuyv_image是一个无符号的char *为好。

This method assumes that your yuyv_image is an unsigned char* as well.

的更多信息,可以发现这里

More information on YUYV can be found here

和更多的澄清YUYV - > RGB退房

And for more clarification on YUYV --> RGB check out this

这篇关于从YUV转换为RGB在C ++(Android的NDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆