使用OpenCV从YUV颜色空间转换为RGB [英] Converting from YUV colour space to RGB using OpenCV

查看:3031
本文介绍了使用OpenCV从YUV颜色空间转换为RGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OpenCV将YUV图像转换为RGB。我是一个完整的新手。我创建了一个函数,它将YUV图像作为源并将其转换为RGB。它是这样的:

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;
}

这应该有效吗?我是否需要将Mat转换为char *?我很遗憾,任何帮助都将不胜感激。

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.

推荐答案

你的问题中没有足够的细节来给出一定的帮助。回答但下面是我最好的猜测。我假设你想要RGBA输出(不是RGB,BGR或BGRA),你的YUV是 yuv420sp (如这是来自Android相机的内容,它与您的Mat尺寸一致)

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;
}




我是否需要转换再次填入char ?*

没有Mat mrgb是dest的包装器,以及你安排它的方式,RGBA数据将直接写入dest缓冲区。

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.

这篇关于使用OpenCV从YUV颜色空间转换为RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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