Baumer industrial camera Baumer industrial camera how to zoom in and zoom out the image through BGAPISDK (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.
​Baumer
industrial cameras are often used in the field of high-speed synchronous acquisition due to their superior and stable performance and quality, and usually use various image algorithms to improve the quality of the captured images.

Technical background of Baumer industrial camera BGAPISDK and image zoom in and out

The BGAPI SDK for Baumer industrial cameras is a set of software development kits developed by Baumer for its camera product series. The SDK provides a set of APIs that enable developers to write professional applications to control, capture, process and display images and data from Baumer cameras. BGAPI SDK supports multiple programming languages, including C++, C#, Visual Basic, LabVIEW, Matlab, etc., and provides a large number of sample codes and documents to help users get started easily and quickly complete application development.

The BGAPI SDK provides a wealth of functions to control all parameters of the Baumer camera, including exposure time, gain, white balance, trigger mode, etc., and supports various data formats, such as Raw, BMP, JPG, etc., and also provides real-time display , data acquisition, image processing and other functions, providing developers with highly customized solutions. In addition, BGAPI SDK also supports the development of multi-camera systems, and can support various computer operating systems, such as Windows, Linux, Mac OS, etc.

Images can be enlarged and reduced as well as displayed by using the industrial camera's software development kit (SDK). This usually involves using functions or methods in the SDK to access the camera's image frame and pass it to a display control or drawing area.

This article introduces the function of using the BGAPI SDK to zoom in and zoom out the camera image.

Baumer industrial camera zooms in and out of camera images through BGAPISDK

The following describes how the Baumer industrial camera zooms in and out of the camera image through the BGAPISDK in C#

1. Reference the appropriate class file

The code is as follows (example):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Data;
using System.Globalization;
using System.Diagnostics;
using System.IO;

2. Use BGAPISDK to zoom in and zoom out the camera image

The core code of the Baumer industrial camera zooming in and out of the camera image is as follows:

SystemList 
Open a System 
Get the InterfaceList and fill it Open an Interface 
private void mPictureBox_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    
    
    Bitmap bmptodraw = null;
    //Bitmap cdbmptodraw = null;
    //Bitmap chbmptodraw = null;
    Rectangle bitmap_complete = new Rectangle(); //rectangle over all bitmaps
    Rectangle src_complete = new Rectangle();
    Rectangle dest_complete = new Rectangle();
    double ratioW = 1.0f;
    double ratioH = 1.0f;
    Rectangle bitmap_image = new Rectangle();
    //Rectangle bitmap_dist = new Rectangle();
    //Rectangle bitmap_header = new Rectangle();
    Point pictureboxlocation = new Point();
    Rectangle src_image = new Rectangle();
    //Rectangle src_header = new Rectangle();
    //Rectangle src_dist = new Rectangle();
    Rectangle dest_image = new Rectangle();
    //Rectangle dest_header = new Rectangle();
    //Rectangle dest_dist = new Rectangle();


    //int ret = 0;
    //SizeF header_size = new SizeF();
    int idx = -1;
    Size destbmp = new Size(1, 1);



    if (mbNewImageReady == true)
    {
    
    
        mbDrawFirstBitmap = !mbDrawFirstBitmap;
        mbFlipDrawImage = true;
    }

    mbNewImageReady = false;
    if (mbDrawFirstBitmap)
    {
    
    
        idx = 0;
        if (mImageData[0].bgapiImage == null)
            return;
        bmptodraw = mImageData[0].bmp;
    }
    else
    {
    
    
        idx = 1;
        if (mImageData[1].bgapiImage == null)
            return;
        bmptodraw = mImageData[1].bmp;
    }

    if (!mShowImage)
        return;

    if (mShowImage)
    {
    
    
        //destbmp.Width += bmptodraw.Width;
        //destbmp.Height += bmptodraw.Height;
        bitmap_complete.Width += bmptodraw.Width;
        bitmap_complete.Height = Math.Max(bitmap_complete.Height, bmptodraw.Height);
    }
          
    //calculate the destination rectangle of the several data blocks
    int nwx = 0;
    if (mShowImage)
    {
    
    
        bitmap_image.X = nwx;
        bitmap_image.Y = 0;
        bitmap_image.Width = bmptodraw.Width;
        bitmap_image.Height = bmptodraw.Height;
        nwx += bmptodraw.Width;
    }
    else
    {
    
    
        if (!bitmap_image.IsEmpty)
            bitmap_image.X = bitmap_image.Y = bitmap_image.Width = bitmap_image.Height = 0;
    }
            
    //adjust pictureBox size 
    if (mSizeMode == PictureBoxSizeMode.Normal)
    {
    
    
        this.pictureBox1.Width = bitmap_complete.Width;
        this.pictureBox1.Height = bitmap_complete.Height;
        this.panel1.AutoScroll = true;
    }
    else if (mSizeMode == PictureBoxSizeMode.StretchImage)
    {
    
    
        if (mbFitToWindow)
        {
    
    
            ratioW = (double)bitmap_complete.Width / (double)panel1.Width;
            ratioH = (double)bitmap_complete.Height / (double)panel1.Height;
            if (ratioW > ratioH)
            {
    
    
                this.pictureBox1.Height = (int)((double)bitmap_complete.Height / ratioW);
                this.pictureBox1.Width = panel1.Width;
            }
            else
            {
    
    
                this.pictureBox1.Width = (int)((double)bitmap_complete.Width / ratioH);
                this.pictureBox1.Height = panel1.Height;
            }
            this.panel1.AutoScroll = false;
        }
        else
        {
    
    //--1
            this.pictureBox1.Width = Convert.ToInt32(bitmap_complete.Width * ZOOMFACTOR);
            this.pictureBox1.Height = Convert.ToInt32(bitmap_complete.Height * ZOOMFACTOR);
            this.panel1.AutoScroll = true;
        }
    }
    else
    {
    
    
        if (bitmap_complete.Width > panel1.Width)
            this.pictureBox1.Width = panel1.Width;
        else
            this.pictureBox1.Width = bitmap_complete.Width;

        if (bitmap_complete.Height > panel1.Height)
            this.pictureBox1.Height = panel1.Height;
        else
            this.pictureBox1.Height = bitmap_complete.Height;
        this.panel1.AutoScroll = false;
    }

    //adjust picture box location
    if (mSizeMode == PictureBoxSizeMode.Normal)
        pictureboxlocation = new Point(panel1.AutoScrollPosition.X, panel1.AutoScrollPosition.Y);
    else if (mSizeMode == PictureBoxSizeMode.StretchImage)
    {
    
    
        if (mbFitToWindow)
        {
    
    
            if (ratioW > ratioH)
            {
    
    
                pictureboxlocation.X = 0;
                pictureboxlocation.Y = (int)(panel1.Height / 2) - (int)(this.pictureBox1.Height / 2);
            }
            else
            {
    
    
                pictureboxlocation.X = (int)(panel1.Width / 2) - (int)(this.pictureBox1.Width / 2);
                pictureboxlocation.Y = 0;
            }
        }
        else
            //--1
            pictureboxlocation = new Point(panel1.AutoScrollPosition.X, panel1.AutoScrollPosition.Y);
    }
    else //center image
    {
    
    
        if (bitmap_complete.Width > panel1.Width)
            pictureboxlocation.X = 0;
        else
            pictureboxlocation.X = (int)(panel1.Width / 2) - (int)(bitmap_complete.Width / 2);

        if (bitmap_complete.Height > panel1.Height)
            pictureboxlocation.Y = 0;
        else
            pictureboxlocation.Y = (int)(panel1.Height / 2) - (int)(bitmap_complete.Height / 2);
    }

    //calculate source rectangles
    if (mSizeMode == PictureBoxSizeMode.Normal)
    {
    
    
        src_complete.X = 0;
        src_complete.Y = 0;
        src_complete.Width = bitmap_complete.Width;
        src_complete.Height = bitmap_complete.Height;
        src_image = new Rectangle(0, 0, bitmap_image.Width, bitmap_image.Height);
    }
    else if (mSizeMode == PictureBoxSizeMode.StretchImage)
    {
    
    
        if (mbFitToWindow)
        {
    
    
            src_complete.X = 0;
            src_complete.Y = 0;
            src_complete.Width = bitmap_complete.Width;
            src_complete.Height = bitmap_complete.Height;
            src_image = new Rectangle(0, 0, bitmap_image.Width, bitmap_image.Height);
        }
        else
        {
    
    
            src_complete.X = 0;
            src_complete.Y = 0;
            src_complete.Width = bitmap_complete.Width;
            src_complete.Height = bitmap_complete.Height;
            src_image = new Rectangle(0, 0, bitmap_image.Width, bitmap_image.Height);
        }
    }
    else
    {
    
    
        if (bitmap_complete.Width > panel1.Width)
            src_complete.X = (int)(bitmap_complete.Width / 2) - (int)(panel1.Width / 2);
        else
            src_complete.X = 0;
        src_complete.Width = this.panel1.Width;

        if (bitmap_complete.Height > panel1.Height)
            src_complete.Y = (int)(bitmap_complete.Height / 2) - (int)(panel1.Height / 2);
        else
            src_complete.Y = 0;
        src_complete.Height = this.pictureBox1.Height;
        src_image = src_complete;
        src_image.Intersect(bitmap_image);
        src_image.X -= bitmap_image.X;
        src_image.Y -= bitmap_image.Y;
    }

    //calculate destination rectangles
    if (mSizeMode == PictureBoxSizeMode.Normal)
    {
    
    
        dest_complete.X = 0;
        dest_complete.Y = 0;
        dest_complete.Width = bitmap_complete.Width;
        dest_complete.Height = bitmap_complete.Height;
        dest_image = bitmap_image;
    }
    else if (mSizeMode == PictureBoxSizeMode.StretchImage)
    {
    
    
        if (mbFitToWindow)
        {
    
    
            dest_complete.X = 0;
            dest_complete.Y = 0;
            dest_complete.Width = this.pictureBox1.Width;
            dest_complete.Height = this.pictureBox1.Height;

            dest_image.X = this.pictureBox1.Width * bitmap_image.Left / src_complete.Width;
            dest_image.Y = this.pictureBox1.Height * bitmap_image.Top / src_complete.Height;
            dest_image.Width = this.pictureBox1.Width * bitmap_image.Width / src_complete.Width;
            dest_image.Height = this.pictureBox1.Height * bitmap_image.Height / src_complete.Height;
        }
        else
        {
    
    
            dest_complete.X = 0;
            dest_complete.Y = 0;
            dest_complete.Width = this.pictureBox1.Width;
            dest_complete.Height = this.pictureBox1.Height;

            dest_image.X = (int)(bitmap_image.X * ZOOMFACTOR);
            dest_image.Y = (int)(bitmap_image.Y * ZOOMFACTOR);
            dest_image.Width = (int)(bitmap_image.Width * ZOOMFACTOR);
            dest_image.Height = (int)(bitmap_image.Height * ZOOMFACTOR);
        }
    }
    else
    {
    
    
        dest_complete.X = 0;
        dest_complete.Y = 0;
        dest_complete.Width = this.pictureBox1.Width;
        dest_complete.Height = this.pictureBox1.Height;
        dest_image = src_complete;
        dest_image.Intersect(bitmap_image);
        dest_image.Offset(src_complete.X * (-1), src_complete.Y * (-1));
    }

    this.pictureBox1.Location = pictureboxlocation;
    try
    {
    
    
        if (mShowImage)
        {
    
    
            e.Graphics.DrawImage(bmptodraw,
                        dest_image,
                        src_image.Left,
                        src_image.Top,
                        src_image.Width,
                        src_image.Height,
                        GraphicsUnit.Pixel);
        }
    }
    catch
    {
    
    
        System.Windows.Forms.MessageBox.Show("Exception while refresh image window");
    }
}
private void DrawingTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    
    
    lock (mImageData)
    {
    
    
        if (mbNewImageAvailable)//1
        {
    
    
            if (mbNewImageReady == false)//2
            {
    
    
                if (mbDrawFirstBitmap == true)//3
                {
    
    
                    if (mImageData[1].bgapiImage != null)//4
                    {
    
    
                        lock (mCamera.BackImagesColl)
                            mCamera.BackImagesColl.Add(mImageData[1].bgapiImage);
                    }
                    mImageData[1].bgapiImage = mNewBgapiImage;
                    CreateBitmap(ref mImageData[1].bgapiImage, ref mImageData[1].bmp, BGAPI.BGAPI_ChunkId.BGAPI_CHUNKID_IMAGE);
                          
                }
                else//3 --mbDrawFirstBitmap == false
                {
    
    
                    if (mImageData[0].bgapiImage != null)//--4
                    {
    
    
                        //transformation preset
                        lock (mCamera.BackImagesColl)
                            mCamera.BackImagesColl.Add(mImageData[0].bgapiImage);
                    }
                    mImageData[0].bgapiImage = mNewBgapiImage;
                    CreateBitmap(ref mImageData[0].bgapiImage, ref mImageData[0].bmp, BGAPI.BGAPI_ChunkId.BGAPI_CHUNKID_IMAGE);
                            
                }
                mbNewImageAvailable = false;
                mbNewImageReady = true;
                pictureBox1.Invalidate(pictureBox1.ClientRectangle, false);
            }
        }
    }
}
    

