ref: https://blog.csdn.net/weixin_38419133/article/details/98496722
NV21格式是安卓手机上普遍使用的图像格式。
NV12和NV21属于YUV420格式(每2x2四个Y,共用一组uv),是一种two-plane模式,即Y和UV分为两个Plane,但是UV(CbCr)为交错存储,而不是分为三个plane。先存储所有的Y,然后UV交错存储:NV12先U后V,NV21先V后U。
NV21:
NV12:
可以看到NV21 和NV12 的储存形式是Y分量和U、V分量进行分离的,而不像RBG的存储格式那样是进行对储存的。
转换代码(opencv C++):
int cv2yuvNv21(const cv::Mat& Img, unsigned char *yuvbuff)
{
if (Img.empty())
{
std::cout << "empty!check your image" << std::endl;
return -1;
}
if (yuvbuff == nullptr)
{
std::cout << "output buff is null" << std::endl;
return -1;
}
int cols = Img.cols;
int rows = Img.rows;
int Yindex = 0;
int UVindex = rows * cols;
//unsigned char* yuvbuff = new unsigned char[1.5 * rows * cols];
cv::Mat NV21(rows + rows / 2, cols, CV_8UC1);
cv::Mat OpencvYUV;
cv::Mat OpencvImg;
cv::cvtColor(Img, OpencvYUV, cv::COLOR_BGR2YUV_YV12);
int UVRow{ 0 };
for (int i = 0; i<rows; i++)
{
for (int j = 0; j<cols; j++)
{
uchar* YPointer = NV21.ptr<uchar>(i);
int B = Img.at<cv::Vec3b>(i, j)[0];
int G = Img.at<cv::Vec3b>(i, j)[1];
int R = Img.at<cv::Vec3b>(i, j)[2];
//计算Y的值
int Y = (77 * R + 150 * G + 29 * B) >> 8;
YPointer[j] = Y;
yuvbuff[Yindex++] = (Y < 0) ? 0 : ((Y > 255) ? 255 : Y);
uchar* UVPointer = NV21.ptr<uchar>(rows + i / 2);
//计算U、V的值,进行2x2的采样
if (i % 2 == 0 && (j) % 2 == 0)
{
int U = ((-44 * R - 87 * G + 131 * B) >> 8) + 128;
int V = ((131 * R - 110 * G - 21 * B) >> 8) + 128;
UVPointer[j] = V;
UVPointer[j + 1] = U;
yuvbuff[UVindex++] = (V < 0) ? 0 : ((V > 255) ? 255 : V);
yuvbuff[UVindex++] = (U < 0) ? 0 : ((U > 255) ? 255 : U);
}
}
}
return 0;
}