halcon图片格式和opencv的IPLimage格式的转换

本文介绍如何实现Halcon图像格式与OpenCV的IPLimage格式之间的相互转换。文章详细展示了两种不同通道数(单通道和三通道)图像在两个库之间的转换过程,并强调了转换时需要注意的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}



opencv 转halcon格式:

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格式是无法转的。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值