Opencv中取得的视频数据流IplImage转为BMP

本文介绍了如何利用OpenCV从USB摄像头获取的24位视频数据流,并将其转换为BMP图片格式的实现过程。通过定义自定义结构体并编写转换函数,实现了视频帧到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;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值