/*************灰度图 Halcon图像类型转VisionPro***********/
/// <summary>
/// Halcon图像类型转VisionPro图像类型(灰度图)
/// </summary>
/// <param name="ho_Image">输入Halcon图像类型(HImage)</param>
/// <returns></returns>
public ICogImage Gray_Halocn_to_VisionPro(HObject ho_Image)
{
HTuple type, width, height, pointer;
HalconDotNet.HOperatorSet.GetImagePointer1(ho_Image, out pointer, out type, out width, out height); //拿Halcon图像的指针
CogImage8Root tmpCogImage8Root = new CogImage8Root();//创建vp灰度图
tmpCogImage8Root.Initialize(width, height, (IntPtr)pointer, width, null);//初始化vp灰度图
CogImage8Grey outVproImage = new CogImage8Grey();
outVproImage.SetRoot(tmpCogImage8Root);
ICogImage VproImage = outVproImage;
return VproImage;
}
/*************灰度图 VisionPro图像类型转Halcon***********/
/// <summary>
/// VisionPro图像类型转Halcon图像类型(灰度图)
/// </summary>
/// <param name="VproImage">输入VisionPro图像类型(ICogImage)</param>
/// <returns></returns>
public HObject Gray_VisionPro_to_Halocn(ICogImage VproImage)
{
HObject ho_Image2;
IntPtr pointer;
CogImage8Grey outVproImage = CogImageConvert.GetIntensityImage(VproImage, 0, 0, VproImage.Width, VproImage.Height);
ICogImage8PixelMemory ptr = outVproImage.Get8GreyPixelMemory(CogImageDataModeConstants.Read, 0, 0, outVproImage.Width, outVproImage.Height);
pointer = ptr.Scan0; //拿VP图像的指针
if (ptr.Stride== outVproImage.Width)
HalconDotNet.HOperatorSet.GenImage1(out ho_Image2, "byte", outVproImage.Width, outVproImage.Height, pointer); //创建H图像
else//若图像列数不被4整除
{
Bitmap bmp = new Bitmap(outVproImage.Width, outVproImage.Height, PixelFormat.Format8bppIndexed);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, outVproImage.Width, outVproImage.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); //获取图像参数
int bytes = outVproImage.Width * outVproImage.Height;
byte[] data = new byte[bytes];
//手动提取像素信息
unsafe
{
byte* bptr = (byte*)pointer;
for (int i = 0; i < outVproImage.Height; i++)
{
for (int j = 0; j < outVproImage.Width; j++)
{
//data[i * outVproImage.Width + j] = outVproImage.GetPixel(j, i);
data[i * outVproImage.Width + j] = bptr[i * ptr.Stride + j];
}
}
}
IntPtr iptr = bmpData.Scan0; // 获取bmpData的内存起始位置
System.Runtime.InteropServices.Marshal.Copy(data, 0, iptr, outVproImage.Width * outVproImage.Height);
HOperatorSet.GenImage1(out ho_Image2, "byte", outVproImage.Width, outVproImage.Height, bmpData.Scan0);//内存拷贝到halcon图像
}
return ho_Image2;
}
*************RGB_VisionPro图像类型转Halcon***********/
/// <summary>
/// VisionPro图像类型转Halcon图像类型(RGB彩图)
/// </summary>
/// <param name="VproImage">输入VisionPro图像类型(ICogImage)</param>
/// <returns></returns>
public HObject RGB_VisionPro_to_Halocn(ICogImage VproImage)
{
//获取图像信息
int width = VproImage.Width, height = VproImage.Height;
HalconDotNet.HObject hImage = new HalconDotNet.HObject();
if (VproImage.GetType().Name == "CogImage24PlanarColor")
{
//用RGB格式变量接收图像
CogImage24PlanarColor RGBImage = (CogImage24PlanarColor)VproImage;
//获取R G B 三通道像素指针信息
ICogImage8PixelMemory Rptr, Gptr, Bptr;
RGBImage.Get24PlanarColorPixelMemory(CogImageDataModeConstants.Read, 0, 0, width, height, out Rptr, out Gptr, out Bptr);
//创建Halcon图像变量
if (Gptr.Stride == Gptr.Width)
{
//halcon合成彩色图像
HOperatorSet.GenImage3(out hImage, "byte", width, height, Rptr.Scan0, Gptr.Scan0, Bptr.Scan0);
}
else//图像不被4整除(直接合成图像会乱)
{
//创建3个bitmap格式变量,接收R G B 三通道信息
Bitmap bmpR = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bmpDataR = bmpR.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); //获取图像参数
Bitmap bmpG = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bmpDataG = bmpG.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); //获取图像参数
Bitmap bmpB = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bmpDataB = bmpB.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); //获取图像参数
//接收R G B 三通道信息
unsafe
{
byte* bptrR = (byte*)bmpDataR.Scan0;
byte* bptrG = (byte*)bmpDataG.Scan0;
byte* bptrB = (byte*)bmpDataB.Scan0;
byte* bptrRP = (byte*)Rptr.Scan0;
byte* bptrGP = (byte*)Gptr.Scan0;
byte* bptrBP = (byte*)Bptr.Scan0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
bptrR[i * bmpDataR.Width + j] = bptrRP[i * Rptr.Stride + j];
bptrG[i * bmpDataR.Width + j] = bptrGP[i * Rptr.Stride + j];
bptrB[i * bmpDataR.Width + j] = bptrBP[i * Rptr.Stride + j];
}
}
}
//利用3个bitmap 合成彩图
HOperatorSet.GenImage3(out hImage, "byte", width, height, bmpDataR.Scan0, bmpDataG.Scan0, bmpDataB.Scan0);
}
return hImage;
}
else
return null;
}
/*************RGB_Halcon图像类型转VisionPro***********/
/// <summary>
/// Halcon图像类型转VisionPro图像类型(RGB彩图)
/// </summary>
/// <param name="ho_Image">输入Halcon图像类型(HImage)</param>
/// <returns></returns>
public ICogImage RGB_Halocn_to_VisionPro(HObject ho_Image)
{
HTuple hred, hgreen, hblue, type, width, height, channels;
HalconDotNet.HOperatorSet.CountChannels(ho_Image, out channels);
if (channels == 3)
{
//获取halcon图像rgb像素指针
HOperatorSet.GetImagePointer3(ho_Image, out hred, out hgreen, out hblue, out type, out width, out height);
//创建visionpro 彩色图像变量
CogImage24PlanarColor RGBImage = new CogImage24PlanarColor();
//获取新建彩色图像三通道
ICogImage8Root rImg, gImg, bImg;
RGBImage.GetRoots(out rImg, out gImg, out bImg);
//创建3个灰度图存储R G B三通道信息
CogImage8Root Rimg = new CogImage8Root();//创建vp灰度图
CogImage8Root Gimg = new CogImage8Root();//创建vp灰度图
CogImage8Root Bimg = new CogImage8Root();//创建vp灰度图
Rimg.Initialize(width, height, (IntPtr)hred, width, null);//初始化vp灰度图
Gimg.Initialize(width, height, (IntPtr)hgreen, width, null);//初始化vp灰度图
Bimg.Initialize(width, height, (IntPtr)hblue, width, null);//初始化vp灰度图
//将R G B三通道信息复制到新建彩色图像的三通道
rImg = Rimg; gImg = Gimg; bImg = Bimg;
RGBImage.SetRoots(rImg, gImg, bImg);
//visionpro图像类型
ICogImage VproImage = RGBImage;
return VproImage;
}
else
return null;
}
Halcon和VisionPro图像类型转换
最新推荐文章于 2024-07-13 10:00:53 发布