我使用的是海康DS-2CD852MF-E, 200万,网络摄像机,已经比较老了,不过SDK在海康官网下载的,开发流程都差不多.
海康摄像机回调解码后的视频数据格式为YV12,顺便说一下YV12的数据格式 YYYY V U. 我这个是720P,即1280 * 720分辨率. 那么Y分量的数量为 1280 * 720 = 921600 字节, V = U 数量为Y的1/4, 即921600 / 4 = 230400字节,所以一帧720P的YV12数据量为1382400字节,与断点调试查看nSize一致.
这篇博客实现方法均来自网上找的代码,不是自己写的.仅作为一个记录.有一篇博客YUV与RGB格式转换 里面就是YV12转到BGR的实现,看着它的代码改的.
1.用OpenCV 颜色空间转换函数
Mat pImgYCrCb, pImg;
pImgYCrCb.create(720, 1280,CV_8UC3); pImg.create(720, 1280,CV_8UC3); yv12toYUV(pImgYCrCb.data, (uchar*)pBuf, pFrameInfo->nWidth,pFrameInfo->nHeight,pImgYCrCb.step); cvtColor(pImgYCrCb, pImg, CV_YCrCb2RGB);//转化为RGB图像,pImg是输出的帧
void yv12toYUV(uchar *outYuv, uchar *inYv12, int width, int height,int widthStep) { int col,row; unsigned int Y,U,V; int tmp; int idx; for (row=0; row<height; row++) { idx=row * widthStep; int rowptr=row*width; for (col=0; col<width; col++) { tmp = (row/2)*(width/2)+(col/2); Y=(unsigned int) inYv12[row*width+col]; U=(unsigned int) inYv12[width*height+width*height/4+tmp]; V=(unsigned int) inYv12[width*height+tmp]; outYuv[idx+col*3] = Y; outYuv[idx+col*3+1] = U; outYuv[idx+col*3+2] = V; } } }
2.另一种opencv cvtColor实现,较为简单
Mat dst(pFrameInfo->nHeight,pFrameInfo->nWidth,CV_8UC3);//这里nHeight为720,nWidth为1280,8UC3表示8bit uchar 无符号类型,3通道值 Mat src(pFrameInfo->nHeight + pFrameInfo->nHeight/2,pFrameInfo->nWidth,CV_8UC1,(uchar*)pBuf); cvtColor(src,dst,CV_YUV2BGR_YV12); imshow("bgr",dst); waitKey(1);
3.只保留Y分量,观看黑白视频
uchar Y[921600];
memcpy(Y, pBuf, 921600); if (Videofile==NULL) { sprintf_s(filename,"is_Y.yuv",0); fopen_s(&Videofile,filename,"wb"); } fwrite(Y,921600,1,Videofile);
4.ffmpeg转换
int sizeRGB = pFrameInfo->nWidth * pFrameInfo->nHeight * 3; uchar* pBGR24 = new unsigned char[sizeRGB];//开辟存储一副图像大小的内存空间,因为这里是RGB图像,所以需要 1280 * 720 * 3的大小 memset(pBGR24, 0, sizeRGB); YV12ToBGR24_FFmpeg((uchar*)pBuf, pBGR24,1280, 720); Mat BGR24_IMG(pFrameInfo->nHeight,pFrameInfo->nWidth,CV_8UC3,pBGR24); imshow("bgr24", BGR24_IMG); waitKey(1); delete []pBGR24;
bool YV12ToBGR24_FFmpeg(unsigned char* pYUV,unsigned char* pBGR24,int width,int height) { if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL) return false; AVPicture pFrameYUV,pFrameBGR; avpicture_fill(&pFrameYUV,pYUV,AV_PIX_FMT_YUV420P,width,height); //U,V互换 uint8_t * ptmp=pFrameYUV.data[1]; pFrameYUV.data[1]=pFrameYUV.data[2]; pFrameYUV.data [2]=ptmp; avpicture_fill(&pFrameBGR,pBGR24,AV_PIX_FMT_BGR24,width,height); struct SwsContext* imgCtx = NULL; imgCtx = sws_getContext(width,height,AV_PIX_FMT_YUV420P,width,height,AV_PIX_FMT_BGR24,SWS_BILINEAR,0,0,0); if (imgCtx != NULL){ sws_scale(imgCtx,pFrameYUV.data,pFrameYUV.linesize,0,height,pFrameBGR.data,pFrameBGR.linesize); if(imgCtx){ sws_freeContext(imgCtx); imgCtx = NULL; } return true; } else{ sws_freeContext(imgCtx); imgCtx = NULL; return false; } }
注意这里ffmpeg会出现函数被否决,需要用新接口替换,也可以忽略这个警告,并且头文件需要用extern "C"包含
extern "C" { #include <libavcodec\avcodec.h> #include <libswscale\swscale.h> #include <libavutil\pixfmt.h> #include <libavutil\imgutils.h> } #pragma warning(disable: 4996)//忽略被否决的警告