-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump_yuv.cpp
91 lines (76 loc) · 1.97 KB
/
dump_yuv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
using namespace std;
unsigned char yuvData[1920 * 1080 * 3];
unsigned char *pYuvBuf = yuvData;
static void rgb2nv12(Mat &src, char*filename)
{
std::vector<cv::Mat> rgbChannels(3);
split(src, rgbChannels);
printf("%ld %d %d\n", rgbChannels[0].step[0], rgbChannels[0].cols, rgbChannels[0].rows);
unsigned char *pR = rgbChannels[2].data;
unsigned char *pG = rgbChannels[1].data;
unsigned char *pB = rgbChannels[0].data;
unsigned char *pY = pYuvBuf;
unsigned char *pUV;
for (int i = 0; i < src.rows; i++)
{
pUV = pYuvBuf + src.rows*src.cols + (i/2)*src.cols;
for (int j = 0; j < src.cols; j++)
{
unsigned char r = *pR, g = *pG, b = *pB;
unsigned char Y = (unsigned char)(0.299*r + 0.587*g + 0.144*b);
*pY++ = Y;
if (0 == (j%2))
{
unsigned char U = (unsigned char)(0.713*(r - Y) + 128);
unsigned char V = (unsigned char)(0.564*(b - Y) + 128);
*pUV++ = V;
*pUV++ = U;
}
pR++;
pG++;
pB++;
}
}
FILE* pFileOut0 = fopen(filename, "wb");
if (!pFileOut0)
{
printf("pFileOut open error \n");
exit(-1);
}
fwrite(pYuvBuf, src.rows*src.cols*src.channels(), 1, pFileOut0);
fclose(pFileOut0);
}
static void dumpYV12(Mat &src, char*filename)
{
Mat yuvM;
cvtColor(src, yuvM, CV_BGR2YUV_I420);
printf("[%d, %d, %d]\n", yuvM.channels(), yuvM.cols, yuvM.rows);
memcpy(pYuvBuf, yuvM.data, yuvM.rows*yuvM.cols*yuvM.channels());
FILE* pFileOut0 = fopen(filename, "wb");
if (!pFileOut0)
{
printf("pFileOut open error \n");
exit(-1);
}
fwrite(pYuvBuf, yuvM.rows*yuvM.cols*yuvM.channels(), 1, pFileOut0);
fclose(pFileOut0);
}
int main(int argc, char *argv[])
{
printf("filename: %s\n", argv[1]);
printf("dump to %s\n", argv[2]);
Mat src = imread(argv[1]);
if (src.empty())
{
printf("can not open %s\n", argv[1]);
exit(-1);
}
rgb2nv12(src, argv[2]);
dumpYV12(src, argv[2]);
return 0;
}