以下将一个yuv444的二进制文件转换为jpg图片,
yuv444的数据来源于以下:
ffmpeg -i in.jpg -pix_fmt yuv444p output444.yuv -y
注意 yuv444个存储是分层存储,也就是先存Y的值,然后是U的值,最后是V的值。
#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 800
#define IMAGE_H 1067
#define FILE_NAME "output444.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;
pixel[0] = b; // blue
pixel[1] = g; // green
pixel[2] = r; //red
return pixel32;
}
int convert_yuv444_to_rgb888_buffer(unsigned char *yuv, unsigned char *rgb,
unsigned int width, unsigned int height) {
unsigned int pixel32;
unsigned char *ptr = rgb;
int y, u, v;
int i = 0;
unsigned char *a = yuv +width * height * 0;
unsigned char *b = yuv +width * height * 1;
unsigned char *c =yuv + width * height * 2;
while (i < width * height) {
i++;
y = *(a++);
u = *(b++);
v = *(c++);
pixel32 = convert_yuv_to_rgb_pixel(y, u, v);
*(ptr++) = (pixel32 & 0x000000ff);
*(ptr++) = (pixel32 & 0x0000ff00) >> 8;
*(ptr++) = (pixel32 & 0x00ff0000) >> 16;
}
return 0;
}
/**
* yuyv422 to rgb to jpg.
* ffmpeg -i in.jpg -pix_fmt yuv444p output444.yuv -y
* ffmpeg -pix_fmts
* yuyv444 use 3bytes to store 3 pixes.
* Y,Cr,Cb.
* every pixel : Y,Cr,Cb.
*
*/
int main() {
int ret;
int fd = open(FILE_NAME, O_RDONLY);
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 * 3);
char rgb24[IMAGE_W * IMAGE_H * 3];
convert_yuv444_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; // prints !!!Hello World!!!
return 0;
}
有任何问题,欢迎评论.