RGB与YV12互转完整例子

李国帅 2010

不通过第三方库函数进行直接转换



#include <TIME.H>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;

long writefile(fstream& oStream, char* content, long contentlen)
{
    oStream.seekp(0, ios::end);
    streamsize _Count = (streamsize)strlen(content);
    //UINT nActual = (UINT)oStream.tellp();
    //strcpy_s(content,contentlen,("<?xml version=\"1.0\" encoding=\"GB2312\" ?>\r\n<DateSet>\r\n</DateSet>"));
    oStream.write(content, _Count + 1);//
    return 0;
}

void YUV2RGB(unsigned char Y, unsigned char U, unsigned char V, unsigned char* pRGB)
{
    int R, G, B;
    B = (int)(1.164*(Y - 16) + 2.018*(U - 128));
    G = (int)(1.164*(Y - 16) - 0.813*(V - 128) - 0.391*(U - 128));
    R = (int)(1.164*(Y - 16) + 1.596*(V - 128));
    // #define GETR(y,u,v) ((1.164 * (y - 16)) + (1.596 * ((v) - 128)))
    // #define GETG(y,u,v) ((1.164 * (y - 16)) - (0.813 * ((v) - 128)) - (0.391 * ((u) - 128)))
    // #define GETB(y,u,v) ((1.164 * (y - 16)) + (2.018 * ((u) - 128)))
    //cout << "RGB("<<GETR(y,u,v)<<","<<GETG(y,u,v)<<","<<GETB(y,u,v)<<","<<")"<<endl;
    //cout << "("<<(int)Y<<","<<(int)U<<","<<(int)V<<")->RGB("<<R<<","<<G<<","<<B<<","<<")"<<endl;

    R = min(255, max(0, R));
    G = min(255, max(0, G));
    B = min(255, max(0, B));

    pRGB[0] = R;
    pRGB[1] = G;
    pRGB[2] = B;
    return;
}
void RGB2YUV(unsigned char R, unsigned char G, unsigned char B, unsigned char* pYUV)
{
    int Y, U, V;

    Y = 16 + 0.257*R + 0.504*G + 0.098*B;
    U = 128 - 0.148*R - 0.291*G + 0.439*B;
    V = 128 + 0.439*R - 0.368*G - 0.071*B;

    Y = min(255, max(0, Y));
    U = min(255, max(0, U));
    V = min(255, max(0, V));

    pYUV[0] = Y;
    pYUV[1] = U;
    pYUV[2] = V;

    return;
}

int __cdecl main(int argc, CHAR **argv)
{
    unsigned char RGB[3];
    unsigned char YUV[3];
    char* filename = "c:\\testyuv.log";
    fstream oStream(filename, ios::app | ios::out | ios::in); // 定义打开输出流 ios::binary|
    bool bRet = oStream.fail();
    if (bRet) return 0;

    int r = 255, g = 0, b = 0;
    RGB2YUV(r, g, b, YUV);
    cout << "(" << (unsigned int)(YUV[0]) << "," << (unsigned int)(YUV[1]) << "," << (unsigned int)(YUV[2]) << ")->RGB(" << r << "," << g << "," << b << "," << ")" << endl;

    int y = (unsigned int)(YUV[0]), u = (unsigned int)(YUV[1]), v = (unsigned int)(YUV[2]);
    YUV2RGB(y, u, v, RGB);
    cout << "(" << (int)y << "," << (int)u << "," << (int)v << ")->RGB(" << (unsigned int)(RGB[0]) << "," << (unsigned int)(RGB[1]) << "," << (unsigned int)(RGB[2]) << "," << ")" << endl;

    oStream.flush();
    oStream.close(); // 关闭输出流
    //::DeleteFile(filename);

    system("PAUSE");
    return 0;
}
//生成一个rgb白色图片
void write_demo_rgb_file()
{
    unsigned char RGB[3];
    char output[50];
    int y, u, v;
    for (y = 0; y < 256; y++)
    {
        for (u = 0; u < 256; u++)
        for (v = 0; v < 256; v++) {
            /* initialize coefficient table using formula definition */
            YUV2RGB(y, u, v, RGB);
            sprintf_s(output, 50, "(%u,%u,%u)->RGB(%u,%u,%u)", y, u, v, (unsigned int)(RGB[0]), (unsigned int)(RGB[1]), (unsigned int)(RGB[2]));
            writefile(oStream, output, strlen(output));
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
yv12rgb是一种像素格式的换方法,它将YUV颜色空间中的图像数据换为RGB颜色空间中的图像数据。在OpenCV中,可以使用以下源代码实现yv12rgb的操作。 ```c++ #include <opencv2/opencv.hpp> void yv12ToRgb(const unsigned char* y, const unsigned char* u, const unsigned char* v, int width, int height, unsigned char* rgb) { cv::Mat yMat = cv::Mat(height, width, CV_8UC1, const_cast<unsigned char*>(y)); cv::Mat uMat = cv::Mat(height / 2, width / 2, CV_8UC1, const_cast<unsigned char*>(u)); cv::Mat vMat = cv::Mat(height / 2, width / 2, CV_8UC1, const_cast<unsigned char*>(v)); cv::Mat yuvMat; cv::merge(std::vector<cv::Mat>{yMat, uMat, vMat}, yuvMat); cv::cvtColor(yuvMat, yuvMat, cv::COLOR_YUV2RGB_I420); cv::Mat rgbMat; cv::cvtColor(yuvMat, rgbMat, cv::COLOR_YUV2BGR); memcpy(rgb, rgbMat.data, width * height * 3); } int main() { int width = 640; int height = 480; unsigned char* yData = new unsigned char[width * height]; unsigned char* uData = new unsigned char[width * height / 4]; unsigned char* vData = new unsigned char[width * height / 4]; // 假设已经将y、u、v数据填充到了相应的数组中 unsigned char* rgbData = new unsigned char[width * height * 3]; yv12ToRgb(yData, uData, vData, width, height, rgbData); // 处理rgbData,可以将其写入文件或进行其他操作 delete[] yData; delete[] uData; delete[] vData; delete[] rgbData; return 0; } ``` 上述代码中,`yv12ToRgb`函数接受原始的Y、U、V分量数据以及图像的宽度和高度,将其换为RGB格式,并将结果存储在`rgb`数组中。`main`函数中的测试代码展示了如何使用该函数进行yv12rgb的操作。注意,在使用完数组后使用`delete[]`进行内存释放,以防止内存泄漏。 以上是利用OpenCV的源码实现yv12rgb的方法,可以根据具体的使用场景进行相应的调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微澜-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值