在c++中从YUV转换为RGB(android-ndk)。

 2023-01-20    395  

问题描述

即时在Android中开发,并希望从相机的PreviewCallback(在YUV-Format中)转换为RGB-Format.

.

在c++中从YUV转换为RGB(android-ndk)。

我已经使用了此答案中给出的功能:从视频图像中获取框架在Android

它在Java中工作得很完美,但我的问题是我想在C ++中制作功能(我正在使用NDK,并且对C ++不太熟悉).

我尝试在C ++中创建该功能,但它总是会产生奇怪的结果(例如图片都是绿色的).

有人在C ++中具有类似的功能还是此功能?

谢谢.

推荐答案

在C ++中从YUYV转换为RGB:

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[i+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[i+4] = (unsigned char)g;
    rgb_image[i+5] = (unsigned char)b;
}

此方法假设您的yuyv_image也是一个未签名的char*.

可以找到有关YUYV的更多信息,

,请在YUYV上进行更多澄清 – > RGB查看 this

其他推荐答案

看一下:
http://pastebin.com/mdcwqjv3

定点转换从YUYV到RGB24

另外,某些摄像机返回” uyvy”字节中的原始图像,因此在转换功能中进行相应的更改.

以上所述是小编给大家介绍的在c++中从YUV转换为RGB(android-ndk)。,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/25922.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。