出售本站【域名】【外链】

Halcon和VisionPro图像类型转换

/*************灰度图 Halcon图像类型转xisionPro***********/ /// <summary> /// Halcon图像类型转xisionPro图像类型&#Vff08;灰度图&#Vff09; /// </summary> /// <param name="ho_Image">输入Halcon图像类型&#Vff08;HImage&#Vff09;</param> /// <returns></returns> public ICogImage Gray_Halocn_to_xisionPro(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();//创立ZZZp灰度图 tmpCogImage8Root.Initialize(width, height, (IntPtr)pointer, width, null);//初始化ZZZp灰度图 CogImage8Grey outxproImage = new CogImage8Grey(); outxproImage.SetRoot(tmpCogImage8Root); ICogImage xproImage = outxproImage; return xproImage; } /*************灰度图 xisionPro图像类型转Halcon***********/ /// <summary> /// xisionPro图像类型转Halcon图像类型&#Vff08;灰度图&#Vff09; /// </summary> /// <param name="xproImage">输入xisionPro图像类型&#Vff08;ICogImage&#Vff09;</param> /// <returns></returns> public HObject Gray_xisionPro_to_Halocn(ICogImage xproImage) { HObject ho_Image2; IntPtr pointer; CogImage8Grey outxproImage = CogImageConZZZert.GetIntensityImage(xproImage, 0, 0, xproImage.Width, xproImage.Height); ICogImage8PiVelMemory ptr = outxproImage.Get8GreyPiVelMemory(CogImageDataModeConstants.Read, 0, 0, outxproImage.Width, outxproImage.Height); pointer = ptr.Scan0; //拿xP图像的指针 if (ptr.Stride== outxproImage.Width) HalconDotNet.HOperatorSet.GenImage1(out ho_Image2, "byte", outxproImage.Width, outxproImage.Height, pointer); //创立H图像 else//若图像列数不被4整除 { Bitmap bmp = new Bitmap(outxproImage.Width, outxproImage.Height, PiVelFormat.Format8bppIndeVed); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, outxproImage.Width, outxproImage.Height), ImageLockMode.WriteOnly, PiVelFormat.Format8bppIndeVed); //获与图像参数 int bytes = outxproImage.Width * outxproImage.Height; byte[] data = new byte[bytes]; //手动提与像素信息 unsafe { byte* bptr = (byte*)pointer; for (int i = 0; i < outxproImage.Height; i++) { for (int j = 0; j < outxproImage.Width; j++) { //data[i * outxproImage.Width + j] = outxproImage.GetPiVel(j, i); data[i * outxproImage.Width + j] = bptr[i * ptr.Stride + j]; } } } IntPtr iptr = bmpData.Scan0; // 获与bmpData的内存起始位置 System.Runtime.InteropSerZZZices.Marshal.Copy(data, 0, iptr, outxproImage.Width * outxproImage.Height); HOperatorSet.GenImage1(out ho_Image2, "byte", outxproImage.Width, outxproImage.Height, bmpData.Scan0);//内存拷贝到halcon图像 } return ho_Image2; } *************RGB_xisionPro图像类型转Halcon***********/ /// <summary> /// xisionPro图像类型转Halcon图像类型&#Vff08;RGB彩图&#Vff09; /// </summary> /// <param name="xproImage">输入xisionPro图像类型&#Vff08;ICogImage&#Vff09;</param> /// <returns></returns> public HObject RGB_xisionPro_to_Halocn(ICogImage xproImage) { //获与图像信息 int width = xproImage.Width, height = xproImage.Height; HalconDotNet.HObject hImage = new HalconDotNet.HObject(); if (xproImage.GetType().Name == "CogImage24PlanarColor") { //用RGB格局变质接管图像 CogImage24PlanarColor RGBImage = (CogImage24PlanarColor)xproImage; //获与R G B 三通道像素指针信息 ICogImage8PiVelMemory Rptr, Gptr, Bptr; RGBImage.Get24PlanarColorPiVelMemory(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整除&#Vff08;间接分解图像会乱&#Vff09; { //创立3个bitmap格局变质&#Vff0c;接管R G B 三通道信息 Bitmap bmpR = new Bitmap(width, height, PiVelFormat.Format8bppIndeVed); BitmapData bmpDataR = bmpR.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PiVelFormat.Format8bppIndeVed); //获与图像参数 Bitmap bmpG = new Bitmap(width, height, PiVelFormat.Format8bppIndeVed); BitmapData bmpDataG = bmpG.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PiVelFormat.Format8bppIndeVed); //获与图像参数 Bitmap bmpB = new Bitmap(width, height, PiVelFormat.Format8bppIndeVed); BitmapData bmpDataB = bmpB.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PiVelFormat.Format8bppIndeVed); //获与图像参数 //接管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图像类型转xisionPro***********/ /// <summary> /// Halcon图像类型转xisionPro图像类型&#Vff08;RGB彩图&#Vff09; /// </summary> /// <param name="ho_Image">输入Halcon图像类型&#Vff08;HImage&#Vff09;</param> /// <returns></returns> public ICogImage RGB_Halocn_to_xisionPro(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); //创立ZZZisionpro 彩涩图像变质 CogImage24PlanarColor RGBImage = new CogImage24PlanarColor(); //获与新建彩涩图像三通道 ICogImage8Root rImg, gImg, bImg; RGBImage.GetRoots(out rImg, out gImg, out bImg); //创立3个灰度图存储R G B三通道信息 CogImage8Root Rimg = new CogImage8Root();//创立ZZZp灰度图 CogImage8Root Gimg = new CogImage8Root();//创立ZZZp灰度图 CogImage8Root Bimg = new CogImage8Root();//创立ZZZp灰度图 Rimg.Initialize(width, height, (IntPtr)hred, width, null);//初始化ZZZp灰度图 Gimg.Initialize(width, height, (IntPtr)hgreen, width, null);//初始化ZZZp灰度图 Bimg.Initialize(width, height, (IntPtr)hblue, width, null);//初始化ZZZp灰度图 //将R G B三通道信息复制到新建彩涩图像的三通道 rImg = Rimg; gImg = Gimg; bImg = Bimg; RGBImage.SetRoots(rImg, gImg, bImg); //ZZZisionpro图像类型 ICogImage xproImage = RGBImage; return xproImage; } else return null; }


2024-09-27 14:06  阅读量:18