OpenCV:BGR/RGB转I420(颜色失真),再转NV12
RGB转I420由OpenCV实现,I420转NV12,要自己写代码了。
- RGB转I420
void convert()
{cv::Mat yuv;cv::cvtColor(matBgr, yuv, cv::COLOR_RGB2YUV_I420);I420_TO_NV12(map_info.data, yuv.data, width, height);yuv.release();
}
- I420转NV12
static void I420_TO_NV12(uchar* NV12, uchar* I420, const int width, const int height)
{int ySize = width * height;int uvSize = ySize / 2;memcpy(NV12, I420, ySize);int uvIndex = 0;for (int i = ySize; i < ySize + uvSize; i += 2) {NV12[i+1] = I420[ySize + uvIndex];NV12[i ] = I420[ySize + uvSize/2+uvIndex];uvIndex ++;}
}
- 颜色失真参考
https://blog.csdn.net/quantum7/article/details/153632326
