halcon变量,图像与C++的一些转换(未完待续)

1、将HTuple转化为C++的类型

将Local control variables(HTuple类型)转化为C++基本类型,其中I()代表int类型,L()代表转化为long类型,Num()代表返回数据个数,D()代表转化为double类型,S()代表转换为string类型。

HTuple hTuple = 1234;

 

int i = hTuple[0].I(); // i=1234
long l = hTuple[0].L(); // l=1234
long lNumber = hTuple.Num(); // lNumber=1,数据个数
double d = hTuple[0].D(); // d=1234.0000

hTuple = "1234";

 

CString strBuffer = hTuple[0].S(); // strBuffer="1234"

 

2、OpenCV中的Mat类型与Halcon中的HObject类型相互转化

 

HObject Mat2HObject(Mat& image)
{
	HObject Hobj=HObject();
	int hgt=image.rows;
	int wid=image.cols;
	int i;
	//	CV_8UC3
	if(image.type() == CV_8UC3)
	{
		vector<Mat> imgchannel;
		split(image,imgchannel);
		Mat imgB=imgchannel[0];
		Mat imgG=imgchannel[1];
		Mat imgR=imgchannel[2];
		uchar* dataR=new uchar[hgt*wid];
		uchar* dataG=new uchar[hgt*wid];
		uchar* dataB=new uchar[hgt*wid];
		for(i=0;i<hgt;i++)
		{
			memcpy(dataR+wid*i,imgR.data+imgR.step*i,wid);
			memcpy(dataG+wid*i,imgG.data+imgG.step*i,wid);
			memcpy(dataB+wid*i,imgB.data+imgB.step*i,wid);
		}
		GenImage3(&Hobj,"byte",wid,hgt,(Hlong)dataR,(Hlong)dataG,(Hlong)dataB);
		delete []dataR;
		delete []dataG;
		delete []dataB;
		dataR=NULL;
		dataG=NULL;
		dataB=NULL;
	}
	//	CV_8UCU1
	else if(image.type() == CV_8UC1)
	{
		uchar* data=new uchar[hgt*wid];
		for(i=0;i<hgt;i++)
			memcpy(data+wid*i,image.data+image.step*i,wid);
		GenImage1(&Hobj,"byte",wid,hgt,(Hlong)data);
		delete[] data;
		data=NULL;
	}
	return Hobj;
}

Mat HObject2Mat(HObject Hobj)
{
	HTuple htCh=HTuple();
	HTuple cType;
	Mat Image;
	ConvertImageType(Hobj,&Hobj,"byte");
	CountChannels(Hobj,&htCh);
	HTuple wid;
	HTuple hgt;
	int W,H;
	if(htCh[0].I()==1)
	{
		HTuple ptr;
		GetImagePointer1(Hobj,&ptr,&cType,&wid,&hgt);
		W=(Hlong)wid;
		H=(Hlong)hgt;
		Image.create(H,W,CV_8UC1);
		uchar* pdata=(uchar*)ptr[0].I();
		memcpy(Image.data,pdata,W*H);
	}
	else if(htCh[0].I()==3)
	{
		HTuple ptrR, ptrG, ptrB;
		GetImagePointer3(Hobj,&ptrR,&ptrG,&ptrB,&cType,&wid,&hgt);
		W=(Hlong)wid;
		H=(Hlong)hgt;
		Image.create(H,W,CV_8UC3);
		vector<Mat> vecM(3);
		vecM[2].create(H,W,CV_8UC1);
		vecM[1].create(H,W,CV_8UC1);
		vecM[0].create(H,W,CV_8UC1);
		uchar* pr=(uchar*)ptrR[0].I();
		uchar* pg=(uchar*)ptrG[0].I();
		uchar* pb=(uchar*)ptrB[0].I();
		memcpy(vecM[2].data,pr,W*H);
		memcpy(vecM[1].data,pg,W*H);
		memcpy(vecM[0].data,pb,W*H);
		merge(vecM,Image);
	}
	return Image;
}

 

 

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值