YUV420到BGR图像的像素指针

编程入门 行业动态 更新时间:2024-10-22 13:41:15
本文介绍了YUV420到BGR图像的像素指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在捕获来自YUV420解码器的原始输出.我有三个指针:分别是Y(1920 * 1080),U(960 * 540)和V(960 * 540).

I am capturing raw output from a decoder which is YUV420. I have got three pointers: Y(1920*1080), U(960*540) and V(960*540) separately.

我想使用OpenCV将图像另存为JPEG.我尝试使用opencv的cvtcolor

I want to save the image as JPEG using OpenCV. I tried using cvtcolor of opencv

cv::Mat i_image(cv::Size(columns, rows), CV_8UC3, dataBuffer); cv::Mat i_image_BGR(cv::Size(columns, rows), CV_8UC3); cvtColor(i_image, i_image_BGR, cv::COLOR_YCrCb2BGR); cv::imwrite("/data/data/org.myproject.debug/files/pic1.jpg", i_image_BGR);

但是,这是保存的输出图像:

But, here is the output image which is saved:

有人可以建议保存图像的正确方法是什么吗?

Can someone please suggest what is the proper way of saving the image?

YUV二进制文件以供参考

推荐答案

以下是将提供的YUV文件转换为RGB图像的过程.

Following is the process to convert the provided YUV files into RGB image.

  • 将Y,U和V二进制文件读入字节缓冲区
  • 从创建的缓冲区中创建OpenCV Mat对象.
  • 将U和V垫的大小调整为Y的大小.
  • 合并Y并调整U和V的大小.
  • 从YUV转换为BGR

请注意,调整大小步骤只是一种重复U和V值的优化方法.这仅在Y在两个方向上具有U和V分辨率两倍的情况下才有效.这种方法对于任意尺寸的图像(未经测试)应该无效.

Be advised that the resizing step is just an optimized way of repeating the values of U and V. This is only valid in the case where Y has twice the resolution of U and V in both dimensions. This approach should be invalid for arbitrary size images (not tested).

这是上述过程的代码.

Here is the code for the above-mentioned process.

#include <iostream> #include <vector> #include <opencv2/opencv.hpp> std::vector<unsigned char> readBytesFromFile(const char* filename) { std::vector<unsigned char> result; FILE* f = fopen(filename, "rb"); fseek(f, 0, SEEK_END); // Jump to the end of the file long length = ftell(f); // Get the current byte offset in the file rewind(f); // Jump back to the beginning of the file result.resize(length); char* ptr = reinterpret_cast<char*>(&(result[0])); fread(ptr, length, 1, f); // Read in the entire file fclose(f); // Close the file return result; } int main(int argc, char** argv) { cv::Size actual_size(1920, 1080); cv::Size half_size(960, 540); //Read y, u and v in bytes arrays auto y_buffer = readBytesFromFile("ypixel.bin"); auto u_buffer = readBytesFromFile("upixel.bin"); auto v_buffer = readBytesFromFile("vpixel.bin"); cv::Mat y(actual_size, CV_8UC1, y_buffer.data()); cv::Mat u(half_size, CV_8UC1, u_buffer.data()); cv::Mat v(half_size, CV_8UC1, v_buffer.data()); cv::Mat u_resized, v_resized; cv::resize(u, u_resized, actual_size, 0, 0, cv::INTER_NEAREST); //repeat u values 4 times cv::resize(v, v_resized, actual_size, 0, 0, cv::INTER_NEAREST); //repeat v values 4 times cv::Mat yuv; std::vector<cv::Mat> yuv_channels = { y, u_resized, v_resized }; cv::merge(yuv_channels, yuv); cv::Mat bgr; cv::cvtColor(yuv, bgr, cv::COLOR_YUV2BGR); cv::imwrite("bgr.jpg", bgr); return 0; }

使用以下命令进行编译和测试:

g ++ -o yuv2rgb -std = c ++ 11 yuv2rgb.cpp -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_imgproc

g++ -o yuv2rgb -std=c++11 yuv2rgb.cpp -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_imgproc

通过执行以下代码生成以下输出图像:

Following output image is generated by executing the above code:

更多推荐

YUV420到BGR图像的像素指针

本文发布于:2023-11-30 20:39:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   像素   图像   BGR

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!