工业相机如何实现实时和本地Raw格式图像和Bitmap格式图像的保存和相互转换(C#代码,UI界面版)

简介: 工业相机如何实现实时和本地Raw格式图像和Bitmap格式图像的保存和相互转换(C#代码,UI界面版)

工业相机图像格式


工业相机RAW文件是一种记录了工业相机传感器的原始信息,同时记录了由相机拍摄所产生的一些原数据(Metadata,如ISO的设置、快门速度、光圈值、白平衡等)的文件。RAW是未经处理、也未经压缩的格式,可以把RAW概念化为“原始图像编码数据”。


工业相机Bitmap图像是一种无损的图像格式,它将图像存储为像素阵列,并可包含调色板信息。这种格式通常用于工业应用中,因为它能够保留图像的细节和质量,并且易于处理和分析。


本文以Baumer工业相机作为案例进行演示,实现将工业相机的图像转换为Raw图像并进行保存到本地,转换Raw图像为Bitmap图像,再从Bitmap图像转换为Raw图像等操作。


工业相机实现Raw图像和Bitmap图像的保存和转换的技术背景


本文通过C#中实现一个简单的UI界面,用于将Raw图像转换为Bitmap图像并进行保存。


用户可以通过该界面执行以下操作:


  1. 转换Raw图像为Bitmap图像:用户可通过指定的操作步骤和可能的参数,将从工业相机获取的Raw图像数据转换为可处理的Bitmap格式。
  2. 转换Bitmap图像为Raw图像:用户有能力将转换后的Bitmap图像转换为Raw图像保存到指定的文件路径,以备后续分析或使用。


通过这个UI界面,用户能够在实时应用机器视觉数据处理时快速有效地进行操作,无需深入了解图像数据的底层处理过程。这个简单的介绍旨在为开发人员提供一个明确的方向,以便开始构建此类应用程序,并且该程序主要用于演示目的。


在相机SDK中获取图像转换图像的代码分析


本文介绍使用Baumer工业相机,实现将图像转换为Raw图像并进行保存到本地,转换Raw图像为Bitmap图像,再从Bitmap图像转换为Raw图像等操作


工业相机回调函数里保存Bitmap图像数据


C#环境下在回调函数里保存Bitmap图像代码如下所示:

void mDataStream_NewBufferEvent(object sender, BGAPI2.Events.NewBufferEventArgs mDSEvent)
{
    try
    {
        BGAPI2.Buffer mBufferFilled = null;              
        mBufferFilled = mDSEvent.BufferObj;
        if (mBufferFilled == null)
        {
            MessageBox.Show("Error: Buffer Timeout after 1000 ms!");
        }
        else if (mBufferFilled.IsIncomplete == true)
        {          
            mBufferFilled.QueueBuffer();
        }
        else
        {
            //将相机内部图像内存数据转为bitmap数据
            System.Drawing.Bitmap bitmap  = new System.Drawing.Bitmap((int)mBufferFilled.Width, (int)mBufferFilled.Height, (int)mBufferFilled.Width,
                System.Drawing.Imaging.PixelFormat.Format8bppIndexed, (IntPtr)((ulong)mBufferFilled.MemPtr + mBufferFilled.ImageOffset));
            #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 (bSaveImg)
            {
                //使用bitmap自带函数保存
                string strtime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                string saveimagepath = pImgFileDir + "\\" + strtime + ".jpg";
                //bitmap.Save(saveimagepath, System.Drawing.Imaging.ImageFormat.Bmp);         
                bSaveImg = false;//变量控制单次保存图像
            }
            #region//bitmap的图像数据复制pBitmap
            Bitmap clonebitmap = (Bitmap)bitmap.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)mBufferFilled.Width;prcSource.Height = (int)mBufferFilled.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;
}
}


工业相机图像转换Bitmap图像格式重要核心代码

//将相机内部图像内存数据转为bitmap数据
System.Drawing.Bitmap bitmap  = new System.Drawing.Bitmap((int)mBufferFilled.Width, (int)mBufferFilled.Height,(int)mBufferFilled.Width,System.Drawing.Imaging.PixelFormat.Format8bppIndexed, (IntPtr)((ulong)mBufferFilled.MemPtr + mBufferFilled.ImageOffset));
#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
string strtime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
string saveimagepath = pImgFileDir + "\\" + strtime + ".brw";
//使用Bitmap格式保存         
bitmap.Save(saveimagepath, System.Drawing.Imaging.ImageFormat.Bmp);  


工业相机回调函数里保存Raw图像数据


C#环境下在回调函数里保存Raw图像代码如下所示:

void mDataStream_NewBufferEvent(object sender, BGAPI2.Events.NewBufferEventArgs mDSEvent)
{
    try
    {
        BGAPI2.Buffer mBufferFilled = null;              
        mBufferFilled = mDSEvent.BufferObj;
        if (mBufferFilled == null)
        {
            MessageBox.Show("Error: Buffer Timeout after 1000 ms!");
        }
        else if (mBufferFilled.IsIncomplete == true)
        {          
            mBufferFilled.QueueBuffer();
        }
        else
        {
             //回调函数保存图像功能
            if (bSaveImg)
            {
                //使用bitmap自带函数保存
                string strtime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                string saveimagepath = pImgFileDir + "\\" + strtime + ".jpg";
                //bitmap.Save(saveimagepath, System.Drawing.Imaging.ImageFormat.Bmp);
                // Raw格式图像名称
                string Rawimagepath = pImgFileDir + "\\" + strtime + ".raw";
                // 原始图像数据保存为Raw格式
                // 获取第一行的地址
                IntPtr ptr0 = (IntPtr)((ulong)mBufferFilled.MemPtr + mBufferFilled.ImageOffset);
                // 计算图像每一行的字节数
                int stride = (int)mBufferFilled.Width; // 在MONO格式中,每个像素只占据一个字节
                // 声明一个数组保存图像的数据
                int bytes0 = Math.Abs(stride) * (int)mBufferFilled.Height;
                // 将图像数据复制到新的数组中
                byte[] rawData = new byte[stride * (int)mBufferFilled.Height];
                System.Runtime.InteropServices.Marshal.Copy(ptr0, rawData, 0, bytes0);                
                // 将数组保存为Raw格式文件
                System.IO.File.WriteAllBytes(Rawimagepath , rawData);
                bSaveImg = false;//变量控制单次保存图像
            }
            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;
}
}


工业相机图像转换Raw图像格式重要核心代码

// Raw格式图像名称
string strtime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
string Rawimagepath = pImgFileDir + "\\" + strtime + ".raw";
// 原始图像数据保存为Raw格式
// 获取第一行的地址
IntPtr ptr0 = (IntPtr)((ulong)mBufferFilled.MemPtr + mBufferFilled.ImageOffset);
// 计算图像每一行的字节数
int stride = (int)mBufferFilled.Width; // 在MONO格式中,每个像素只占据一个字节
// 声明一个数组保存图像的数据
int bytes0 = Math.Abs(stride) * (int)mBufferFilled.Height;
// 将图像数据复制到新的数组中
byte[] rawData = new byte[stride * (int)mBufferFilled.Height];
System.Runtime.InteropServices.Marshal.Copy(ptr0, rawData, 0, bytes0);
// 将数组保存为Raw格式文件
System.IO.File.WriteAllBytes(Rawimagepath , rawData);


代码实现演示(保存Raw图像)



代码实现演示(保存Bmp图像)



代码实现演示(Bitmap图像转换为Raw图像)



代码实现演示(本地Raw图像转换为Bitmap图像)


这里的转换是可以直接从本地载入Raw图像将其转换为Bitmap图像


代码实现演示(本地Bitmap图像转换为Raw图像)


这里的转换是可以直接从本地载入Bitmap图像将其转换为Raw图像


源码下载链接


完整资源下载链接:[基于机器视觉工业相机的Raw图像和Bitmap图像的保存和转换(C#代码,UI界面版)

](https://mbd.pub/o/bread/mbd-ZZiclJ1x)


源码下载链接


若您想获得博文中涉及的实现完整全部程序文件(包括测试图片、视频,UI文件等,如下图),这里已打包上传至博主的面包多平台和CSDN下载资源,具体可见参考文章和参考视频,已将所有涉及的文件同时打包到里面,点击即可运行,完整文件截图如下:


Baumer工业相机通过SDK实现Raw格式的图像保存的行业应用


工业相机通过SDK实现Raw格式的图像保存在许多行业应用中发挥重要作用,包括但不限于:


  1. 检测和测量应用:在制造业中,工业相机通过SDK保存Raw格式的图像可用于精确的检测和测量应用,例如缺陷检测、尺寸测量、外观质量控制等。Raw格式图像的高质量和完整性有助于确保实时检测和测量的准确性。


  1. 医学成像:医疗领域也常常利用工业相机进行医学成像,比如X射线、CT扫描、核磁共振成像等。通过SDK保存Raw格式的图像能够保留更多的图像细节和动态范围,有助于医学图像的后期处理和分析。


  1. 智能交通:在智能交通系统中,工业相机通过SDK保存Raw格式的图像可用于车牌识别、交通监控等应用。Raw格式的图像数据能提供更多细节,有助于提高识别的准确性和可靠性。


  1. 机器视觉:在自动化生产线和机器视觉系统中,工业相机通过SDK保存Raw格式的图像可用于产品检测、识别和定位等应用。Raw格式图像保留了更多的信息,有助于提高机器视觉系统的准确性和稳定性。


总的来说,工业相机通过SDK实现Raw格式的图像保存在需要高质量图像数据、精确测量和复杂分析的行业应用中具有广泛的应用前景。

目录
相关文章
|
1月前
|
Ubuntu Java 测试技术
【Linux】一站式教会:Ubuntu(无UI界面)使用apache-jmeter进行压测
【Linux】一站式教会:Ubuntu(无UI界面)使用apache-jmeter进行压测
|
1月前
|
存储 测试技术 UED
Qt中实现界面回放的艺术:从理论到代码“ (“The Art of Implementing UI Playback in Qt: From Theory to Code
Qt中实现界面回放的艺术:从理论到代码“ (“The Art of Implementing UI Playback in Qt: From Theory to Code
66 1
|
3月前
|
监控 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
48 0
|
12天前
|
数据安全/隐私保护 图形学
基于 LVGL 使用 SquareLine Studio 快速设计 UI 界面
基于 LVGL 使用 SquareLine Studio 快速设计 UI 界面
|
1月前
|
存储 SQL PHP
彩虹外链网盘界面UI美化版超级简洁好看
彩虹外链网盘界面UI美化版超级简洁好看
24 0
|
3月前
|
存储 机器人 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机图像转换为Bitmap图像功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机图像转换为Bitmap图像功能(C#)
23 1
|
1月前
|
C#
24. C# 编程:用户设定敌人初始血值的实现
24. C# 编程:用户设定敌人初始血值的实现
22 0
|
2月前
|
SQL 数据库连接 应用服务中间件
C#WinForm基础编程(三)
C#WinForm基础编程
78 0
|
2月前
C#WinForm基础编程(二)
C#WinForm基础编程
59 0
|
2月前
|
C# 数据安全/隐私保护
C#WinForm基础编程(一)
C#WinForm基础编程
62 0