Note: The yv12 type of data collected from the hikvision camera must be converted to rgb24 for data processing. The data collected from other cameras is generally rgb24, which can be processed directly.
Bool yv12_to_rgb24 (unsigned char * pyv12, unsigned char * prgb24, int iwidth, int iheight)
{
If (! Pyv12 |! Prgb24)
Return false;
Const long nylen = long (iheight * iwidth );
Const int nhfwidth = (iwidth> 1 );
If (nylen <1 | nhfwidth <1)
Return false;
// Yv12 data format, where the Y component length is width * height, and the u and V component lengths are both width * Height/4
// | Width |
// Y... y --------
// Y... y height
// Y... y
// Y... y --------
// V. v
// V. v
// U .. u
// U .. u
Unsigned char * ydata = pyv12; // The first address of Y
Unsigned char * vdata = & ydata [nylen]; // The first address of the U.
Unsigned char * udata = & vdata [nylen> 2]; // The first address of V
If (! Udata |! Vdata)
Return false;
// Convert yv12 to rgb24
Int RGB [3];
Int I, j, m, n, x, y;
M =-iwidth;
N =-nhfwidth;
For (y = 0; y <iheight; y ++)
{
M + = iwidth; // index of an element in the first row
If (! (Y % 2 ))
N + = nhfwidth;
For (x = 0; x <iwidth; X ++)
{
I = m + X;
J = N + (x> 1 );
RGB [2] = int (ydata [I] + 1.370705 * (vdata [J]-128); // R component Value
RGB [1] = int (ydata [I]-0.698001 * (udata [J]-128)-0.703125 * (vdata [J]-128); // G component Value
RGB [0] = int (ydata [I] + 1.732446 * (udata [J]-128); // B component Value
J = nylen-iwidth-M + X;
I = (j <1) + J;
For (Int J = 0; j <3; j ++)
{
If (RGB [J]> = 0 & RGB [J] <= 255)
Prgb24 [I + J] = RGB [J];
Else
Prgb24 [I + J] = (RGB [J] <0 )? 0: 255;
}
}
}
Return true;
}
Note:
Frame_info * pframeinfo;
Iwidth is pframeinfo-> nwidth
Iheight is pframeinfo-> nheight