如何将yuv420p图像数据转换为RGB数据并使用opencv保存为jpg图片

yuv420是用4个byte存储4个Y的信息,用1个Byte存储U的信息,一个Byte存储V的信息,
这4个Y共用这2个U和V ,也就是用6个Byte 存储4个像素信息,也就是一个像素需要12个Bits(6*8/4),也就是12bpp。
注意yuv420p里面的p是指planar,也就是分层存储,先存全部Y的信息,然后是U的信息,最后是V的信息,
或者这样说: 如果把一个图片按yuv420p的格式保存为二进制数据文件,那么把这个文件均分为6份,那么前面的4份是Y,第5份是U,第6份是V。

以下程序的流程是:

1 使用 ffmpeg 先将一张图片保存为 yuv420p的数据文件。
2 使用下面的程序读取此文件,然后将yuv数据转为rgb,最后使用opencv保存为jpg格式的图片。
3 主要是熟悉yuv420p的数据格式,以及熟悉yuv如何转为RGB。
4 最后就是使用opencv将RGB数据保存为图片,注意opencv是需要BGR的数据.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <string.h>
#include <sys/mman.h>
#include <assert.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

using namespace std;
using namespace cv;

#define  IMAGE_W 1280
#define  IMAGE_H 720

#define  FILE_NAME "output420.yuv"

int convert_yuv_to_rgb_pixel(int y, int u, int v) {
	unsigned int pixel32 = 0;
	unsigned char *pixel = (unsigned char*) &pixel32;
	int r, g, b;
	r = y + (1.370705 * (v - 128));
	g = y - (0.698001 * (v - 128)) - (0.337633 * (u - 128));
	b = y + (1.732446 * (u - 128));

	if (r > 255)
		r = 255;
	if (g > 255)
		g = 255;
	if (b > 255)
		b = 255;
	if (r < 0)
		r = 0;
	if (g < 0)
		g = 0;
	if (b < 0)
		b = 0;
   //注意opencv是需要BGR的数据
	pixel[0] = b;  // blue
	pixel[1] = g;  // green
	pixel[2] = r;  //red
	return pixel32;
}

int convert_yuv420p_to_rgb888_buffer(unsigned char *yuv, unsigned char *rgb,
		unsigned int width, unsigned int height) {
	unsigned int pixel32;
	unsigned char *ptr = rgb;
	int y0, y1, y2, y3, u, v;
	int i = 0;
	unsigned char *a = yuv + width * height * 0;
	unsigned char *b = yuv + width * height * 1;
	unsigned char *c = yuv + (int) (width * height * 1.25);

	while (i < width * height) {
		i += 4;
		u = *(b++);
		v = *(c++);
		for (int j = 0; j < 4; j++) {
			y0 = *(a++);
			pixel32 = convert_yuv_to_rgb_pixel(y0, u, v);
			*(ptr++) = (pixel32 & 0x000000ff);
			*(ptr++) = (pixel32 & 0x0000ff00) >> 8;
			*(ptr++) = (pixel32 & 0x00ff0000) >> 16;
		}

	}
	return 0;
}

/**
 * 	 yuyv420p to rgb to jpg.
 *   ffmpeg -i in.jpg -pix_fmt yuv420p output420.yuv -y
 *   ffmpeg  -pix_fmts
 *   yuyv420 use 12bits to store 1 pixes.
 *
 */
int main() {
	int ret;
	int fd = open(FILE_NAME, O_RDWR);
	if (ret == -1) {
		perror("open");
		return -1;
	}
	unsigned char yuv[IMAGE_W * IMAGE_H * 3 + 1000];

	ret = read(fd, yuv, IMAGE_W * IMAGE_H * 3 + 1000);
	if (ret == -1) {
		perror("read");
		return -1;
	}

	assert(ret == IMAGE_W * IMAGE_H * 12 / 8);
	char rgb24[IMAGE_W * IMAGE_H * 3];
	convert_yuv420p_to_rgb888_buffer((unsigned char*) yuv,
			(unsigned char*) rgb24,
			IMAGE_W, IMAGE_H);
	Mat image(IMAGE_H, IMAGE_W, CV_8UC3, rgb24);
	imwrite("output.jpg", image);
	cout << "success" << endl;
	return 0;
}

如果有任何问题,欢迎评论。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用 FFmpeg 框架将 YVU420SP 格式的 buffer 转换YUV 数据格式,然后将 YUV 数据转换RGB 数据格式,并最终用 OpenCVRGB 数据写入图像文件,您可以按照以下步骤进行操作: 1. 首先,确保已经正确配置和安装了 FFmpeg 库和 OpenCV 库,以及相关的头文件和链接库。 2. 创建一个 FFmpeg 的 `AVFrame` 对象,并分配足够的内存空间,用于存储 YUV 数据。 ```cpp AVFrame* frame = av_frame_alloc(); frame->format = AV_PIX_FMT_YUV420P; frame->width = width; frame->height = height; av_frame_get_buffer(frame, 0); ``` 3. 将 YVU420SP 格式的 buffer 数据复制到 `AVFrame` 对象中。 ```cpp // 假设 buffer 是存储 YVU420SP 数据的缓冲区 uint8_t* srcData[3] = { buffer, nullptr, nullptr }; int srcLinesize[3] = { width, width / 2, width / 2 }; // 将 buffer 数据复制到 AVFrame 对象中 av_image_fill_arrays(frame->data, frame->linesize, srcData, AV_PIX_FMT_YUV420P, width, height, 1); ``` 4. 创建一个 FFmpeg 的 `SwsContext` 对象,用于进行 YUVRGB 的图像转换。 ```cpp SwsContext* swsContext = sws_getContext(width, height, AV_PIX_FMT_YUV420P, width, height, AV_PIX_FMT_RGB24, 0, nullptr, nullptr, nullptr); ``` 5. 创建一个 `cv::Mat` 对象,用于存储 RGB 数据。 ```cpp cv::Mat rgbImage(height, width, CV_8UC3); ``` 6. 使用 `sws_scale` 函数将 YUV 数据转换RGB 数据。 ```cpp uint8_t* dstData[1] = { rgbImage.data }; int dstLinesize[1] = { 3 * width }; sws_scale(swsContext, frame->data, frame->linesize, 0, height, dstData, dstLinesize); ``` 7. 最后,使用 OpenCV 的 `cv::imwrite` 函数将 RGB 数据写入图像文件。 ```cpp cv::imwrite("output.jpg", rgbImage); ``` 记得在程序结束前进行清理工作: ```cpp // 释放资源 sws_freeContext(swsContext); av_frame_free(&frame); ``` 请注意,上述代码中的变量和函数调用需要根据实际情况进行修改,并且需要正确链接 FFmpegOpenCV 库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值