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

简介: 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);

注意

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


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


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


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


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

目录
相关文章
|
11天前
|
计算机视觉
Opencv学习笔记(三):图像二值化函数cv2.threshold函数详解
这篇文章详细介绍了OpenCV库中的图像二值化函数`cv2.threshold`,包括二值化的概念、常见的阈值类型、函数的参数说明以及通过代码实例展示了如何应用该函数进行图像二值化处理,并展示了运行结果。
107 0
Opencv学习笔记(三):图像二值化函数cv2.threshold函数详解
|
1月前
|
算法 计算机视觉
opencv图像形态学
图像形态学是一种基于数学形态学的图像处理技术,它主要用于分析和修改图像的形状和结构。
39 4
|
29天前
|
存储 计算机视觉
Opencv的基本操作(一)图像的读取显示存储及几何图形的绘制
本文介绍了使用OpenCV进行图像读取、显示和存储的基本操作,以及如何绘制直线、圆形、矩形和文本等几何图形的方法。
Opencv的基本操作(一)图像的读取显示存储及几何图形的绘制
WK
|
2月前
|
编解码 计算机视觉 Python
如何在OpenCV中进行图像转换
在OpenCV中,图像转换涉及颜色空间变换、大小调整及类型转换等操作。常用函数如`cvtColor`可实现BGR到RGB、灰度图或HSV的转换;`resize`则用于调整图像分辨率。此外,通过`astype`或`convertScaleAbs`可改变图像数据类型。对于复杂的几何变换,如仿射或透视变换,则可利用`warpAffine`和`warpPerspective`函数实现。这些技术为图像处理提供了强大的工具。
WK
86 1
|
5月前
|
JavaScript Java Maven
云效产品使用常见问题之android sdk 构建出aar后,上传到私有maven仓库失败如何解决
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
5月前
|
安全 开发工具 Android开发
几个Flutter常见诊断错误与解决Android toolchain - develop for Android devices X Unable to locate Android SDK
几个Flutter常见诊断错误与解决Android toolchain - develop for Android devices X Unable to locate Android SDK
1642 0
|
2月前
|
JavaScript 前端开发 Java
[Android][Framework]系统jar包,sdk的制作及引用
[Android][Framework]系统jar包,sdk的制作及引用
59 0
|
2月前
|
开发工具 Android开发
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
149 4
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
|
2月前
|
Dart 开发工具 Android开发
Android Studio导入Flutter项目提示Dart SDK is not configured
Android Studio导入Flutter项目提示Dart SDK is not configured
207 4
|
2月前
|
开发工具 Android开发
Flutter: Android SDK not found at this location,Android Studio not found at xxx
Flutter: Android SDK not found at this location,Android Studio not found at xxx
140 2