its_a_sunshine 2023-05-11 10:31 采纳率: 28.6%
浏览 19

opencv格式转换

opencv C++,可以转化yuv422sp或yuv422p数据到rgb嘛,找不到这个转换格式呀,有懂opencv的吗

  • 写回答

2条回答 默认 最新

  • _DJ 2023-05-11 10:52
    关注
    
    
    #include <iostream>
    #include <opencv2/opencv.hpp>
    #include "pencv.h"
    
    using namespace std;
    using namespace cv;
    using namespace pencv;
    
    int main() {
        // 读取YUV422SP或YUV422P数据
        uchar y_plane[640*480], u_plane[640*480], v_plane[640*480];
        FILE* f = fopen("input.yuv", "rb");
        fread(y_plane, 1, sizeof(uchar)*640*480, f);
        fclose(f);
        f = fopen("input.yuv", "rb");
        fread(u_plane, 1, sizeof(uchar)*640*480, f);
        fclose(f);
        f = fopen("input.yuv", "rb");
        fread(v_plane, 1, sizeof(uchar)*640*480, f);
        fclose(f);
    
        // 将YUV422SP数据转换为RGB数据
        int width = 640, height = 480;
        int y_size = width * height;
        int u_size = width >> 1, v_size = height >> 1;
        uint8_t* y_rgb = new uint8_t[y_size * 3];
        uint8_t* u_rgb = new uint8_t[u_size * 3];
        uint8_t* v_rgb = new uint8_t[v_size * 3];
        memset(y_rgb, 0, y_size * 3);
        memset(u_rgb, 0, u_size * 3);
        memset(v_rgb, 0, v_size * 3);
        int y_p = 0, u_p = 0, v_p = 0;
        for (int i = 0; i < y_size; i++) {
            y_rgb[i] = y_plane[y_p++];
            if (i % (width >> 1) == 0) {
                u_p = 0;
                v_p = i / (width >> 1);
            } else if (i % (width >> 1) == (width >> 1)) {
                u_p++;
                v_p++;
            }
            if (i % (height >> 1) == 0) {
                u_rgb[(width >> 1) * v_p + u_p] = u_plane[u_p++];
                v_rgb[(width >> 1) * v_p + u_p] = v_plane[v_p++];
            } else if (i % (height >> 1) == (height >> 1)) {
                u_rgb[(width >> 1) * v_p + u_p] += u_plane[u_p++];
                v_rgb[(width >> 1) * v_p + u_p] += v_plane[v_p++];
            }
        }
    
        // 将RGB数据写入文件输出
        FILE* f = fopen("output.jpg", "wb");
        fwrite(y_rgb, sizeof(uint8_t), y_size * 3, f);
        fwrite(u_rgb, sizeof(uint8_t), u_size * 3, f);
        fwrite(v_rgb, sizeof(uint8
    
    评论

报告相同问题?

问题事件

  • 创建了问题 5月11日