[英]Convert from YUV to RGB in c++ (android-ndk)
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中开发,并希望将字节数组从相机的previewCallback(YUV格式)转换为rgb格式。
I have used the function given in this answer: Getting frames from Video Image in Android 我使用了这个答案中给出的功能: 从Android中的视频图像中获取帧
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++). 它在java中完美运行,但我的问题是我想用c ++编写函数(我使用的是ndk,而不是很熟悉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++? 有没有人有类似的功能或这个功能在c ++中工作?
Thanks. 谢谢。
Conversion from YUYV to RGB in 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;
}
This method assumes that your yuyv_image is an unsigned char* as well. 此方法假定您的yuyv_image也是unsigned char *。
More information on YUYV can be found here 有关YUYV的更多信息,请点击此处
And for more clarification on YUYV --> RGB check out this 有关YUYV的更多说明 - > RGB请查看此内容
Look at this: http://pastebin.com/mDcwqJV3 看看这个: http : //pastebin.com/mDcwqJV3
Fixed-point conversion from YUYV to RGB24 从YUYV到RGB24的定点转换
Also, some cameras return raw images in 'UYVY' byte orger, so make corresponding changes in the conversion function. 此外,某些相机以“UYVY”字节为单位返回原始图像,因此在转换功能中进行相应的更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.