Baumer工业相机堡盟工业相机如何通过BGAPISDK转换和显示偏振相机的图像(C#)

Baumer工业相机

Baumer工业相机堡盟相机是一种高性能、高质量的工业相机,可用于各种应用场景,如物体检测、计数和识别、运动分析和图像处理。

Baumer的万兆网相机拥有出色的图像处理性能,可以实时传输高分辨率图像。此外,该相机还具有快速数据传输、低功耗、易于集成以及高度可扩展性等特点。

Baumer工业相机的BGAPI SDK给新型偏振相机提供了测量所获图像的强度和偏振的能力。因此,它能够在应用程序中利用偏振信息。本应用说明描述了如何获得偏振相机的图像数据并进行转换和显示的功能。

Baumer工业相机的偏振相机的技术背景

Baumer工业相机的BGAPI SDK可以提供相机的图像原始数据,Halcon具有极为巨大的图像处理库,在图像处理领域非常强大,功能丰富,使用于工业视觉检测。

工业相机的SDK(Software Development Kit)是为了方便开发人员对工业相机进行控制和图像采集而提供的一套软件工具。而Halcon是一款强大的机器视觉软件,能够进行图像处理、分析、识别等多种任务。

Baumer工业相机中的偏振相机是基于索尼IMC250MZR传感器的。该传感器涂有一层金属网,可以过滤4个相邻像素的偏振信息。偏振角度被过滤以0°、45°、90°、135°的方式排列。
有了这些信息,就可以计算出以下数据:
偏振角(AOP)。
直线极化度(DOLP)
角度和直角极化度(ADOLP)
图像的强度。

Baumer工业相机通过BGAPI SDK在回调函数里显示偏振相机的图像

Baumer工业相机集成偏振功能,下面介绍在C#里Baumer工业相机显示偏振相机的图像的方式

Baumer工业相机在BufferEvent转换并显示偏振相机图像

Baumer工业相机BGAPI SDK的图像处理库中提供了可以将偏振图像格式转换为合适适用格式的图像算法。

在回调函数里转换并显示偏振格式图像,C#调用代码如下所示:

static void mDataStream_NewBufferEvent(object sender, BGAPI2.Events.NewBufferEventArgs mDSEvent)
{
    
    
            
    BGAPI2.Image imagePolarized = null;
    BGAPI2.ImageProcessor pImgProcessor = null;

    try
    {
    
    
        BGAPI2.Buffer mBufferFilled = null;
        //System.Drawing.Bitmap pBitmap = null;
        //Byte[] pImgBits;
        mBufferFilled = mDSEvent.BufferObj;
        if (mBufferFilled == null)
        {
    
    
            MessageBox.Show("Error: Buffer Timeout after 1000 ms!");
        }
        else if (mBufferFilled.IsIncomplete == true)
        {
    
    
            //MessageBox.Show("Error: Image is incomplete!");
            // queue buffer again
            mBufferFilled.QueueBuffer();

        }
        else
        {
    
    
            // get information about the image from the buffer object
            uint width = (uint)(mBufferFilled.Width);
            uint height = (uint)(mBufferFilled.Height);
            IntPtr pBufferData = mBufferFilled.MemPtr;
            ulong bufferDataSize = mBufferFilled.MemSize;
            ulong imageOffset = mBufferFilled.ImageOffset;
            ulong imageDataSize = (bufferDataSize > imageOffset) ? (bufferDataSize - imageOffset) : 0;
            IntPtr pImageData = (IntPtr)((ulong)(pBufferData) + imageOffset);

            string sComponent = "ADOLP";
            string sPixelFormatRaw = "";
            sPixelFormatRaw = "BaumerPolarized8";

            if (imagePolarized == null)
            {
    
    

                imagePolarized =actform.pImgProcessor.CreateImage(width, height, sPixelFormatRaw, pImageData, imageDataSize);
                        

                // Enable the component to be calculated, disable all others
                       
                EnableSingleComponent(imagePolarized, sComponent);
            }
            else
            {
    
    
                imagePolarized.Init(width, height, sPixelFormatRaw, pImageData, imageDataSize);
                /* As the pixel format is the same for all images captured, the enabled components and the active component selector
                    are preserved, so you don't need to enable components on every image. */
            }

            // Calculate the required Polarisation format using a direct transformation from raw polarized image.

            BGAPI2.Image component = actform.pImgProcessor.CreateTransformedImage(imagePolarized
                , (sComponent != "ADOLP") ? "Mono8" : "RGB8");

            System.Console.Write("  component image: {0}\r\n", component.NodeList["ComponentSelector"].Value);

                   
            IntPtr imagebuffer = new IntPtr();

            BGAPI2.Image pImage = actform.pImgProcessor.CreateImage((uint)mBufferFilled.Width, (uint)mBufferFilled.Height, mBufferFilled.PixelFormat, mBufferFilled.MemPtr, mBufferFilled.MemSize);
            BGAPI2.Image pTranImage = null;
            pTranImage = actform.pImgProcessor.CreateTransformedImage(pImage, "BGR8");

            int w = 0;
            int h = 0;
            w = (int)pTranImage.Width;
            h = (int)pTranImage.Height;



            imagebuffer = component.Buffer;

            //Now you have the Imagebuffer and can do everything with it you want
            if (actform.bFirstFrame)
            {
    
    
                actform.pImgBits = new Byte[w * h * 3];
                //actform.pBitmap = new Bitmap(w,h,PixelFormat.Format8bppIndexed);
                //pBitmap = new System.Drawing.Bitmap(w,h,System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                // actform.pBitmap = new System.Drawing.Bitmap(w,h,System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                actform.pBitmap = new System.Drawing.Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                actform.prcSource.X = 0;
                actform.prcSource.Y = 0;
                actform.prcSource.Width = w;
                actform.prcSource.Height = h;
                actform.bFirstFrame = false;
            }
            System.Drawing.Imaging.BitmapData bmpdata;
 
            bmpdata = actform.pBitmap.LockBits(actform.prcSource, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            System.Runtime.InteropServices.Marshal.Copy(imagebuffer, actform.pImgBits, 0, w * h * 3);
            System.Runtime.InteropServices.Marshal.Copy(actform.pImgBits, 0, bmpdata.Scan0, w * h * 3);
            actform.pBitmap.UnlockBits(bmpdata);

            System.Drawing.Graphics graph = System.Drawing.Graphics.FromHwnd(actform.pictureBoxA.Handle);
            graph.DrawImage(actform.pBitmap, actform.prcPBox, actform.prcSource, GraphicsUnit.Pixel);

            if (actform.bSaveImg)
            {
    
    
                String strPath;
                String strtime;
                strtime = DateTime.Now.ToString("yyyyMMddhhmmss");
                strPath = actform.pImgFileDir + "\\" + strtime + sComponent + ".bmp";
                actform.pBitmap.Save(strPath,System.Drawing.Imaging.ImageFormat.Png);
                        
            }

            mBufferFilled.QueueBuffer();
        }
    }
    catch (BGAPI2.Exceptions.IException ex)
    {
    
    
        {
    
    
            string str2;
			str2 = string.Format("ExceptionType:{0}! ErrorDescription:{1} in function:{2}",ex.GetType(),ex.GetErrorDescription(),ex.GetFunctionName());
			MessageBox.Show(str2);
        }
    }
    return;
}

Baumer工业相机BGAPI SDK转换和显示偏振图像的具体步骤

C#调用核心代码如下所示:

if (imagePolarized == null)
{
    
    

    imagePolarized =actform.pImgProcessor.CreateImage(width, height, sPixelFormatRaw, pImageData, imageDataSize);                        
    // Enable the component to be calculated, disable all others                       
    EnableSingleComponent(imagePolarized, sComponent);
}
 else
{
    
    
    imagePolarized.Init(width, height, sPixelFormatRaw, pImageData, imageDataSize);
     /* As the pixel format is the same for all images captured, the enabled components and the active component selector are preserved, so you don't need to enable components on every image. */
}
 
// Calculate the required Polarisation format using a direct transformation from raw polarized image.

BGAPI2.Image component = actform.pImgProcessor.CreateTransformedImage(imagePolarized
                , (sComponent != "ADOLP") ? "Mono8" : "RGB8");
System.Console.Write("  component image: {0}\r\n", component.NodeList["ComponentSelector"].Value);                 
IntPtr imagebuffer = new IntPtr();

BGAPI2.Image pImage = actform.pImgProcessor.CreateImage((uint)mBufferFilled.Width, (uint)mBufferFilled.Height, mBufferFilled.PixelFormat, mBufferFilled.MemPtr, mBufferFilled.MemSize);
BGAPI2.Image pTranImage = null;
pTranImage = actform.pImgProcessor.CreateTransformedImage(pImage, "BGR8");

Baumer工业相机使用偏振相机数据的原因

偏振相机使用偏振滤波器来分离光线的偏振状态,这样可以提高图像的对比度,使得细微的表面形态、反射和透射率等信息被准确地记录下来。这些信息对于工业品质控制、表面形貌评价、材料研究等方面都非常重要。

相比较传统的工业相机,偏振相机还可以减少光线干扰,提高图像细节,增强测量精度,更好地适应复杂的工业环境。

因此,工业相机使用偏振相机的数据可以更加准确地确定产品质量,提高生产效率和效益。

Baumer工业相机使用偏振相机的优势

  1. 抑制光逆反射:工业现场中通常会出现光源反射的问题,会干扰图像的质量。偏振相机能够让只有某些角度的光被捕获,而反射产生的光线则被过滤掉。

  2. 改善材料的表面检测:很多工业生产中需要对材料、产品的表面缺陷进行检测,例如玻璃、塑料等。偏振相机可以识别和区分这些材料表面的缺陷,并提高检测精度和检测速度。

  3. 改善物体外形检测:产品在生产过程中可能会出现形变,或者形状不规则,这些因素都会影响使用普通相机拍摄的效果。而偏振相机采用偏振光线,可以减少这些干扰,提高检测结果的准确度。

  4. 增强图像的对比度:偏振相机检测的图像能够显示出物体表面的细节和特征,而不仅仅是颜色和亮度等基本属性,从而提高图像的对比度和分辨率,使得检测结果更准确、更可靠。

Baumer工业相机使用偏振相机的行业应用

工业相机,特别是在机器视觉应用中,偏振相机被用来分析解决许多问题,包括表面缺陷检测、玻璃或透明塑料裂纹检测等。由于偏振相机可以区分材料表面的不同反射率,因此可以减少表面反射和减少模糊图像的存在。

在制造和工业应用中,偏振相机可以用于检测不同方向上的应力,这使得它们可以识别和追踪具有特定工艺标记的物体,从而改善制造和组装的精度。

此外,偏振相机还可用于食品和制药行业中的质量控制,以检测不同材料中的异物和杂质。

工业相机使用偏振相机的行业应用有很多。其中一些包括:

  1. 汽车制造:偏振相机可以用于检测汽车表面的缺陷和异物,如油脂和污垢。
  2. 塑料行业:偏振相机可以检测塑料制品的裂痕、气孔和异物。
  3. 食品行业:偏振相机可以用于检测食品中的异物,如骨头、塑料碎片等。
  4. 电子工业:偏振相机可以用于检测电子元件的质量,如半导体器件和电子电路板的质量。
  5. 纺织工业:偏振相机可以用于检测纺织品的质量,如纤维的方向、密度和强度。

总的来说,偏振相机在工业中的应用十分广泛,能够帮助生产厂家提高产品品质、提高生产效率和降低生产成本。

猜你喜欢

转载自blog.csdn.net/xianzuzhicai/article/details/130332791