OpenCV的Mat和Halcon的HObject类型互相转换

项目之前一直使用的OpenCV,最近有个小问题使用Halcon有更好的效果,于是有了cv::Mat和Halcon中HObject互相转换的需求。

参考了博客http://blog.csdn.net/u010627377/article/details/76687178,我使用他的代码的时候出错了,于是在其基础上稍微做了下修改。Halcon的版本是13,OpenCV的版本是3.2。如有问题,欢迎讨论。

HObject Mat2HObject(const cv::Mat &image)
{
	HObject Hobj = HObject();
	int hgt = image.rows;
	int wid = image.cols;
	int i;
	//  CV_8UC3  
	if (image.type() == CV_8UC3)
	{
		vector imgchannel;
		split(image, imgchannel);
		cv::Mat imgB = imgchannel[0];
		cv::Mat imgG = imgchannel[1];
		cv::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(ptr);
		memcpy(Image.data, pdata, W*H);
	}
	else if (htCh[0].I() == 3)
	{
		void *Rptr;
		void *Gptr;
		void *Bptr;
		HImage hImg(Hobj);
		hImg.GetImagePointer3(&Rptr, &Gptr, &Bptr, &cType, &wid, &hgt);
		int W = wid;
		int H = hgt;
		Image.create(H, W, CV_8UC3);
		vector VecM(3);
		VecM[0].create(H, W, CV_8UC1);
		VecM[1].create(H, W, CV_8UC1);
		VecM[2].create(H, W, CV_8UC1);
		unsigned char *R = (unsigned char *)Rptr;
		unsigned char *G = (unsigned char *)Gptr;
		unsigned char *B = (unsigned char *)Bptr;
		memcpy(VecM[2].data, R, W*H);
		memcpy(VecM[1].data, G, W*H);
		memcpy(VecM[0].data, B, W*H);
		cv::merge(VecM, Image);
	}
	return Image;
}



你可能感兴趣的:(图像处理,c和c++的学习)