opencv raw转rgb_使用OpenCV从YUV颜色空间转换为RGB

本文介绍了如何使用OpenCV将YUV图像转换为RGB或RGBA格式。提供了可能的代码实现,并纠正了原始代码中的一些错误,如传递缓冲区指针而不是地址,以及选择正确的颜色转换代码(CV_YUV2RGBA_NV21)。
摘要由CSDN通过智能技术生成

I am trying to convert a YUV image to RGB using OpenCV. I am a complete novice at this. I have created a function which takes a YUV image as source and converts it into RGB. It is like this :

void ConvertYUVtoRGBA(const unsigned char *src, unsigned char *dest, int width, int height)

{

cv::Mat myuv(height + height/2, width, CV_8UC1, &src);

cv::Mat mrgb(height, width, CV_8UC4, &dest);

cv::cvtColor(myuv, mrgb, CV_YCrCb2RGB);

return;

}

Should this work? Do I need to convert the Mat into char* again? I am in a loss and any help will be greatly appreciated.

解决方案

There is not enough detail in your question to give a certain answer but below is my best guess. I'll assume you want RGBA output (not RGB, BGR or BGRA) and that your YUV is yuv420sp (as this is what comes out of an Android camera, and it is consistent with your Mat sizes)

void ConvertYUVtoRGBA(const unsigned char *src, unsigned char *dest, int width, int height)

{

//cv::Mat myuv(height + height/2, width, CV_8UC1, &src);

cv::Mat myuv(height + height/2, width, CV_8UC1, src); // pass buffer pointer, not its address

//cv::Mat mrgb(height, width, CV_8UC4, &dest);

cv::Mat mrgb(height, width, CV_8UC4, dest);

//cv::cvtColor(myuv, mrgb, CV_YCrCb2RGB);

cv::cvtColor(myuv, mrgb, CV_YUV2RGBA_NV21); // are you sure you don't want BGRA?

return;

}

Do I need to convert the Mat into char again?*

No the Mat mrgb is a wrapper around dest and, the way you have arranged it, the RGBA data will written directly into the dest buffer.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值