Opencv中取得的视频数据流IplImage转为BMP
在opencv中文网站上使用的开源库取得的视频数据流是指向IplImage结构的指针,试了几USB摄像头取下来的数据都是24b的,所以转为BMP图片直接简单的方法
代码如下:
typedef struct VideoDataDibStruct
{
?DWORD height;
?DWORD width;
?DWORD lineBytes;
?DWORD lineBytes24;
?DWORD totalSize;
?BYTE pData[1024*225];
}VIDEODATADIB;
bool IplImage2Bmp(IplImage *img, VIDEODATADIB *bmpData)
{
?DWORD height = img->height;
?DWORD width = img->width;
?DWORD lineBytes = (width*8+31)/32*4;
?DWORD lineBytes24 = (width*24+31)/32*4;
?bmpData->height = height;
?bmpData->width = width;
?bmpData->lineBytes = lineBytes;
?bmpData->lineBytes24 = lineBytes24;
?bmpData->totalSize = height*lineBytes24;
?WORD bitCount;
?if(img->nChannels == 1)
?{
??bitCount = 8;
?}
?else if(img->nChannels == 3)
?{
??bitCount = 24;
?}
?else
?{
??return false;
?}
?if(bitCount==8)
?{
??for(int i=0; i<img->height; i++)
??{
???for(int j=0,n=0; j<img->width*3; j++,n++)
???{
????*((bmpData->pData) + lineBytes24 * (height-1-i)+j) = (BYTE)((uchar*)(img->imageData+img->widthStep*i))[n];
????j++;
????*((bmpData->pData) + lineBytes24 * (height-1-i)+j) = (BYTE)((uchar*)(img->imageData+img->widthStep*i))[n];
????j++;
????*((bmpData->pData) + lineBytes24 * (height-1-i)+j) = (BYTE)((uchar*)(img->imageData+img->widthStep*i))[n];
???}
??}
?}
?else
?{
??for(int i=0; i<img->height; i++)
??{
???for(int j=0,n=0; j<img->width*3; j++,n++)
???{
????*((bmpData->pData) + lineBytes24 * (height-1-i)+j) = (BYTE)((uchar*)(img->imageData+img->widthStep*i))[3*n];
????j++;
????*((bmpData->pData) + lineBytes24 * (height-1-i)+j) = (BYTE)((uchar*)(img->imageData+img->widthStep*i))[3*n+1];
????j++;
????*((bmpData->pData) + lineBytes24 * (height-1-i)+j) = (BYTE)((uchar*)(img->imageData+img->widthStep*i))[3*n+2];
???}
??}
?}
?return true;
}