【图像-视频处理】YUV420、YV12与RGB24的转换公式
bool YV12ToBGR24_Native(unsigned char* pYUV,unsigned char* pBGR24,int width,int height) { if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL) return false; const long len = width * height; unsigned char* yData = pYUV; unsigned char* vData = &yData[len]; unsigned char* uData = &vData[len >> 2]; int bgr[3]; int yIdx,uIdx,vIdx,idx; for (int i = 0;i < height;i++){ for (int j = 0;j < width;j++){ yIdx = i * width + j; vIdx = (i/2) * (width/2) + (j/2); uIdx = vIdx; bgr[0] = (int)(yData[yIdx] + 1.732446 * (uData[vIdx] - 128)); // b分量 bgr[1] = (int)(yData[yIdx] - 0.698001 * (uData[uIdx] - 128) - 0.703125 * (vData[vIdx] - 128)); // g分量 bgr[2] = (int)(yData[yIdx] + 1.370705 * (vData[uIdx] - 128)); // r分量 for (int k = 0;k < 3;k++){ idx = (i * width + j) * 3 + k; if(bgr[k] >= 0 && bgr[k] <= 255) pBGR24[idx] = bgr[k]; else pBGR24[idx] = (bgr[k] < 0)?0:255; } } } return true; }
以上是yv12到RGB24的转换算法,如果是yuv420到RGB24转换,秩序u,v反过来就可以了。
即:
unsigned char* uData = &yData[nYLen]; unsigned char* vData = &vData[nYLen>>2];
---
注:海康威视网络摄像头一般就是yu12格式的!
---
2016-9-22 19:53
张朋艺 pyZhangBIT2010@126.com