Advantages of Baumer industrial cameras zooming in and out of camera images

Improve image details: With enlarged images, you can see more details, helping you analyze and understand images better.

Convenient capture and processing: Enlarging the image can make it easier to capture the target that needs to be measured or analyzed for subsequent processing, improving work efficiency.

Reduce error rate: When target positioning or measurement is required, zooming in on the image can help you reduce error rate and improve measurement accuracy.

More flexible for equipment: With the continuous improvement of technology and equipment, camera adjustment will become more flexible, and the image size and display ratio can be adjusted according to needs.

Better user experience: Zooming in and out of images can provide a better user experience, making it easier for users to observe image details, and easier to understand and analyze images.

Industrial application of Baumer industrial camera zooming in and zooming out the camera image

Manufacturing: In manufacturing, industrial cameras are commonly used to inspect and measure the size, shape and surface quality of parts. The zoom-in and zoom-out function of industrial cameras can help operators observe images better and improve production efficiency.

Medical industry: In the medical industry, industrial cameras can be used to capture images of bones, blood vessels or other organs. Zooming in and out enables doctors to better observe and analyze these images, and helps them make better diagnoses.

. Food line: In the food industry, industrial cameras can be used to check the quality and safety of food. Zooming in and out allows the operator to better observe food details and check for defects.

Transportation and Security: In transportation and security, industrial cameras can be used to identify vehicles and individuals. Zoom in and zoom out can help identify and track targets more accurately and quickly.

Environmental monitoring: Industrial cameras can be used for environmental monitoring such as weather and pollutants. The zoom in and zoom out function can help detect and analyze environmental problems in the picture, such as the distribution area of ​​pollutants.

Guess you like

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