CBitmap載入IDB 4通道向3通道轉化 IplImage

CBitmap Bmp;//載入位圖 
 Bmp.LoadBitmap(IDB_BITMAP_BK_IMG); 
 HBITMAP hBmp = HBITMAP(Bmp);//將CBitmap轉換爲HBITMAP
 BITMAP bmp; 
 Bmp.GetBitmap(&bmp);//獲得位圖信息
 int depth,nChannels;
 if(bmp.bmBitsPixel==1)//得到圖像深度和通道數
  {depth=IPL_DEPTH_1U;  nChannels=1;
  } else {
  depth=IPL_DEPTH_8U;
  nChannels=bmp.bmBitsPixel/8;
 }
 IplImage* m_load_image = cvCreateImage(cvSize(bmp.bmWidth,bmp.bmHeight), depth, nChannels); //創建圖像

 BYTE *pBuffer = new BYTE[bmp.bmHeight*bmp.bmWidth*nChannels]; //創建緩衝區
 GetBitmapBits(hBmp, bmp.bmHeight*bmp.bmWidth*nChannels, pBuffer); //將位圖信息複製到緩衝區
 memcpy(m_load_image->imageData, pBuffer, bmp.bmHeight*bmp.bmWidth*nChannels);//將緩衝區信息複製給IplImage
 int destimgSize;
 destimgSize = m_load_image->height*m_load_image->width*3;
 int srcImgTotalSize;
 srcImgTotalSize = m_load_image->widthStep*m_load_image->height;
 char * buffer = new char[destimgSize];//用於存儲處理後的圖片數據
 IplImage* m_bk_image = cvCreateImage(cvGetSize(m_load_image),IPL_DEPTH_8U,3);
 int destCout=0;
 char * pSrcData = m_load_image->imageData;
 for (int i=1;i<=srcImgTotalSize;i++)
 {
  if (i%4 == 0)
  {
   continue;
  }
  buffer[destCout] = *(pSrcData + i-1);
  destCout++;
 }
 m_bk_image->imageData = buffer;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章