Baumer工业相机堡盟相机BGAPI SDK联合OpenCV进行Mat图像转换(C#)

项目场景

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

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

Baumer工业相机堡盟相机传统开发包BGAPI SDK进行工业视觉软件整合时,常常需要将SDK中采集的图像数据转换为适合图像格式如Bitmap等,再进行图像处理从而开启图像处理任务;

Baumer工业相机堡盟相机的SDK目前有两种类型:BGAPI SDK和NEOAPI SDK ;

目前BGAPI SDK使用的比较多,这里主要涉及BGAPI SDK相关的图像转换知识;

本文承接上文C++模式的Mat转换:

Baumer工业相机堡盟相机BGAPI SDK联合OpenCV进行Mat图像转换(C++)_格林威的博客-CSDN博客


问题描述

Baumer工业相机BGAPI SDK中图像数据为一般数据格式,C#一般经常转换为BGAPI2.Image格式,下图代码提供的图像数据转换为BGAPI2.Image的数据格式。

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


这里的ImageProcessor是SDK自带的图像转换函数,若需要转换为Mat数据,又需要进行一次BGAPI2.Image格式转为Mat格式,显得较为繁琐,下面则是通过上面的pTranImage 再进行转换为Bitmap的代码

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

string PixelFormatstr = mBufferFilled.PixelFormat;
if (PixelFormatstr.Contains("Mono8"))
{
    if (actform.bFirstFrame)
    {
        actform.pImgBits = new Byte[w * h];
        //actform.pBitmap = new Bitmap(w,h,PixelFormat.Format8bppIndexed);
        //pBitmap = new System.Drawing.Bitmap(w,h,System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
        // actform.pBitmap = new System.Drawing.Bitmap(w,h,System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
        actform.pBitmap = new System.Drawing.Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
        actform.prcSource.X = 0;
        actform.prcSource.Y = 0;
        actform.prcSource.Width = w;
        actform.prcSource.Height = h;
        actform.bFirstFrame = false;
    }
}

System.Drawing.Imaging.BitmapData bmpdata;
System.Drawing.Imaging.BitmapData bmpdata2;
if (PixelFormatstr.Contains("Mono8"))
{
    System.Drawing.Imaging.ColorPalette palette = actform.pBitmap.Palette;
    for (int i = 0; i < 256; i++)
    {
        palette.Entries[i] = Color.FromArgb(255, i, i, i);
    }
    actform.pBitmap.Palette = palette;
}



if (PixelFormatstr.Contains("Mono8"))
    bmpdata = actform.pBitmap.LockBits(actform.prcSource,         
System.Drawing.Imaging.ImageLockMode.WriteOnly,             
System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
else
    bmpdata = actform.pBitmap.LockBits(actform.prcSource, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

if (PixelFormatstr.Contains("Mono8"))
{
    System.Runtime.InteropServices.Marshal.Copy(imagebuffer, actform.pImgBits, 0, w * h);
    System.Runtime.InteropServices.Marshal.Copy(actform.pImgBits, 0, bmpdata.Scan0, w * h);
}
else
{
    System.Runtime.InteropServices.Marshal.Copy(imagebuffer, actform.pImgBits, 0, w * h * 3);
    System.Runtime.InteropServices.Marshal.Copy(actform.pImgBits, 0, bmpdata.Scan0, w * h * 3);
}

actform.pBitmap.UnlockBits(bmpdata);

.


解决方案

下面提供Baumer工业相机BGAPI SDK的相机数据直接转为Mat数据的c#代码

这里提供了相机SDK中原始的图像数据直接转为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));
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;

Bitmap clone = (Bitmap)bitmap.Clone();
BitmapData data = clone.LockBits(new Rectangle(0, 0, clone.Width, clone.Height), ImageLockMode.ReadOnly, clone.PixelFormat);
clone.UnlockBits(data);

注意

为什么需要对图像数据进行快速转换?

工业相机中图像原始数据的转换速度至关重要,因为它决定了相机捕捉和处理图像的速度。

在工厂自动化、基于视觉的检查和监控等行业中,实时图像捕捉和处理是必不可少的。

高转换速度确保摄像机能够快速捕捉图像,并将原始数据实时转换为可显示的格式。

这种速度在高吞吐量的应用中至关重要,在这种应用中,即使是图像转换的微小延迟也会影响整个系统的效率。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
回答: 联合OpenCV是指在C#中使用OpenCV进行图像处理和计算机视觉任务。在C#中使用OpenCV,可以通过调用OpenCV的函数和方法来实现各种图像处理操作,例如图像的读取、修改尺寸、水平和垂直拼接等。在引用中的代码示例中,使用了OpenCV的Cv2命名空间下的函数来读取和处理图像,包括修改图像尺寸、水平和垂直拼接等操作。通过调用这些函数,可以在C#中实现对图像联合处理。在引用和引用中也提供了其他使用OpenCV进行图像处理的代码示例。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C#联合OpenCV进行图像拼接](https://blog.csdn.net/weixin_43852823/article/details/127258175)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Baumer工业相机堡盟相机BGAPI SDK联合OpenCV进行Mat图像转换C#)](https://blog.csdn.net/xianzuzhicai/article/details/129075698)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [OpencvC#.Net版本开发,并访问像素的方法](https://blog.csdn.net/qq_24629901/article/details/77828769)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

格林威

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值