本文主要是介绍halcon图片格式和opencv的IPLimage格式的转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
halcon图片格式和opencv的IPLimage格式的转换:
halcon转opencv格式:
IplImage* HImageToIplImage(Hobject &Hobj)
{IplImage* pImage;HTuple htChannels;char cType[MAX_STRING];Hlong width,height;width=height=0;//转换图像格式convert_image_type(Hobj,&Hobj,"byte");count_channels(Hobj,&htChannels);if(htChannels[0].I()==1){uchar* ptr;get_image_pointer1(Hobj,(Hlong*)&ptr,cType,&width,&height);pImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);for(int i=0;i<height;i++){memcpy(pImage->imageData+pImage->widthStep*i,ptr+width*i,width);}}if(htChannels[0].I()==3){uchar *ptrRed , *ptrGreen , *ptrBlue;ptrRed=ptrGreen=ptrBlue=NULL;get_image_pointer3(Hobj,(Hlong*)&ptrRed,(Hlong*)&ptrGreen,(Hlong*)&ptrBlue,cType,&width,&height);IplImage *pImageRed , *pImageGreen , *pImageBlue ;pImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);pImageRed=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);pImageGreen=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);pImageBlue=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);for(int i=0;i<height;i++){memcpy(pImageRed->imageData+pImageRed->widthStep*i , ptrRed+width*i , width);memcpy(pImageGreen->imageData+pImageGreen->widthStep*i , ptrGreen+width*i , width);memcpy(pImageBlue->imageData+pImageBlue->widthStep*i , ptrBlue+width*i , width);}cvMerge(pImageBlue,pImageGreen,pImageRed,NULL,pImage);cvReleaseImage(&pImageRed);cvReleaseImage(&pImageGreen);cvReleaseImage(&pImageBlue);}return pImage;
}
Hobject IplImageToHImage(IplImage* pImage)
{Hobject Hobj;if(pImage->nChannels==1){int height=pImage->height;int width=pImage->width;uchar *dataGray=new uchar[width*height];for(int i=0; i<height; i++){memcpy(dataGray+width*i, pImage->imageData+pImage->widthStep*i,width);}gen_image1(&Hobj,"byte",pImage->width,pImage->height,(Hlong)(dataGray));delete[ ] dataGray;}if(pImage->nChannels==3){IplImage *IplImageRed, *IplImageGreen, *IplImageBlue;IplImageRed=cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,1);IplImageGreen=cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,1);IplImageBlue=cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,1);cvSplit(pImage, IplImageBlue, IplImageGreen, IplImageRed,NULL);uchar* dataRed=new uchar[pImage->width*pImage->height];uchar* dataGreen=new uchar[pImage->width*pImage->height];uchar* dataBlue=new uchar[pImage->width*pImage->height];int height=pImage->height;int width=pImage->width;for(int i=0; i<height; i++){memcpy(dataRed+width*i, IplImageRed->imageData+IplImageRed->widthStep*i,width);memcpy(dataGreen+width*i, IplImageGreen->imageData+IplImageGreen->widthStep*i,width);memcpy(dataBlue+width*i, IplImageBlue->imageData+IplImageBlue->widthStep*i,width);}gen_image3(&Hobj,"byte",pImage->width,pImage->height,(Hlong)(dataRed),(Hlong)(dataGreen),(Hlong)(dataBlue));cvReleaseImage(&IplImageRed);cvReleaseImage(&IplImageGreen);cvReleaseImage(&IplImageBlue);delete[ ] dataRed;delete[ ] dataGreen;delete[ ] dataBlue;}return Hobj;
}
在halcon转opencv的时候注意,必须为halcon的image格式,halcon的region格式是无法转的。
这篇关于halcon图片格式和opencv的IPLimage格式的转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!