Baumer industrial camera Baumer camera how to use the ROI region of interest function (the advantages and industry applications of the PARTIAL SCAN ROI function) (C#)

 project scene

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 ROI function of the Baumer industrial camera is a function that can be directly set in the image chip to set the region of interest, and then cut the part of the scanned image inside the camera and then transmit it to the processor. To a certain extent, it provides the acquisition frame rate of industrial cameras.


technical background

The ROI (Region of Interest) function in Baumer industrial cameras allows users to select a specific part of an image that they want to focus on for analysis or processing. This can improve the efficiency of the image processing system as it can exclude redundant or unnecessary data.

The ROI feature works by selecting a rectangular area within a larger image frame. The camera then applies all subsequent image processing algorithms only to this smaller area. This can improve the speed and accuracy of image analysis because it reduces the amount of data that needs to be processed.

Overall, the ROI features of Baumer industrial cameras can reduce processing time and improve the accuracy of image analysis, making them useful tools in a variety of industrial applications.


code analysis

Baumer industrial camera 004_PartialScan_ExternalBuffer.cs in the Baumer camera SDK sample describes in detail how to configure the ROI function of the camera in the C# environment.

The address of the software SDK example is as follows: Baumer_GAPI_SDK_2.12.0_win_x86_64_cs\examples\src\0_Common\004_PartialScan_ExternalBuffer\004_PartialScan_ExternalBuffer.cs

/*
  This example describes the FIRST STEPS of handling Baumer-GAPI SDK.
  The given source code applies to handling one system, one camera and twenty images.
  Please see "Baumer-GAPI SDK Programmer's Guide" chapter 5.4.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices; // Marshal.Copy()

namespace _004_PartialScan_ExternalBuffer
{
    class Program
    {
        static int Main(string[] args)
        {
            //DECLARATIONS OF VARIABLES
            BGAPI2.SystemList systemList = null;
            BGAPI2.System mSystem = null;
            string sSystemID = "";

            BGAPI2.InterfaceList interfaceList = null;
            BGAPI2.Interface mInterface = null;
            string sInterfaceID = "";

            BGAPI2.DeviceList deviceList = null;
            BGAPI2.Device mDevice = null;
            string sDeviceID = "";

            BGAPI2.DataStreamList datastreamList = null;
            BGAPI2.DataStream mDataStream = null;
            string sDataStreamID = "";

            BGAPI2.BufferList bufferList = null;
            BGAPI2.Buffer mBuffer = null;
            int returnCode = 0;

            //external buffer
            List<IntPtr> LUserBuffer = new List<IntPtr>();

            System.Console.Write("\r\n");
            System.Console.Write("################################################################\r\n");
            System.Console.Write("# PROGRAMMER'S GUIDE Example 004_PartialScan_ExternalBuffer.cs #\r\n");
            System.Console.Write("################################################################\r\n");
            System.Console.Write("\r\n\r\n");


            System.Console.Write("SYSTEM LIST\r\n");
            System.Console.Write("###########\r\n\r\n");

            //COUNTING AVAILABLE SYSTEMS (TL producers)
            try
            {
                systemList = BGAPI2.SystemList.Instance;
                systemList.Refresh();
                System.Console.Write("5.1.2   Detected systems:  {0}\r\n", systemList.Count);

                //SYSTEM DEVICE INFORMATION
                foreach (KeyValuePair<string, BGAPI2.System> sys_pair in BGAPI2.SystemList.Instance)
                {
                    System.Console.Write("  5.2.1   System Name:     {0}\r\n", sys_pair.Value.FileName);
                    System.Console.Write("          System Type:     {0}\r\n", sys_pair.Value.TLType);
                    System.Console.Write("          System Version:  {0}\r\n", sys_pair.Value.Version);
                    System.Console.Write("          System PathName: {0}\r\n\r\n", sys_pair.Value.PathName);
                }
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }


            //OPEN THE FIRST SYSTEM IN THE LIST WITH A CAMERA CONNECTED
            try
            {
                foreach (KeyValuePair<string, BGAPI2.System> sys_pair in BGAPI2.SystemList.Instance)
                {
                    System.Console.Write("SYSTEM\r\n");
                    System.Console.Write("######\r\n\r\n");

                    try
                    {
                        sys_pair.Value.Open();
                        System.Console.Write("5.1.3   Open next system \r\n");
                        System.Console.Write("  5.2.1   System Name:     {0}\r\n", sys_pair.Value.FileName);
                        System.Console.Write("          System Type:     {0}\r\n", sys_pair.Value.TLType);
                        System.Console.Write("          System Version:  {0}\r\n", sys_pair.Value.Version);
                        System.Console.Write("          System PathName: {0}\r\n\r\n", sys_pair.Value.PathName);
                        sSystemID = sys_pair.Key;
                        System.Console.Write("        Opened system - NodeList Information \r\n");
                        System.Console.Write("          GenTL Version:   {0}.{1}\r\n\r\n", (long)sys_pair.Value.NodeList["GenTLVersionMajor"].Value, (long)sys_pair.Value.NodeList["GenTLVersionMinor"].Value);


                        System.Console.Write("INTERFACE LIST\r\n");
                        System.Console.Write("##############\r\n\r\n");

                        try
                        {
                            interfaceList = sys_pair.Value.Interfaces;
                            //COUNT AVAILABLE INTERFACES
                            interfaceList.Refresh(100); // timeout of 100 msec
                            System.Console.Write("5.1.4   Detected interfaces: {0}\r\n", interfaceList.Count);

                            //INTERFACE INFORMATION
                            foreach (KeyValuePair<string, BGAPI2.Interface> ifc_pair in interfaceList)
                            {
                                System.Console.Write("  5.2.2   Interface ID:      {0}\r\n", ifc_pair.Value.Id);
                                System.Console.Write("          Interface Type:    {0}\r\n", ifc_pair.Value.TLType);
                                System.Console.Write("          Interface Name:    {0}\r\n\r\n", ifc_pair.Value.DisplayName);
                            }
                        }
                        catch (BGAPI2.Exceptions.IException ex)
                        {
                            returnCode = (0 == returnCode) ? 1 : returnCode;
                            System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                            System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                            System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
                        }


                        System.Console.Write("INTERFACE\r\n");
                        System.Console.Write("#########\r\n\r\n");

                        //OPEN THE NEXT INTERFACE IN THE LIST
                        try
                        {
                            foreach (KeyValuePair<string, BGAPI2.Interface> ifc_pair in interfaceList)
                            {
                                try
                                {
                                    System.Console.Write("5.1.5   Open interface \r\n");
                                    System.Console.Write("  5.2.2   Interface ID:      {0}\r\n", ifc_pair.Key);
                                    System.Console.Write("          Interface Type:    {0}\r\n", ifc_pair.Value.TLType);
                                    System.Console.Write("          Interface Name:    {0}\r\n", ifc_pair.Value.DisplayName);
                                    ifc_pair.Value.Open();
                                    //search for any camera is connetced to this interface
                                    deviceList = ifc_pair.Value.Devices;
                                    deviceList.Refresh(100);
                                    if (deviceList.Count == 0)
                                    {
                                        System.Console.Write("5.1.13   Close interface ({0} cameras found) \r\n\r\n", deviceList.Count);
                                        ifc_pair.Value.Close();
                                    }
                                    else
                                    {
                                        sInterfaceID = ifc_pair.Key;
                                        System.Console.Write("  \r\n");
                                        System.Console.Write("        Opened interface - NodeList Information \r\n");
                                        if (ifc_pair.Value.TLType == "GEV")
                                        {
                                            long iIPAddress = (long)ifc_pair.Value.NodeList["GevInterfaceSubnetIPAddress"].Value;
                                            System.Console.Write("          GevInterfaceSubnetIPAddress: {0}.{1}.{2}.{3}\r\n", (iIPAddress & 0xff000000) >> 24,
                                                                                                                            (iIPAddress & 0x00ff0000) >> 16,
                                                                                                                            (iIPAddress & 0x0000ff00) >> 8,
                                                                                                                            (iIPAddress & 0x000000ff));
                                            long iSubnetMask = (long)ifc_pair.Value.NodeList["GevInterfaceSubnetMask"].Value;
                                            System.Console.Write("          GevInterfaceSubnetMask:      {0}.{1}.{2}.{3}\r\n", (iSubnetMask & 0xff000000) >> 24,
                                                                                                                            (iSubnetMask & 0x00ff0000) >> 16,
                                                                                                                            (iSubnetMask & 0x0000ff00) >> 8,
                                                                                                                            (iSubnetMask & 0x000000ff));
                                        }
                                        if (ifc_pair.Value.TLType == "U3V")
                                        {
                                            //System.Console.Write("          NodeListCount:               {0}\r\n", ifc_pair.Value.NodeList.Count);
                                        }
                                        System.Console.Write("  \r\n");
                                        break;
                                    }
                                }
                                catch (BGAPI2.Exceptions.ResourceInUseException ex)
                                {
                                    returnCode = (0 == returnCode) ? 1 : returnCode;
                                    System.Console.Write(" Interface {0} already opened \r\n", ifc_pair.Key);
                                    System.Console.Write(" ResourceInUseException {0} \r\n", ex.GetErrorDescription());
                                }
                            }
                        }
                        catch (BGAPI2.Exceptions.IException ex)
                        {
                            returnCode = (0 == returnCode) ? 1 : returnCode;
                            System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                            System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                            System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
                        }

                        //if a camera is connected to the system interface then leave the system loop
                        if (sInterfaceID != "")
                        {
                            break;
                        }
                    }
                    catch (BGAPI2.Exceptions.ResourceInUseException ex)
                    {
                        returnCode = (0 == returnCode) ? 1 : returnCode;
                        System.Console.Write(" System {0} already opened \r\n", sys_pair.Key);
                        System.Console.Write(" ResourceInUseException {0} \r\n", ex.GetErrorDescription());
                    }
                }
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }

            if (sSystemID == "")
            {
                System.Console.Write(" No System found \r\n");
                System.Console.Write(" Input any number to close the program:\r\n");
                Console.Read();
                return returnCode;
            }
            else
            {
                mSystem = systemList[sSystemID];
            }


            if (sInterfaceID == "")
            {
                System.Console.Write(" No Interface of TLType 'GEV' found \r\n");
                System.Console.Write("\r\nEnd\r\nInput any number to close the program:\r\n");
                Console.Read();
                mSystem.Close();
                return returnCode;
            }
            else
            {
                mInterface = interfaceList[sInterfaceID];
            }


            System.Console.Write("DEVICE LIST\r\n");
            System.Console.Write("###########\r\n\r\n");

            try
            {
                //COUNTING AVAILABLE CAMERAS
                deviceList = mInterface.Devices;
                deviceList.Refresh(100);
                System.Console.Write("5.1.6   Detected devices:         {0}\r\n", deviceList.Count);

                //DEVICE INFORMATION BEFORE OPENING
                foreach (KeyValuePair<string, BGAPI2.Device> dev_pair in deviceList)
                {
                    System.Console.Write("  5.2.3   Device DeviceID:        {0}\r\n", dev_pair.Key);
                    System.Console.Write("          Device Model:           {0}\r\n", dev_pair.Value.Model);
                    System.Console.Write("          Device SerialNumber:    {0}\r\n", dev_pair.Value.SerialNumber);
                    System.Console.Write("          Device Vendor:          {0}\r\n", dev_pair.Value.Vendor);
                    System.Console.Write("          Device TLType:          {0}\r\n", dev_pair.Value.TLType);
                    System.Console.Write("          Device AccessStatus:    {0}\r\n", dev_pair.Value.AccessStatus);
                    System.Console.Write("          Device UserID:          {0}\r\n\r\n", dev_pair.Value.DisplayName);
                }
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }


            System.Console.Write("DEVICE\r\n");
            System.Console.Write("######\r\n\r\n");

            //OPEN THE FIRST CAMERA IN THE LIST
            try
            {
                foreach (KeyValuePair<string, BGAPI2.Device> dev_pair in deviceList)
                {
                    try
                    {
                        System.Console.Write("5.1.7   Open first device \r\n");
                        System.Console.Write("          Device DeviceID:        {0}\r\n", dev_pair.Value.Id);
                        System.Console.Write("          Device Model:           {0}\r\n", dev_pair.Value.Model);
                        System.Console.Write("          Device SerialNumber:    {0}\r\n", dev_pair.Value.SerialNumber);
                        System.Console.Write("          Device Vendor:          {0}\r\n", dev_pair.Value.Vendor);
                        System.Console.Write("          Device TLType:          {0}\r\n", dev_pair.Value.TLType);
                        System.Console.Write("          Device AccessStatus:    {0}\r\n", dev_pair.Value.AccessStatus);
                        System.Console.Write("          Device UserID:          {0}\r\n\r\n", dev_pair.Value.DisplayName);
                        dev_pair.Value.Open();
                        sDeviceID = dev_pair.Key;
                        System.Console.Write("        Opened device - RemoteNodeList Information \r\n");
                        System.Console.Write("          Device AccessStatus:    {0}\r\n", dev_pair.Value.AccessStatus);

                        //SERIAL NUMBER
                        if (dev_pair.Value.RemoteNodeList.GetNodePresent("DeviceSerialNumber") == true)
                            System.Console.Write("          DeviceSerialNumber:     {0}\r\n", (string)dev_pair.Value.RemoteNodeList["DeviceSerialNumber"].Value);
                        else if (dev_pair.Value.RemoteNodeList.GetNodePresent("DeviceID") == true)
                            System.Console.Write("          DeviceID (SN):          {0}\r\n", (string)dev_pair.Value.RemoteNodeList["DeviceID"].Value);
                        else
                            System.Console.Write("          SerialNumber:           Not Available \r\n");

                        //DISPLAY DEVICEMANUFACTURERINFO
                        if (dev_pair.Value.RemoteNodeList.GetNodePresent("DeviceManufacturerInfo") == true)
                            System.Console.Write("          DeviceManufacturerInfo: {0}\r\n", (string)dev_pair.Value.RemoteNodeList["DeviceManufacturerInfo"].Value);

                        //DISPLAY DEVICEFIRMWAREVERSION OR DEVICEVERSION
                        if (dev_pair.Value.RemoteNodeList.GetNodePresent("DeviceFirmwareVersion") == true)
                            System.Console.Write("          DeviceFirmwareVersion:  {0}\r\n", (string)dev_pair.Value.RemoteNodeList["DeviceFirmwareVersion"].Value);
                        else if (dev_pair.Value.RemoteNodeList.GetNodePresent("DeviceVersion") == true)
                            System.Console.Write("          DeviceVersion:          {0}\r\n", (string)dev_pair.Value.RemoteNodeList["DeviceVersion"].Value);
                        else
                            System.Console.Write("          DeviceVersion:          Not Available \r\n");

                        if (dev_pair.Value.TLType == "GEV")
                        {
                            System.Console.Write("          GevCCP:                 {0}\r\n", (string)dev_pair.Value.RemoteNodeList["GevCCP"].Value);
                            System.Console.Write("          GevCurrentIPAddress:    {0}.{1}.{2}.{3}\r\n", ((long)dev_pair.Value.RemoteNodeList["GevCurrentIPAddress"].Value & 0xff000000) >> 24, ((long)dev_pair.Value.RemoteNodeList["GevCurrentIPAddress"].Value & 0x00ff0000) >> 16, ((long)dev_pair.Value.RemoteNodeList["GevCurrentIPAddress"].Value & 0x0000ff00) >> 8, ((long)dev_pair.Value.RemoteNodeList["GevCurrentIPAddress"].Value & 0x000000ff));
                            System.Console.Write("          GevCurrentSubnetMask:   {0}.{1}.{2}.{3}\r\n", ((long)dev_pair.Value.RemoteNodeList["GevCurrentSubnetMask"].Value & 0xff000000) >> 24, ((long)dev_pair.Value.RemoteNodeList["GevCurrentSubnetMask"].Value & 0x00ff0000) >> 16, ((long)dev_pair.Value.RemoteNodeList["GevCurrentSubnetMask"].Value & 0x0000ff00) >> 8, ((long)dev_pair.Value.RemoteNodeList["GevCurrentSubnetMask"].Value & 0x000000ff));
                        }
                        System.Console.Write("          \r\n");
                        break;
                    }
                    catch (BGAPI2.Exceptions.ResourceInUseException ex)
                    {
                        returnCode = (0 == returnCode) ? 1 : returnCode;
                        System.Console.Write(" Device {0} already opened \r\n", dev_pair.Key);
                        System.Console.Write(" ResourceInUseException {0} \r\n", ex.GetErrorDescription());
                    }
                    catch (BGAPI2.Exceptions.AccessDeniedException ex)
                    {
                        returnCode = (0 == returnCode) ? 1 : returnCode;
                        System.Console.Write(" Device {0} already opened \r\n", dev_pair.Key);
                        System.Console.Write(" AccessDeniedException {0} \r\n", ex.GetErrorDescription());
                    }
                }
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }

            if (sDeviceID == "")
            {
                System.Console.Write(" No Device found \r\n");
                System.Console.Write("\r\nEnd\r\nInput any number to close the program:\r\n");
                Console.Read();
                mInterface.Close();
                mSystem.Close();
                return returnCode;
            }
            else
            {
                mDevice = deviceList[sDeviceID];
            }


            System.Console.Write("DEVICE PARAMETER SETUP\r\n");
            System.Console.Write("######################\r\n\r\n");

            try
            {
                //SET TRIGGER MODE OFF (FreeRun)
                mDevice.RemoteNodeList["TriggerMode"].Value = "Off";
                System.Console.Write("        TriggerMode:              {0}\r\n", (string)mDevice.RemoteNodeList["TriggerMode"].Value);
                System.Console.Write("  \r\n");


                //SET A PARTIAL SCAN (ROI...REGION OF INTEREST) IN THE RIGHT BOTTOM AREA OF THE IMAGE
                //====================================================================================
                System.Console.Write("    Set ROI parameters to the right bottom quarter of the sensor\r\n\r\n");

                //IMAGE WIDTH
                //===========
                System.Console.Write("        Width\r\n", (string)mDevice.RemoteNodeList["Width"].Interface);
                System.Console.Write("          description:            {0}\r\n", (string)mDevice.RemoteNodeList["Width"].Description);
                System.Console.Write("          interface type:         {0}\r\n", (string)mDevice.RemoteNodeList["Width"].Interface);

                long iImageWidth = 0;
                long iImageWidthMin = 0;
                long iImageWidthMax = 0;
                long iImageWidthInc = 0;

                //get current value and limits
                iImageWidth = (long)mDevice.RemoteNodeList["Width"].Value;
                iImageWidthMin = (long)mDevice.RemoteNodeList["Width"].Min;
                iImageWidthMax = (long)mDevice.RemoteNodeList["Width"].Max;
                iImageWidthInc = (long)mDevice.RemoteNodeList["Width"].Inc;

                System.Console.Write("          current value:          {0}\r\n", iImageWidth);
                System.Console.Write("          possible value range:   {0} to {1} with increment of {2}\r\n", iImageWidthMin, iImageWidthMax, iImageWidthInc);

                //set new width value same to the half value of sensor width
                long widthvalue = (long)mDevice.RemoteNodeList["SensorWidth"].Value / 2 / iImageWidthInc * iImageWidthInc; // find number to match the increment

                // check new value is within range
                if (widthvalue < iImageWidthMin)
                    widthvalue = iImageWidthMin;

                if (widthvalue > iImageWidthMax)
                    widthvalue = iImageWidthMax;

                mDevice.RemoteNodeList["Width"].Value = widthvalue;

                //recheck new width is set
                System.Console.Write("          set value to:           {0} is half value of the sensor width: {1}\r\n\r\n", (long)mDevice.RemoteNodeList["Width"].Value, (long)mDevice.RemoteNodeList["SensorWidth"].Value);


                //IMAGE OFFSET X
                //==============
                System.Console.Write("        OffsetX\r\n");
                System.Console.Write("          description:            {0}\r\n", (string)mDevice.RemoteNodeList["OffsetX"].Description);
                System.Console.Write("          interface type:         {0}\r\n", (string)mDevice.RemoteNodeList["OffsetX"].Interface);
                long iImageOffsetX = 0;
                long iImageOffsetXMin = 0;
                long iImageOffsetXMax = 0;
                long iImageOffsetXInc = 0;

                //get current value and limits
                iImageOffsetX = (long)mDevice.RemoteNodeList["OffsetX"].Value;
                iImageOffsetXMin = (long)mDevice.RemoteNodeList["OffsetX"].Min;
                iImageOffsetXMax = (long)mDevice.RemoteNodeList["OffsetX"].Max;
                iImageOffsetXInc = (long)mDevice.RemoteNodeList["OffsetX"].Inc;

                System.Console.Write("          current value:          {0}\r\n", iImageOffsetX);
                System.Console.Write("          possible value range:   {0} to {1} with increment of {2}\r\n", iImageOffsetXMin, iImageOffsetXMax, iImageOffsetXInc);

                //set new OffsetX value same to the half value of sensor width
                long OffsetXvalue = (long)mDevice.RemoteNodeList["SensorWidth"].Value / 2 / iImageOffsetXInc * iImageOffsetXInc; // find number to match the increment

                // check new value is within range
                if (OffsetXvalue < iImageOffsetXMin)
                    OffsetXvalue = iImageOffsetXMin;

                if (OffsetXvalue > iImageOffsetXMax)
                    OffsetXvalue = iImageOffsetXMax;

                mDevice.RemoteNodeList["OffsetX"].Value = OffsetXvalue;

                //recheck new OffsetX is set
                System.Console.Write("          set value to:           {0} is half value of the sensor width: {1}\r\n\r\n", (long)mDevice.RemoteNodeList["OffsetX"].Value, (long)mDevice.RemoteNodeList["SensorWidth"].Value);


                //IMAGE HEIGHT
                //============
                System.Console.Write("        Height\r\n");
                System.Console.Write("          description:            {0}\r\n", (string)mDevice.RemoteNodeList["Height"].Description);
                System.Console.Write("          interface type:         {0}\r\n", (string)mDevice.RemoteNodeList["Height"].Interface);
                long iImageHeight = 0;
                long iImageHeightMin = 0;
                long iImageHeightMax = 0;
                long iImageHeightInc = 0;

                //get current value and limits
                iImageHeight = (long)mDevice.RemoteNodeList["Height"].Value;
                iImageHeightMin = (long)mDevice.RemoteNodeList["Height"].Min;
                iImageHeightMax = (long)mDevice.RemoteNodeList["Height"].Max;
                iImageHeightInc = (long)mDevice.RemoteNodeList["Height"].Inc;

                System.Console.Write("          current value:          {0}\r\n", iImageHeight);
                System.Console.Write("          possible value range:   {0} to {1} with increment of {2}\r\n", iImageHeightMin, iImageHeightMax, iImageHeightInc);

                //set new height value same to the half value of sensor height
                long heightvalue = (long)mDevice.RemoteNodeList["SensorHeight"].Value / 2 / iImageHeightInc * iImageHeightInc; // find number to match the increment

                // check new value is within range
                if (heightvalue < iImageHeightMin)
                    heightvalue = iImageHeightMin;

                if (heightvalue > iImageHeightMax)
                    heightvalue = iImageHeightMax;

                mDevice.RemoteNodeList["Height"].Value = heightvalue;

                //recheck new height is set
                System.Console.Write("          set value to:           {0} is half value of the sensor height: {1}\r\n\r\n", (long)mDevice.RemoteNodeList["Height"].Value, (long)mDevice.RemoteNodeList["SensorHeight"].Value);


                //IMAGE OFFSET Y
                //==============
                System.Console.Write("        OffsetY\r\n");
                System.Console.Write("          description:            {0}\r\n", (string)mDevice.RemoteNodeList["OffsetY"].Description);
                System.Console.Write("          interface type:         {0}\r\n", (string)mDevice.RemoteNodeList["OffsetY"].Interface);
                long iImageOffsetY = 0;
                long iImageOffsetYMin = 0;
                long iImageOffsetYMax = 0;
                long iImageOffsetYInc = 0;

                //get current value and limits
                iImageOffsetY = (long)mDevice.RemoteNodeList["OffsetY"].Value;
                iImageOffsetYMin = (long)mDevice.RemoteNodeList["OffsetY"].Min;
                iImageOffsetYMax = (long)mDevice.RemoteNodeList["OffsetY"].Max;
                iImageOffsetYInc = (long)mDevice.RemoteNodeList["OffsetY"].Inc;

                System.Console.Write("          current value:          {0}\r\n", iImageOffsetY);
                System.Console.Write("          possible value range:   {0} to {1} with increment of {2}\r\n", iImageOffsetYMin, iImageOffsetYMax, iImageOffsetYInc);

                //set new OffsetY value same to the half value of sensor height
                long OffsetYvalue = (long)mDevice.RemoteNodeList["SensorHeight"].Value / 2 / iImageOffsetYInc * iImageOffsetYInc; // find number to match the increment

                // check new value is within range
                if (OffsetYvalue < iImageOffsetYMin)
                    OffsetYvalue = iImageOffsetYMin;

                if (OffsetYvalue > iImageOffsetYMax)
                    OffsetYvalue = iImageOffsetYMax;

                mDevice.RemoteNodeList["OffsetY"].Value = OffsetYvalue;

                //recheck new OffsetY is set
                System.Console.Write("          set value to:           {0} is half value of the sensor height: {1}\r\n\r\n", (long)mDevice.RemoteNodeList["OffsetY"].Value, (long)mDevice.RemoteNodeList["SensorHeight"].Value);

            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }


            System.Console.Write("DATA STREAM LIST\r\n");
            System.Console.Write("################\r\n\r\n");

            try
            {
                //COUNTING AVAILABLE DATASTREAMS
                datastreamList = mDevice.DataStreams;
                datastreamList.Refresh();
                System.Console.Write("5.1.8   Detected datastreams:     {0}\r\n", datastreamList.Count);

                //DATASTREAM INFORMATION BEFORE OPENING
                foreach (KeyValuePair<string, BGAPI2.DataStream> dst_pair in datastreamList)
                {
                    System.Console.Write("  5.2.4   DataStream ID:          {0}\r\n\r\n", dst_pair.Key);
                }
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }


            System.Console.Write("DATA STREAM\r\n");
            System.Console.Write("###########\r\n\r\n");

            //OPEN THE FIRST DATASTREAM IN THE LIST
            try
            {
                foreach (KeyValuePair<string, BGAPI2.DataStream> dst_pair in datastreamList)
                {
                    System.Console.Write("5.1.9   Open first datastream \r\n");
                    System.Console.Write("          DataStream ID:          {0}\r\n\r\n", dst_pair.Key);
                    dst_pair.Value.Open();
                    sDataStreamID = dst_pair.Key;
                    System.Console.Write("        Opened datastream - NodeList Information \r\n");
                    System.Console.Write("          StreamAnnounceBufferMinimum:  {0}\r\n", dst_pair.Value.NodeList["StreamAnnounceBufferMinimum"].Value);
                    if (dst_pair.Value.TLType == "GEV")
                    {
                        System.Console.Write("          StreamDriverModel:            {0}\r\n", dst_pair.Value.NodeList["StreamDriverModel"].Value);
                    }
                    System.Console.Write("  \r\n");
                    break;
                }
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }

            if (sDataStreamID == "")
            {
                System.Console.Write(" No DataStream found \r\n");
                System.Console.Write("\r\nEnd\r\nInput any number to close the program:\r\n");
                Console.Read();
                mDevice.Close();
                mInterface.Close();
                mSystem.Close();
                return returnCode;
            }
            else
            {
                mDataStream = datastreamList[sDataStreamID];
            }


            System.Console.Write("BUFFER LIST\r\n");
            System.Console.Write("###########\r\n\r\n");

            try
            {
                //BufferList
                bufferList = mDataStream.BufferList;

                // 4 buffers using external buffer mode
                System.Console.Write("5.4.2   External buffers\r\n");

                long iDevicePayloadsize = (long)mDevice.RemoteNodeList["PayloadSize"].Value;
                System.Console.Write("         payloadsize required for external buffer [bytes]: {0}\r\n", iDevicePayloadsize);

                IntPtr mUserBuffer = new IntPtr(0);
                
                ulong uPayloadSize = mDataStream.IsDefinedPayloadSize ? mDataStream.PayloadSize : (ulong)iDevicePayloadsize;
                for (int i = 0; i < 4; i++) // 4 buffers using external allocated memory
                {
                    mUserBuffer = Marshal.AllocHGlobal((int)uPayloadSize); // use Marshal.FreeHGlobal(mUserBuffer) when releasing resources
                    LUserBuffer.Add(mUserBuffer);
                    IntPtr pUserObj = new IntPtr(0); // NULL pointer, not used
                    mBuffer = new BGAPI2.Buffer(mUserBuffer, uPayloadSize, pUserObj);
                    bufferList.Add(mBuffer);
                    System.Console.Write("         add external buffer [{0}]\r\n", i);
                }
                System.Console.Write("5.1.10   Announced buffers:       {0} using {1} [bytes]\r\n", bufferList.AnnouncedCount, mBuffer.MemSize * bufferList.AnnouncedCount);
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }


            try
            {
                foreach (KeyValuePair<string, BGAPI2.Buffer> buf_pair in bufferList)
                {
                    buf_pair.Value.QueueBuffer();
                }
                System.Console.Write("5.1.11   Queued buffers:          {0}\r\n", bufferList.QueuedCount);
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }
            System.Console.Write("\r\n");


            System.Console.Write("CAMERA START\r\n");
            System.Console.Write("############\r\n\r\n");

            //START DATASTREAM ACQUISITION
            try
            {
                mDataStream.StartAcquisition();
                System.Console.Write("5.1.12   DataStream started \r\n");
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }

            //START CAMERA
            try
            {
                mDevice.RemoteNodeList["AcquisitionStart"].Execute();
                System.Console.Write("5.1.12   {0} started\r\n", mDevice.Model);
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }

            //CAPTURE 20 IMAGES
            System.Console.Write("\r\n");
            System.Console.Write("CAPTURE 20 IMAGES BY IMAGE POLLING USING INTERNAL BUFFERS\r\n");
            System.Console.Write("#########################################################\r\n\r\n");

            BGAPI2.Buffer mBufferFilled = null;
            try
            {
                for (int i = 0; i < 20; i++)
                {
                    mBufferFilled = mDataStream.GetFilledBuffer(1000); // image polling timeout 1000 msec
                    if (mBufferFilled == null)
                    {
                        System.Console.Write("Error: Buffer Timeout after 1000 msec\r\n");
                    }
                    else if (mBuffer.IsIncomplete == true)
                    {
                        System.Console.Write("Error: Image is incomplete\r\n");
                        // queue buffer again
                        mBufferFilled.QueueBuffer();
                    }
                    else
                    {
                        System.Console.Write(" Image {0, 5:d} received in memory address {1:X}", mBufferFilled.FrameID, (ulong)mBufferFilled.MemPtr);
                        System.Console.Write(" size {0} [bytes]\r\n", (ulong)mBufferFilled.MemSize);
                        // queue buffer again
                        mBufferFilled.QueueBuffer();
                    }
                }
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }
            System.Console.Write("\r\n");


            System.Console.Write("CAMERA STOP\r\n");
            System.Console.Write("###########\r\n\r\n");

            String sExposureNodeName = "";
            if (mDevice.GetRemoteNodeList().GetNodePresent("ExposureTime"))
            {
                sExposureNodeName = "ExposureTime";
            }
            else if (mDevice.GetRemoteNodeList().GetNodePresent("ExposureTimeAbs"))
            {
                sExposureNodeName = "ExposureTimeAbs";
            }

            //STOP CAMERA
            try
            {
                if (mDevice.RemoteNodeList.GetNodePresent("AcquisitionAbort") == true)
                {
                    mDevice.RemoteNodeList["AcquisitionAbort"].Execute();
                    System.Console.Write("5.1.12   {0} aborted\r\n", mDevice.Model);
                }

                mDevice.RemoteNodeList["AcquisitionStop"].Execute();
                System.Console.Write("5.1.12   {0} stopped\r\n", mDevice.Model);
                System.Console.Write("\r\n");

                System.Console.Write("         ExposureTime:                   {0} [{1}]\r\n", (double)mDevice.RemoteNodeList[sExposureNodeName].Value, (string)mDevice.RemoteNodeList[sExposureNodeName].Unit);
                if (mDevice.TLType == "GEV")
                {
                    if (mDevice.RemoteNodeList.GetNodePresent("DeviceStreamChannelPacketSize") == true)
                        System.Console.Write("         DeviceStreamChannelPacketSize:  {0} [bytes]\r\n", (long)mDevice.RemoteNodeList["DeviceStreamChannelPacketSize"].Value);
                    else
                        System.Console.Write("         GevSCPSPacketSize:              {0} [bytes]\r\n", (long)mDevice.RemoteNodeList["GevSCPSPacketSize"].Value);
                    System.Console.Write("         GevSCPD (PacketDelay):          {0} [tics]\r\n", (long)mDevice.RemoteNodeList["GevSCPD"].Value);
                }
                System.Console.Write("\r\n");
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }


            //STOP DataStream acquisition & release buffers
            try
            {
                if (mDataStream.TLType == "GEV")
                {
                    //DataStream Statistics
                    System.Console.Write("         DataStream Statistics \r\n");
                    System.Console.Write("           DataBlockComplete:              {0}\r\n", (long)mDataStream.NodeList["DataBlockComplete"].Value);
                    System.Console.Write("           DataBlockInComplete:            {0}\r\n", (long)mDataStream.NodeList["DataBlockInComplete"].Value);
                    System.Console.Write("           DataBlockMissing:               {0}\r\n", (long)mDataStream.NodeList["DataBlockMissing"].Value);
                    System.Console.Write("           PacketResendRequestSingle:      {0}\r\n", (long)mDataStream.NodeList["PacketResendRequestSingle"].Value);
                    System.Console.Write("           PacketResendRequestRange:       {0}\r\n", (long)mDataStream.NodeList["PacketResendRequestRange"].Value);
                    System.Console.Write("           PacketResendReceive:            {0}\r\n", (long)mDataStream.NodeList["PacketResendReceive"].Value);
                    System.Console.Write("           DataBlockDroppedBufferUnderrun: {0}\r\n", (long)mDataStream.NodeList["DataBlockDroppedBufferUnderrun"].Value);
                    System.Console.Write("           Bitrate:                        {0}\r\n", (double)mDataStream.NodeList["Bitrate"].Value);
                    System.Console.Write("           Throughput:                     {0}\r\n", (double)mDataStream.NodeList["Throughput"].Value);
                    System.Console.Write("\r\n");
                }
                if (mDataStream.TLType == "U3V")
                {
                    //DataStream Statistics
                    System.Console.Write("         DataStream Statistics \r\n");
                    System.Console.Write("           GoodFrames:            {0}\r\n", (long)mDataStream.NodeList["GoodFrames"].Value);
                    System.Console.Write("           CorruptedFrames:       {0}\r\n", (long)mDataStream.NodeList["CorruptedFrames"].Value);
                    System.Console.Write("           LostFrames:            {0}\r\n", (long)mDataStream.NodeList["LostFrames"].Value);
                    System.Console.Write("\r\n");
                }
                mDataStream.StopAcquisition();
                System.Console.Write("5.1.12   DataStream stopped \r\n");
                bufferList.DiscardAllBuffers();
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0}.\r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0}.\r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }
            System.Console.Write("\r\n");


            System.Console.Write("RELEASE\r\n");
            System.Console.Write("#######\r\n\r\n");

            //Release buffers
            System.Console.Write("5.1.13   Releasing the resources\r\n");
            try
            {
                while (bufferList.Count > 0)
                {
                    mBuffer = (BGAPI2.Buffer)bufferList.Values.First();
                    bufferList.RevokeBuffer(mBuffer);
                }
                System.Console.Write("         buffers after revoke:    {0}\r\n", bufferList.Count);

                foreach (IntPtr mBufferPointer in LUserBuffer)
                {
                    Marshal.FreeHGlobal(mBufferPointer);
                }
                System.Console.WriteLine("5.4.2    memory blocks of external buffers deleted");
                mDataStream.Close();
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }

            //DEVICE PARAMETER RESET
            try
            {
                System.Console.Write("5.3.1    Reset ROI parameters\r\n");
                mDevice.RemoteNodeList["OffsetY"].Value = (long)mDevice.RemoteNodeList["OffsetY"].Min;
                mDevice.RemoteNodeList["Height"].Value = (long)mDevice.RemoteNodeList["Height"].Max;
                mDevice.RemoteNodeList["OffsetX"].Value = (long)mDevice.RemoteNodeList["OffsetX"].Min;
                mDevice.RemoteNodeList["Width"].Value = (long)mDevice.RemoteNodeList["Width"].Max;
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0} \r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0} \r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }

            try
            {
                mDevice.Close();
                mInterface.Close();
                mSystem.Close();
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                returnCode = (0 == returnCode) ? 1 : returnCode;
                System.Console.Write("ExceptionType:    {0}.\r\n", ex.GetType());
                System.Console.Write("ErrorDescription: {0}.\r\n", ex.GetErrorDescription());
                System.Console.Write("in function:      {0} \r\n", ex.GetFunctionName());
            }

            System.Console.Write("\r\nEnd\r\n\r\n");
            System.Console.Write("Input any number to close the program:\r\n");
            Console.Read();
            return returnCode;
        }
    }
}

 


Advantages of industrial camera ROI image function

The ROI (Region of Interest) function of Baumer industrial cameras offers significant advantages, incl.

1. Improve precision: By highlighting a specific region of interest in an image, the ROI function can make measurement and analysis more precise and accurate.

2. Reduced processing time. By focusing processing power on specific areas of the image, the ROI feature reduces the amount of data that needs to be processed, resulting in faster analysis and processing times.

3. Improve image quality. The ROI function can be used to crop out unwanted areas of the image, resulting in higher resolution and better image quality.

4. Increase flexibility. The ROI function allows users to customize regions of interest in real time, enabling more efficient monitoring and analysis of specific regions of interest.

5. Improve the dynamic range. The ROI feature can also be used to improve the dynamic range of an image, as it enables users to adjust exposure settings for specific areas within the image.

In general, the ROI function of industrial cameras can significantly improve the accuracy, speed and efficiency of imaging and analysis in a wide range of industrial applications.


Industrial application of industrial camera ROI function

The ROI (Region of Interest) function of Baumer industrial cameras can be applied in various scenarios. Below are a few examples.

1. Quality control and inspection. The ROI function can be used to focus on a certain region of interest in a product or sample and capture high-quality images for analysis.

2. Machine vision. In automated manufacturing processes, ROI capabilities can help identify specific parts or components on the production line for inspection and analysis.

3. Surveillance and Security. The ROI feature can be used to zoom in on specific areas of the surveillance video frame, ensuring any suspicious activity is captured in high definition.

4. Scientific research. In fields such as biotechnology or materials science, the ROI function can help researchers capture detailed images of samples, cells or materials for analysis.

Overall, the ROI feature enables industrial cameras to capture and ROI a specific area, making it an invaluable tool in various industries.

Guess you like

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