最近做海康网络摄像机的时候碰到需要将YV12格式图像转换到IplImage让OpenCV来处理它,考虑到效率问题,所以用空间来换取时间。结果还是可以接受的,一秒钟转换1280 * 720的图像30帧左右,足矣。
YV12的格式请参考:
1.http://zyg0227.blog.51cto.com/1043164/295479
2.http://zh.wikipedia.org/wiki/YUV
Code.
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
uchar* table_r = 0;
uchar* table_g = 0;
uchar* table_b = 0;
void YV12_2_IplImage(const uchar* yvData, IplImage* img)
{
uchar* pData = (uchar*)img->imageData;
uchar* y = (uchar*)yvData;
uchar* u = y + img->imageSize / 3;
uchar* v = y + img->imageSize / 3 + ((img->width / 2 + img->w