Baumer industrial camera Baumer industrial camera how to combine BGAPI SDK and OpenCVSharp to merge and display images from 4 angles of polarization camera (C#)

Baumer industrial camera

Baumer Industrial Cameras Baumer cameras are high-performance, high-quality industrial cameras that can be used in various application scenarios, such as object detection, counting and recognition, motion analysis and image processing.

Baumer's 10 Gigabit cameras have excellent image processing performance and can transmit high-resolution images in real time. In addition, the camera features fast data transfer, low power consumption, easy integration, and high scalability. ​The BGAPI SDK for
Baumer
Therefore, it is able to utilize the polarization information in the application. This application note describes how to simultaneously obtain the image data from 4 angles of the polarization camera and perform conversion and display functions.


​For a detailed introduction and application of the polarization function of Baumer
polarization functions (Polarization camera advantages and industry applications) ( C++)

A technical blog on how to use ADOLP's polarized image data for display:

Baumer industrial camera Baumer industrial camera how to convert and display polarized camera image through BGAPISDK (C#)

How to use BGAPISDK to obtain images from four angles at the same time:
Baumer Industrial Camera Baumer Industrial Camera How to use BGAPI SDK to obtain image conversion and display of four angles of polarization camera (C#)

Technical background of Baumer industrial camera polarization camera

The polarization camera in the industrial camera is realized based on the principle of polarized light. Polarized light refers to the light whose vibration direction only oscillates in a specific plane during the propagation of light, and the opposite light is called natural light. Polarization cameras use polarizers or polarizers to control the vibration direction of light, so that only light with a specific polarization direction passes through the camera lens, thereby achieving specific screening or measurement functions.

Polarization cameras are widely used in industrial quality inspection, medical diagnosis, material analysis and other fields. For example, in industrial quality inspection, polarization cameras can detect information such as stress distribution and surface defects of materials to determine the quality of products; in medical diagnosis, polarization cameras can be used to detect material characteristics of biological samples such as tissue and chromatin ; In material analysis, polarization cameras can be used to detect defects such as distortions and cracks in materials.

In general, polarization cameras have important application value in the fields of industrial automation and scientific research, and can realize fast, accurate and efficient detection and analysis.
The polarization camera in Baumer Industrial Camera is based on the Sony IMC250MZR sensor. The sensor is coated with a metal mesh that filters polarization information from 4 adjacent pixels. The polarization angles are filtered and arranged in the manner of 0°, 45°, 90°, and 135°.

Here mainly describes how to combine BGAPI SDK and OpenCVSharp under the platform of C# to merge images from 4 angles of polarization camera and display the core code

Code case sharing

This article introduces the function of merging and displaying images from 4 angles of the polarization camera when using the BGAPI SDK to develop Baumer's industrial cameras

The following are the core code implementation steps:

1: Reference the appropriate class file

The core code in the C# environment is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using CSCameraDemo.Properties;
using System.Globalization;
using WindowsFormsApplication1;
using System.Threading.Tasks;
using System.Threading;
using System.Drawing.Imaging;

using BGAPI2;
using OpenCvSharp;
using OpenCvSharp.Dnn;

2: Combine BGAPI SDK and OpenCVSharp in the callback function to merge images from 4 angles of the polarization camera

The following is the core code for merging images from 4 angles of the polarization camera in combination with BGAPI SDK and OpenCVSharp after the camera is turned on in the C# environment and connected to the camera.

As follows:

SystemList 
Open a System 
Get the InterfaceList and fill it Open an Interface 
Get the DeviceList and fill it 
Open a Device

void mDataStream_NewBufferEvent(object sender, BGAPI2.Events.NewBufferEventArgs mDSEvent)
{
    
    
    try
    {
    
    
        BGAPI2.Image imagePolarized = null;
               
        BGAPI2.Buffer mBufferFilled = null;              
        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
        {
    
    
            #region//获取当前FrameID
            FrameIDInt = (int)mBufferFilled.FrameID;
            OnNotifySetFrameID(FrameIDInt.ToString());
            #endregion


            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 = "POL90";
            string sPixelFormatRaw = "BaumerPolarized8";

            SortedSet<string> sComponents = new SortedSet<string> {
    
     "POL0", "POL45", "POL90", "POL135" };
            if (imagePolarized == null)
            {
    
    
                imagePolarized = pImgProcessor.CreateImage(width, height, sPixelFormatRaw, pImageData, imageDataSize);
                SortedSet<string> sEnableComponents = new SortedSet<string>(sComponents);                      
                EnableComponents(imagePolarized, sEnableComponents);
            }
            else
            {
    
    
                imagePolarized.Init(width, height, sPixelFormatRaw, pImageData, imageDataSize);                        
            } 

            BGAPI2.Image multiPartImage = pImgProcessor.CreateTransformedImage(imagePolarized, "Mono8");
            BGAPI2.Node componentSelector = multiPartImage.NodeList["ComponentSelector"];
            BGAPI2.Node componentEnable = multiPartImage.NodeList["ComponentEnable"];
            BGAPI2.Node componentOffset = multiPartImage.NodeList["ComponentOffset"];
            BGAPI2.Node componentLength = multiPartImage.NodeList["ComponentLength"];
            IntPtr pImageBuffer = multiPartImage.Buffer;
            BGAPI2.Image component = null;

            System.Drawing.Bitmap bitmap1;
            System.Drawing.Bitmap bitmap2;
            System.Drawing.Bitmap bitmap3;
            System.Drawing.Bitmap bitmap4;

            OpenCvSharp.Mat Matgray1 = new Mat();
            OpenCvSharp.Mat Matgray2 = new Mat();
            OpenCvSharp.Mat Matgray3 = new Mat();
            OpenCvSharp.Mat Matgray4 = new Mat();

            int CountIndex = 0;
            foreach (string sComponentCur in sComponents)
            {
    
    
                componentSelector.Value = sComponentCur;
                if (componentEnable.Value == true)
                {
    
    
                    CountIndex = CountIndex + 1;
                    ulong partLength = (ulong)componentLength.Value;

                    if (partLength > 0)
                    {
    
    
                        // Part present - direct access to a single part
                        ulong partOffset = (ulong)componentOffset.Value;

                        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap((int)width, (int)height, (int)width,
                System.Drawing.Imaging.PixelFormat.Format8bppIndexed, (IntPtr)((ulong)(pImageBuffer) + partOffset));

                        #region//Mono图像数据转换。彩色图像数据转换于此不同
                        System.Drawing.Imaging.ColorPalette palette = bitmap.Palette;
                        int nColors = 256;
                        for (int ix = 0; ix < nColors; ix++)
                        {
    
    
                            uint Alpha = 0xFF;
                            uint Intensity = (uint)(ix * 0xFF / (nColors - 1));
                            palette.Entries[ix] = System.Drawing.Color.FromArgb((int)Alpha, (int)Intensity, (int)Intensity, (int)Intensity);
                        }
                        bitmap.Palette = palette;
                        #endregion

                        if (CountIndex == 1)
                        {
    
    
                            Matgray1 = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);//用bitmap转换为mat  
                        }
                        if (CountIndex == 2)
                        {
    
    
                            Matgray2 = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);//用bitmap转换为mat  
                        }
                        if (CountIndex == 3)
                        {
    
    
                            Matgray3 = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);//用bitmap转换为mat  
                        }
                        if (CountIndex == 4)
                        {
    
    
                            Matgray4 = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);//用bitmap转换为mat  
                        }
                        //string strtime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                        //string saveimagepath = sComponentCur + ".jpg";
                        //bitmap.Save(saveimagepath, System.Drawing.Imaging.ImageFormat.Bmp);
                    }


                           
                }
            }


                   

            long currenttime = (long)mBufferFilled.Timestamp;                   
            DateTime sdasd = GetTime(currenttime, true);

                   

            #region//对四张图像进行基础拼接

                   

            Mat panorama1 = new Mat();
            Mat panorama2 = new Mat();
            Mat panoramaResult = new Mat();
            Cv2.VConcat(Matgray1, Matgray2, panorama1);
            Cv2.VConcat(Matgray3, Matgray4, panorama2);
            Cv2.HConcat(panorama1, panorama2, panoramaResult);
            Bitmap bmp = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(panoramaResult);//用mat转换为bitmap              
            panorama1.Dispose(); panorama2.Dispose(); panoramaResult.Dispose();

            #endregion




            #region//回调函数保存图像功能
            if (bSaveImg)
            {
    
    


                if (!AutoSaveCheck.Checked & !ContinueSave.Checked)
                {
    
    
                    //使用bitmap自带函数保存
                    string strtime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                    string saveimagepath = pImgFileDir + "\\" + strtime + ".jpg";
                    bmp.Save(saveimagepath, System.Drawing.Imaging.ImageFormat.Bmp);

                    使用opencv进行保存图像
                    //if (mBufferFilled.PixelFormat == "Mono8")
                    //{
    
    
                    //    OpenCvSharp.Mat matgray = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);//用bitmap转换为mat                           
                    //    matgray.SaveImage("opencv_image.png");
                    //    Cv2.ImWrite("opencvcv_image_Clone.png", matgray);
                    //}
                    bSaveImg = false;//变量控制单次保存图像
                }


                if (AutoSaveCheck.Checked)
                {
    
    
                    //使用bitmap自带函数保存
                    string strtime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                    string saveimagepath = pImgFileDir + "\\" + strtime + ".jpg";
                    //bmp.Save(saveimagepath, System.Drawing.Imaging.ImageFormat.Jpeg);

                    Thread SaveImagesThread1 = new Thread((ThreadStart)delegate() {
    
     AutoSaveImageRun(bmp, saveimagepath); });
                    SaveImagesThread1.Start();
                    SaveImagesThread1.Join();

                    AutoSaveCount = AutoSaveCount + 1;
                    if (AutoSaveCount == AutoSaveNum0)
                    {
    
    
                        AutoSaveCount = 0;
                        bSaveImg = false;
                        AutoSaveCheck.Checked = false;
                    }
                }


                if (ContinueSave.Checked)
                {
    
    
                    //使用bitmap自带函数保存
                    string strtime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                    string saveimagepath = pImgFileDir + "\\" + strtime + ".jpg";
                    //bmp.Save(saveimagepath, System.Drawing.Imaging.ImageFormat.Jpeg);

                    Thread SaveImagesThread1 = new Thread((ThreadStart)delegate() {
    
     AutoSaveImageRun(bmp, saveimagepath); });
                    SaveImagesThread1.Start();
                    SaveImagesThread1.Join();
                }

                        
            }
            #endregion




            #region//bitmap的图像数据复制pBitmap
            Bitmap clonebitmap = (Bitmap)bmp.Clone();
            BitmapData data = clonebitmap.LockBits(new Rectangle(0, 0, clonebitmap.Width, clonebitmap.Height), ImageLockMode.ReadOnly, clonebitmap.PixelFormat);
            clonebitmap.UnlockBits(data);
            pBitmap = clonebitmap;
            #endregion

            #region//将pBitmap图像数据显示在UI界面PictureBox控件上
            prcSource.X = 0;prcSource.Y = 0;
            prcSource.Width = (int)clonebitmap.Width; prcSource.Height = (int)clonebitmap.Height;
            System.Drawing.Graphics graph = System.Drawing.Graphics.FromHwnd(pictureBoxA.Handle);
            graph.DrawImage(pBitmap, prcPBox, prcSource, GraphicsUnit.Pixel);
            #endregion

            clonebitmap.Dispose(); //清除临时变量clonebitmap所占内存空间
            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;
}



//------------------------------------------------------------------------------
/* Setup the Baumer GAPI to calculate the requested polarization component from the raw
    polarized image */
static ulong EnableComponents(BGAPI2.Image image, SortedSet<string> sComponents)
{
    
    
    BGAPI2.Node componentSelector = image.NodeList["ComponentSelector"];
    BGAPI2.Node componentEnable = image.NodeList["ComponentEnable"];
    BGAPI2.NodeMap components = componentSelector.EnumNodeList;
    ulong componentsAvailable = components.Count;

    ulong componentsEnabled = 0;
    for (ulong i = 0; i < componentsAvailable; i++)
    {
    
    
        componentSelector.Value = i;
        bool bState = componentEnable.Value;
        if (sComponents.Contains(componentSelector.Value) != true)
        {
    
    
            componentEnable.Value = false;
        }
        else
        {
    
    
            componentEnable.Value = true;
            componentsEnabled++;
        }
    }
    return componentsEnabled;
}

      

3: The function of OpenCVSharp to merge images from 4 angles of the polarization camera

The following is the core code for merging images from 4 angles of the polarized camera through the BGAPI SDK and OpenCVSharp after the camera is turned on in the C# environment and connected to the camera.

As follows:

#region//对四张图像进行基础拼接

Mat panorama1 = new Mat();
Mat panorama2 = new Mat();
Mat panoramaResult = new Mat();
Cv2.VConcat(Matgray1, Matgray2, panorama1);
Cv2.VConcat(Matgray3, Matgray4, panorama2);
Cv2.HConcat(panorama1, panorama2, panoramaResult);
Bitmap bmp = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(panoramaResult);//用mat转换为bitmap              
panorama1.Dispose(); panorama2.Dispose(); panoramaResult.Dispose();

#endregion

Results as shown below:
insert image description here

Advantages of Baumer industrial polarization cameras to acquire multi-angle polarization images

1. They use polarizing filters to capture light waves that vibrate in a single direction, reducing glare and reflections from shiny surfaces. This results in a clearer and more precise image, making it easier to identify defects or anomalies in highly reflective surfaces.

2. Polarized cameras also provide better contrast and color accuracy, allowing precise color measurement and analysis.

3. Polarizing cameras can be used in harsh environmental conditions and can capture images of objects that are difficult to see with ordinary cameras.

Industrial application of Baumer industrial polarization camera to acquire multi-angle polarization images

Polarized industrial cameras are commonly used in various industrial applications such as quality control, defect inspection, material analysis, and surface inspection.
They help eliminate glare and reflections, and improve image contrast and accuracy on a variety of materials, including glass, plastic, and metal.
Polarized industrial cameras are also useful in detecting hidden defects or contaminants, identifying stress points in materials, and inspecting hidden structures. They are commonly used in industries such as automotive, aerospace, electronics and manufacturing.

  1. Inspection and Quality Control: Industrial polarization cameras can be used to acquire multi-angle polarization images of products and materials for detailed inspection and quality control. This is especially useful in industries such as food and beverage, pharmaceutical and electronics manufacturing.

  2. Material Analysis: Industrial polarization cameras can be used to analyze the polarization properties of materials such as plastics, fabrics and metals. This is useful in forensics, product development, and research and development.

  3. Surface Inspection: By analyzing the polarization of light reflected from a surface, industrial polarization cameras can be used to detect defects, scratches and other imperfections. This is useful in industries such as automotive manufacturing, aerospace and electronics.

  4. Machine Vision: Industrial polarization cameras can be integrated into machine vision systems, allowing real-time analysis and processing of polarization images from multiple angles. This is useful in automated inspection and quality control processes.

Overall, industrial polarization cameras provide powerful tools for acquiring detailed and complex images that can be used to improve production processes, product quality, and material analysis in various industries.

​​​

Guess you like

Origin blog.csdn.net/xianzuzhicai/article/details/130415766