halcon HImage 与opencv 里的 IplImage互相转换

  1. Hobject IplImageToHImage(IplImage *pImage)  
  2. {    
  3.     Hobject Hobj;  
  4.     if (3 == pImage->nChannels)  
  5.     {  
  6.         IplImage *pImageRed,*pImageGreen,*pImageBlue;  
  7.         pImageRed = cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,1);  
  8.         pImageGreen = cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,1);  
  9.         pImageBlue = cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,1);  
  10.         cvSplit(pImage,pImageBlue,pImageGreen,pImageRed,NULL);  
  11.         uchar *dataRed = new uchar[pImage->width*pImage->height];  
  12.         uchar *dataGreen = new uchar[pImage->width*pImage->height];;  
  13.         uchar *dataBlue = new uchar[pImage->width*pImage->height];;  
  14.                
  15.         int height = pImage->height;  
  16.         int width =pImage->width;  
  17.         for(int i = 0; i<height; i++)  
  18.         {  
  19.             memcpy(dataRed + width*i,pImageRed->imageData + pImageRed->widthStep*i, width);  
  20.             memcpy(dataGreen + width*i,pImageGreen->imageData + pImageGreen->widthStep*i, width);  
  21.             memcpy(dataBlue + width*i,pImageBlue->imageData + pImageBlue->widthStep*i, width);  
  22.         }  
  23.         gen_image3(&Hobj,"byte",pImage->width,pImage->height,(Hlong)(dataRed),(Hlong)(dataGreen),(Hlong)(dataBlue));  
  24.         cvReleaseImage(&pImageRed);  
  25.         cvReleaseImage(&pImageGreen);  
  26.         cvReleaseImage(&pImageBlue);  
  27.         delete[] dataRed;  
  28.         delete[] dataGreen;  
  29.         delete[] dataBlue;  
  30.     }  
  31.     if (1 == pImage->nChannels)  
  32.     {  
  33.         int height = pImage->height;  
  34.         int width =pImage->width;  
  35.         uchar *dataGray = new uchar[width*height];  
  36.                
  37.         for(int i = 0; i<height; i++)  
  38.         {  
  39.             memcpy(dataGray + width*i,pImage->imageData + pImage->widthStep*i, width);  
  40.         }  
  41.         gen_image1(&Hobj,"byte",pImage->width,pImage->height,(Hlong)(dataGray));  
  42.         delete[] dataGray;  
  43.     }     
  44.     return Hobj;  
  45. }  


[cpp]  view plain  copy
  1. IplImage* HImageToIplImage(Hobject &Hobj)  
  2. {  
  3.     IplImage *pImage;  
  4.     HTuple htChannels;  
  5.     char cType[MAX_STRING];  
  6.     Hlong     width,height;  
  7.     width = height =0;    
  8.     //转换图像格式  
  9.     convert_image_type(Hobj,&Hobj,"byte");  
  10.     count_channels(Hobj,&htChannels);  
  11.     if (htChannels[0].I() == 1)    
  12.     {  
  13.         unsigned char *ptr;  
  14.         get_image_pointer1(Hobj,(Hlong *)&ptr,cType,&width,&height);  
  15.         pImage = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);  
  16.         for(int i = 0; i<height; i++)  
  17.         {  
  18.             memcpy(pImage->imageData+ pImage->widthStep*i, ptr + width*i, width);  
  19.         }  
  20.     }  
  21.     if (htChannels[0].I() == 3)    
  22.     {  
  23.        
  24.         unsigned char *ptrRed,*ptrGreen,*ptrBlue;  
  25.         ptrRed = ptrGreen =ptrBlue = NULL;  
  26.        
  27.         get_image_pointer3(Hobj,(Hlong *)&ptrRed,(Hlong *)&ptrGreen,(Hlong *)&ptrBlue,cType,&width,&height);  
  28.        
  29.         IplImage *pImageRed,*pImageGreen,*pImageBlue;  
  30.         pImage = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);  
  31.         pImageRed = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);  
  32.         pImageGreen = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);  
  33.         pImageBlue = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);  
  34.         for(int i = 0; i<height; i++)  
  35.         {  
  36.             memcpy(pImageRed->imageData+ pImageRed->widthStep*i, ptrRed + width*i, width);  
  37.             memcpy(pImageGreen->imageData+ pImageGreen->widthStep*i, ptrGreen + width*i, width);  
  38.             memcpy(pImageBlue->imageData+ pImageBlue->widthStep*i, ptrBlue + width*i, width);  
  39.         }  
  40.         cvMerge(pImageBlue,pImageGreen,pImageRed,NULL,pImage);  
  41.         cvReleaseImage(&pImageRed);  
  42.         cvReleaseImage(&pImageGreen);  
  43.         cvReleaseImage(&pImageBlue);  
  44.     }    
  45.     return pImage;  
  46. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值