首页 > 代码库 > Halcon 10.0:Hobject图像转CBitmap
Halcon 10.0:Hobject图像转CBitmap
1 void HImage2CBitmap(Hobject pImage,CBitmap *wImage) 2 { 3 char lpcsType[MAX_STRING]; 4 Hlong lPointer,width,height,channels; 5 Hlong lPointerR,lPointerG,lPointerB; 6 count_channels(pImage,&channels); 7 //获取Halcon数据的Long指针 8 if (channels == 3) 9 {10 get_image_pointer3(pImage,&lPointerR,&lPointerG,&lPointerB,lpcsType,&width,&height);11 }else12 {13 get_image_pointer1(pImage,&lPointer,lpcsType,&width,&height);14 }15 16 17 //创建文件头18 BYTE tmp[sizeof(BITMAPINFO)+1024];19 BITMAPINFO *bmi = (BITMAPINFO*)tmp;20 HBITMAP hBmp;21 22 memset(bmi,0,sizeof(BITMAPINFO));23 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);24 bmi->bmiHeader.biWidth = width;25 bmi->bmiHeader.biHeight = -height; //正负可以上下翻转图像26 bmi->bmiHeader.biPlanes = 1;27 bmi->bmiHeader.biBitCount = 8*channels;28 bmi->bmiHeader.biCompression = BI_RGB;29 bmi->bmiHeader.biSizeImage = 0; //if biCompression is BI_RGB,this can be 030 bmi->bmiHeader.biClrImportant =0 ;31 //初始化数据32 switch(8*channels) 33 { 34 case 8 : 35 for(int i=0 ; i < 256 ; i++){ 36 bmi->bmiColors[i].rgbBlue = i;37 bmi->bmiColors[i].rgbGreen= i;38 bmi->bmiColors[i].rgbRed= i;39 }40 break;41 case 32:42 case 24: 43 ((DWORD*) bmi->bmiColors)[0] = 0x00FF0000;44 ((DWORD*) bmi->bmiColors)[1] = 0x0000FF00;45 ((DWORD*) bmi->bmiColors)[2] = 0x000000FF;46 break; 47 } 48 hBmp = ::CreateDIBSection(NULL,bmi,DIB_RGB_COLORS,NULL,0,0);49 50 //Halcon转换成BYTE数据51 BYTE *pData = http://www.mamicode.com/new BYTE[width*height*channels];52 long byteStep = 0;53 if (channels == 3)54 {55 byteStep = 3*width;56 BYTE *pHimageR = (BYTE*)lPointerR;57 BYTE *pHimageG = (BYTE*)lPointerG;58 BYTE *pHimageB = (BYTE*)lPointerB;59 for (int i=0; i<height; ++i)60 {61 for (int j=0; j<width; ++j)62 {63 *(pData + i*byteStep + 3*j + 0) = *pHimageB;64 *(pData + i*byteStep + 3*j + 1) = *pHimageG;65 *(pData + i*byteStep + 3*j + 2) = *pHimageR;66 pHimageR++;67 pHimageG++;68 pHimageB++;69 }70 }71 }else72 {73 byteStep = width;74 BYTE *pHimage = (BYTE*)lPointer;75 for (int i=0; i<height; ++i)76 {77 for (int j=0; j<width; ++j)78 {79 *(pData + i*byteStep + j) = *pHimage;80 pHimage++;81 }82 }83 }84 85 //BYTE数据拷贝86 SetDIBits(NULL,hBmp,0,height,pData,bmi,DIB_RGB_COLORS);87 88 //CBitmap关联HBITMAP89 wImage->Attach(hBmp);90 91 delete [] pData;92 93 return;94 }
对于灰度图像和彩色图像基本能使用,只测试过宽为4的倍数的图像。另外速度和效率可能不够用,500W的彩色图像(约14M)时间估计在1s左右。
有能力的可以自行优化下或留言交流下想法。
Halcon 10.0:Hobject图像转CBitmap
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。