视觉工具与C#联合开发图片格式转换
·
在视觉工具与C#联合开发中,图片格式转换是一个常见的需求。以下是整合的知识总结:
一、借助同种格式去转换
在C#中与VisionPro、Halcon和OpenEvision联合编程时,通常需要将采集到的图片转换为.NET框架中的System.Drawing.Bitmap类或者类似的图像处理库中的图像对象,以便于进一步处理或显示。
VisionPro 转 Bitmap
private Bitmap ICogImageToBitmap(ICogImage image)
{
if (image == null) throw new ArgumentNullException("image");
int width = image.Width;
int height = image.Height;
Bitmap bitmap = new Bitmap(width, height);
image.CopyTo(bitmap);
return bitmap;
}
Halcon 转 Bitmap
private Bitmap HImageToBitmap(HImage hImage)
{
if (hImage == null) throw new ArgumentNullException("hImage");
HTuple width, height;
hImage.GetImageSize(out width, out height);
Bitmap bitmap = new Bitmap(width.ToInt32(), height.ToInt32());
hImage.ToBitmap(bitmap);
return bitmap;
}
OpenEvision 转 Bitmap
public static byte[] ImageToBuffer(Image Image, System.Drawing.Imaging.ImageFormat imageFormat)
{
if (Image == null) return null;
byte[] data = null;
using (MemoryStream oMemoryStream = new MemoryStream())
{
using (Bitmap oBitmap = new Bitmap(Image))
{
oBitmap.Save(oMemoryStream, imageFormat);
oMemoryStream.Position = 0;
data = new byte[oMemoryStream.Length];
oMemoryStream.Read(data, 0, Convert.ToInt32(oMemoryStream.Length));
oMemoryStream.Flush();
}
}
return data;
}
public static Image BufferToImage(byte[] Buffer)
{
if (Buffer == null || Buffer.Length == 0) return null;
byte[] data = (byte[])Buffer.Clone();
Image oImage = null;
Bitmap oBitmap = null;
try
{
MemoryStream oMemoryStream = new MemoryStream(Buffer);
oMemoryStream.Position = 0;
oImage = System.Drawing.Image.FromStream(oMemoryStream);
oBitmap = new Bitmap(oImage);
}
catch
{
throw;
}
return oBitmap;
}
二、直接互转(以VP和Ha之间互转为例)
灰度图 Halcon 转 VisionPro
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);
CogImage8Root tmpCogImage8Root = new CogImage8Root();
tmpCogImage8Root.Initialize(width, height, (IntPtr)pointer, width, null);
CogImage8Grey outVproImage = new CogImage8Grey();
outVproImage.SetRoot(tmpCogImage8Root);
return outVproImage;
}
灰度图 VisionPro 转 Halcon
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;
if (ptr.Stride == outVproImage.Width)
{
HalconDotNet.HOperatorSet.GenImage1(out ho_Image2, "byte", outVproImage.Width, outVproImage.Height, pointer);
}
else
{
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] = bptr[i * ptr.Stride + j];
}
}
}
IntPtr iptr = bmpData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(data, 0, iptr, outVproImage.Width * outVproImage.Height);
HOperatorSet.GenImage1(out ho_Image2, "byte", outVproImage.Width, outVproImage.Height, bmpData.Scan0);
}
return ho_Image2;
}
RGB 图 VisionPro 转 Halcon
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")
{
CogImage24PlanarColor RGBImage = (CogImage24PlanarColor)VproImage;
ICogImage8PixelMemory Rptr, Gptr, Bptr;
RGBImage.Get24PlanarColorPixelMemory(CogImageDataModeConstants.Read, 0, 0, width, height, out Rptr, out Gptr, out Bptr);
if (Gptr.Stride == Gptr.Width)
{
HOperatorSet.GenImage3(out hImage, "byte", width, height, Rptr.Scan0, Gptr.Scan0, Bptr.Scan0);
}
else
{
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);
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];
}
}
}
HOperatorSet.GenImage3(out hImage, "byte", width, height, bmpDataR.Scan0, bmpDataG.Scan0, bmpDataB.Scan0);
}
return hImage;
}
else
{
return null;
}
}
RGB 图 Halcon 转 VisionPro
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)
{
HOperatorSet.GetImagePointer3(ho_Image, out hred, out hgreen, out hblue, out type, out width, out height);
CogImage24PlanarColor RGBImage = new CogImage24PlanarColor();
ICogImage8Root rImg, gImg, bImg;
RGBImage.GetRoots(out rImg, out gImg, out bImg);
CogImage8Root Rimg = new CogImage8Root();
CogImage8Root Gimg = new CogImage8Root();
CogImage8Root Bimg = new CogImage8Root();
Rimg.Initialize(width, height, (IntPtr)hred, width, null);
Gimg.Initialize(width, height, (IntPtr)hgreen, width, null);
Bimg.Initialize(width, height, (IntPtr)hblue, width, null);
rImg = Rimg; gImg = Gimg; bImg = Bimg;
RGBImage.SetRoots(rImg, gImg, bImg);
return RGBImage;
}
else
{
return null;
}
}
这些代码示例展示了如何在不同的视觉工具之间进行图像格式的转换,以便在C#环境中进行进一步的处理或显示。
更多推荐
所有评论(0)