用DirectShow调用摄像头采集图像并转化为halcon图像

//转Halcon图像
HObject ImageDataPtr2HObject(CCameraDS& CameraDS, BYTE* imageDataPtr)
{
	HObject image;
	if (8 == CameraDS.GetBitCount()) //灰度图
	{
		//造一个灰度图
		GenImage1(
			&image,
			"byte",
			CameraDS.GetWidth(),
			CameraDS.GetHeight(),
			Hlong(imageDataPtr)
		);
	}
	else
	{
		BYTE* RChannel = NULL, * GChannel = NULL, * BChannel = NULL;
		int Width, Height;
		Width = CameraDS.GetWidth();
		Height = CameraDS.GetHeight();
		RChannel = new BYTE[Width * Height];
		GChannel = new BYTE[Width * Height];
		BChannel = new BYTE[Width * Height];
		//4字节对齐
		int LineByte = (Width * CameraDS.GetBitCount() + 31) / 32 * 4;
		for (int i = 0; i < Height; i++) //图像的高---行
		{
			for (int j = 0; j < Width; j++) //图像的宽---列
			{

				//图像效果上下左右镜像  顺序读取时
				/*memcpy(BChannel + Width * i + j, imageDataPtr + LineByte * i + 3 * j + 0, 1);
				memcpy(GChannel + Width * i + j, imageDataPtr + LineByte * i + 3 * j + 1, 1);
				memcpy(RChannel + Width * i + j, imageDataPtr + LineByte * i + 3 * j + 2, 1);*/

				//使上下翻转
				memcpy(BChannel + Width * (Height - 1 - i) + j, imageDataPtr + LineByte * i + 3 * j + 0, 1);
				memcpy(GChannel + Width * (Height - 1 - i) + j, imageDataPtr + LineByte * i + 3 * j + 1, 1);
				memcpy(RChannel + Width * (Height - 1 - i) + j, imageDataPtr + LineByte * i + 3 * j + 2, 1);

				//使左右翻转
				/*memcpy(BChannel + Width * i + Width - 1 - j , imageDataPtr + LineByte * i + 3 * j + 0, 1);
				memcpy(GChannel + Width * i + Width - 1 - j, imageDataPtr + LineByte * i + 3 * j + 1, 1);
				memcpy(RChannel + Width * i + Width - 1 - j, imageDataPtr + LineByte * i + 3 * j + 2, 1);*/

				//使上下左右翻转
				/*memcpy(BChannel + Width * (Height - 1 - i) + Width - 1 - j, imageDataPtr + LineByte * i + 3 * j + 0, 1);
				memcpy(GChannel + Width * (Height - 1 - i) + Width - 1 - j, imageDataPtr + LineByte * i + 3 * j + 1, 1);
				memcpy(RChannel + Width * (Height - 1 - i) + Width - 1 - j, imageDataPtr + LineByte * i + 3 * j + 2, 1);*/

				//参考
				/*memcpy(BChannel + Width * i + Width - j - 1, imageDataPtr + LineByte * (Height - 1 - i) + 3 * (Width - 1 - j) + 0, 1);
				memcpy(GChannel + Width * i + Width - j - 1, imageDataPtr + LineByte * (Height - 1 - i) + 3 * (Width - 1 - j) + 1, 1);
				memcpy(RChannel + Width * i + Width - j - 1, imageDataPtr + LineByte * (Height - 1 - i) + 3 * (Width - 1 - j) + 2, 1);*/
			}

		}
		//创建3通道图像
		GenImage3(
			&image,
			"byte",
			Width,
			Height,
			Hlong(RChannel),
			Hlong(GChannel),
			Hlong(BChannel)
		);
		delete[] RChannel;
		delete[] GChannel;
		delete[] BChannel;
	}
	return image;	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值