opencv-Win32下HBITMAP格式圖像 Convert to爲opencv下 IplImage

Code:
  1. // hbitmap convert to IplImage   
  2. IplImage* hBitmapToIpl(HBITMAP hBmp)   
  3. {   
  4.     BITMAP bmp;    
  5.     GetObject(hBmp,sizeof(BITMAP),&bmp);    
  6.   
  7.     // get channels which equal 1 2 3 or 4    
  8.     // bmBitsPixel :   
  9.     // Specifies the number of bits    
  10.     // required to indicate the color of a pixel.    
  11.     int nChannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8 ;   
  12.   
  13.     // get depth color bitmap or grayscale   
  14.     int depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;    
  15.        
  16.        
  17.     // create header image   
  18.     IplImage* img = cvCreateImage(cvSize(bmp.bmWidth,bmp.bmHeight),depth,nChannels);    
  19.        
  20.     // allocat memory for the pBuffer   
  21.     BYTE *pBuffer = new BYTE[bmp.bmHeight*bmp.bmWidth*nChannels];    
  22.        
  23.     // copies the bitmap bits of a specified device-dependent bitmap into a buffer   
  24.     GetBitmapBits(hBmp,bmp.bmHeight*bmp.bmWidth*nChannels,pBuffer);    
  25.        
  26.     // copy data to the imagedata   
  27.     memcpy(img->imageData,pBuffer,bmp.bmHeight*bmp.bmWidth*nChannels);   
  28.     delete pBuffer;    
  29.   
  30.     // create the image   
  31.     IplImage *dst = cvCreateImage(cvGetSize(img),img->depth,3);   
  32.     // convert color   
  33.     cvCvtColor(img,dst,CV_BGRA2BGR);   
  34.     cvReleaseImage(&img);   
  35.     return dst;   
  36. }  

 

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