Baumer工业相机堡盟万兆网相机如何在SDK中使用ColorProcessing颜色处理功能(C#)

简介: Baumer工业相机堡盟万兆网相机如何在SDK中使用ColorProcessing颜色处理功能(C#)

项目场景

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


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


Baumer工业相机的颜色处理功能可以在BGAPI SDK中通过Baumer提供的API函数完成。


技术背景

Baumer工业相机具有先进的色彩处理能力,使其能够捕获具有准确和生动色彩的图像。这是通过各种色彩处理技术实现的,如色彩插值、白平衡调整、色差校正和色彩增强。


颜色插值涉及在图像中创建缺失的颜色信息,根据周围的像素来估计应该有哪些颜色。白平衡调整对场景的整体色彩平衡进行调整,以确保白色表面在图像中呈现白色。


色差校正用于纠正镜头或图像传感器可能带来的色彩失真,而色彩增强可用于提高图像的色彩饱和度或对比度。


总的来说,工业相机能够产生高质量的图像,具有准确和一致的色彩,使它们成为广泛的工业和科学应用的理想选择。


代码分析

Baumer工业相机堡盟相机SDK示例中015_ColorProcessing.cpp详细介绍了如何配置在相机内部配置颜色处理功能。


软件SDK示例地址如下所示:Baumer_GAPI_SDK_2.12.0_win_x86_64_cpp\examples\src\0_Common\015_ColorProcessing\015_ColorProcessing.cpp

1.png

如下为使用Baumer BGAPI SDK进行相机颜色处理矩阵的核心代码:


SystemList 
Open a System 
Get the InterfaceList and fill it Open an Interface 
Get the DeviceList and fill it 
Open a Device
//获取颜色转换处理功能
BGAPI2.Node colorTransformationFactoryListSelector = GetFeature(deviceRemoteMap, "ColorTransformationFactoryListSelector");
if (colorTransformationFactoryListSelector != null)
{
    BGAPI2.NodeMap colorTransformationMap = colorTransformationFactoryListSelector.EnumNodeList;
    ulong iMatrixCount = colorTransformationMap.Count;
    List<string> colorMatrixSet = new List<string>();
    for (ulong iMatrixIndex = 0; iMatrixIndex < iMatrixCount; iMatrixIndex++) {
        BGAPI2.Node colorMatrix = colorTransformationMap[iMatrixIndex];
        if (colorMatrix.IsAvailable && colorMatrix.IsImplemented)
        {
            colorMatrixSet.Add(colorMatrix.Value);          
        }
    }
    BGAPI2.Node colorTransformationResetToFactoryList = GetFeature(deviceRemoteMap, "ColorTransformationResetToFactoryList");
    if (colorTransformationResetToFactoryList != null)
    {       
        int iMatrixIndex = 0;
        Int32.TryParse(Console.ReadLine(), out iMatrixIndex);
        iMatrixIndex--;    
        if ((iMatrixIndex >= 0) && (iMatrixIndex < colorMatrixSet.Count))
        {
            colorTransformationFactoryListSelector.Value = colorMatrixSet[iMatrixIndex];
            colorTransformationResetToFactoryList.Execute();
        }
    }
}
double[] dColorMatrix = new double[9];
string sPixelFormatSrc = "";
string sPixelFormatDst = "";
BGAPI2.Node devicePixelFormat = null;
BGAPI2.ImageProcessor imageProcessor = null;
//显示相机颜色矩阵
if (GetDeviceColorMatrix(mDevice, dColorMatrix))
{
    try
    {
        devicePixelFormat = mDevice.RemoteNodeList["PixelFormat"];
        BGAPI2.NodeMap nodeMap = devicePixelFormat.EnumNodeList;
        ulong count = nodeMap.Count;
        for (ulong i = 0; i < count; i++) {
            BGAPI2.Node node = nodeMap[i];
            if (node.IsImplemented && node.IsAvailable) {
                string sPixelFormat = node.Value;               
                if (sPixelFormatSrc == "")
                {
                    if (sPixelFormat.IndexOf("Bayer") >= 0)
                    {
                        sPixelFormatSrc = sPixelFormat;
                        sPixelFormatDst = "BGR8";
                    }
                }
            }
        }
    }
    catch (BGAPI2.Exceptions.IException ex)
    {
        returncode = 0 == returncode ? 1 : returncode;       
    }
}
//确认相机是否具有颜色处理功能
if ((sPixelFormatSrc == "") || (sPixelFormatDst == ""))
{
    System.Console.Write("NO COLOR MATRIX SUPPORT\r\n\r\n");
    return;
}
if ((sPixelFormatSrc != "") & (sPixelFormatDst != ""))
{
     datastreamList = mDevice.DataStreams;
     datastreamList.Refresh();
     foreach (KeyValuePair<string, BGAPI2.DataStream> dst_pair in datastreamList)
     {                    
         dst_pair.Value.Open();
         DataStreamID = dst_pair.Key;
         break;
    }
}
mDataStream = datastreamList[sDataStreamID];
//BufferList
bufferList = mDataStream.BufferList;
// 4 buffers using internal buffer mode
BGAPI2.Buffer buffer = null;
for (int i = 0; i < 4; i++)
{
     buffer = new BGAPI2.Buffer();
     bufferList.Add(buffer);
}
foreach (KeyValuePair<string, BGAPI2.Buffer> buf_pair in bufferList)
{
    buf_pair.Value.QueueBuffer();
}
//开始使用相机的颜色处理矩阵功能
imageProcessor = new BGAPI2.ImageProcessor();
if (imageProcessor != null) {
   // SET COLOR MATRIX TO IMAGE PROCESSOR
   System.Console.Write(" Set color matrix to image processor\r\n");
   SetImageProcessorColorMatrix(imageProcessor, dColorMatrix);
}

上面部分获取相机属性代码的函数如下所示:

//------------------------------------------------------------------------------
static BGAPI2.Node GetFeature(BGAPI2.NodeMap nodeMap, string feature)
{
    if (nodeMap != null)
    {
        try
        {
            if (nodeMap.GetNodePresent(feature))
            {
                BGAPI2.Node node = nodeMap[feature];
                if ((node != null) && node.IsImplemented && node.IsAvailable)
                {
                    return node;
                }
            }
        }
        catch (BGAPI2.Exceptions.IException ex)
        {
            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());
        }
    }
    return null;
}
//------------------------------------------------------------------------------
// Readout device color matrix
static bool GetDeviceColorMatrix(BGAPI2.Device device, double[] dColorMatrix)
{
    uint uMask = 0;
    try
    {
        BGAPI2.NodeMap nodeMap = device.RemoteNodeList;
        BGAPI2.Node colorTransformationValue         = GetFeature(nodeMap, "ColorTransformationValue");
        BGAPI2.Node colorTransformationValueSelector = GetFeature(nodeMap, "ColorTransformationValueSelector");
        if ((colorTransformationValueSelector != null) && (colorTransformationValue != null)) {
            BGAPI2.NodeMap colorTransformationNodeMap = colorTransformationValueSelector.EnumNodeList;
            ulong iSelectorCount = colorTransformationNodeMap.Count;
            string sOldSelector = colorTransformationValueSelector.Value;
            string sSelector = sOldSelector;
            for (ulong iSelectorIndex = 0; iSelectorIndex<iSelectorCount; iSelectorIndex++)
            {
                BGAPI2.Node node = colorTransformationNodeMap[iSelectorIndex];
                if (node.IsImplemented && node.IsAvailable)
                {
                    string sGainItemValue = node.Value;
                    int iIndex = -1;
                    if (sGainItemValue == "Gain00")
                    {
                        iIndex = 0;
                    }
                    else if (sGainItemValue == "Gain01")
                    {
                        iIndex = 1;
                    }
                    else if (sGainItemValue == "Gain02")
                    {
                        iIndex = 2;
                    }
                    else if (sGainItemValue == "Gain10")
                    {
                        iIndex = 3;
                    }
                    else if (sGainItemValue == "Gain11")
                    {
                        iIndex = 4;
                    }
                    else if (sGainItemValue == "Gain12")
                    {
                        iIndex = 5;
                    }
                    else if (sGainItemValue == "Gain20")
                    {
                        iIndex = 6;
                    }
                    else if (sGainItemValue == "Gain21")
                    {
                        iIndex = 7;
                    }
                    else if (sGainItemValue == "Gain22")
                    {
                        iIndex = 8;
                    }
                    if (iIndex >= 0)
                    {
                        sSelector = sGainItemValue;
                        colorTransformationValueSelector.Value = sSelector;
                        dColorMatrix[iIndex] = colorTransformationValue.Value;
                        uMask |= (uint)(1 << iIndex);
                    }
                }
            }
            if (sSelector != sOldSelector)
            {
                colorTransformationValueSelector.Value = sOldSelector;
            }
        }
    }
    catch (BGAPI2.Exceptions.IException ex)
    {
        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());
    }
    return (uMask == 0x1FF);
}
//------------------------------------------------------------------------------
static bool SetImageProcessorColorMatrix(BGAPI2.ImageProcessor imageProcessor, double[] dColorMatrix)
{
    uint uMask = 0;
    try
    {
        BGAPI2.NodeMap nodeMap = imageProcessor.NodeList;
        BGAPI2.Node colorTransformationValueSelector = nodeMap["ColorTransformationValueSelector"];
        BGAPI2.Node colorTransformationValue = nodeMap["ColorTransformationValue"];
        for (int iIndex = 0; iIndex < 9; iIndex++)
        {
            colorTransformationValueSelector.Value = iIndex;
            colorTransformationValue.Value = dColorMatrix[iIndex];
            uMask |= (uint)(1 << iIndex);
        }
    }
    catch (BGAPI2.Exceptions.IException ex)
    {
            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());
    }
    return (uMask == 0x1FF);
}

颜色处理功能优点

工业相机的色彩处理功能有几个优点,包括:


1. 提高准确性和一致性: 通过准确捕捉色彩信息,工业相机可以更准确地呈现被检查对象。这有助于提高产品质量和制造过程的一致性。


2. 更快的图像处理: 使用彩色图像可以使机器视觉应用的图像处理更快、更有效,这有助于提高制造业的生产力。


3. 加强图像分析: 彩色图像提供额外的信息,可用于更复杂的图像分析技术,如模式识别和机器学习。


4. 更好的质量控制: 通过监测产品的颜色变化,制造商可以更好地确保产品符合质量标准,并与既定规格一致。


总的来说,工业相机的色彩处理功能可以帮助改善生产效率,提高产品质量,并能对一系列工业应用的图像进行更精确的分析。


颜色处理行业应用

有不同的场景可以利用工业相机的色彩处理功能。


1. 质量控制和检查: 在制造和生产行业,工业相机的颜色处理功能可用于检查和评估产品的颜色质量。它可以用来检测可能影响最终产品的质量的颜色的不一致、缺陷或偏差。


2. 医学和生命科学: 在医学和生命科学的应用中,工业相机的色彩处理可以用来捕捉生物标本、染色或液体的图像,这些图像显示了关于样品的不同类型的信息,如细胞类型、结构和颜色。


3. 交通监控: 在交通监控应用中,工业相机可用于检测和识别基于颜色的交通标志或信号,如红色表示停止,绿色表示前进,黄色表示警告。


4.安全和监控: 工业相机的颜色处理功能可用于安全和监控系统,根据颜色来识别和确认个人或物体,如区分移动物体和背景颜色。


5. 农业和食品工业: 在农业和食品行业,工业相机的色彩处理功能可用于根据颜色对农产品进行分级和分类,如成熟度、成熟度和质量。

目录
相关文章
|
监控 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
567 0
|
监控 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C++)
422 0
|
数据采集 开发工具 Python
海康威视工业相机SDK+Python+PyQt开发数据采集系统(支持软件触发、编码器触发)
该系统基于海康威视工业相机SDK,使用Python与PyQt开发,支持Gige与USB相机设备的搜索及双相机同时显示。系统提供软件触发与编码器触发模式,并可在数据采集过程中实时保存图像。此外,用户可以调节曝光时间和增益,并进行信息输入,这些信息将被保存至配置文件以便下次自动加载。参数调节与实时预览等功能进一步增强了系统的实用性。
2583 1
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。
767 3
|
XML 前端开发 C#
C#编程实践:解析HTML文档并执行元素匹配
通过上述步骤,可以在C#中有效地解析HTML文档并执行元素匹配。HtmlAgilityPack提供了一个强大而灵活的工具集,可以处理各种HTML解析任务。
618 19
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
576 3
|
监控 算法 C#
C#与Halcon联合编程实现鼠标控制图像缩放、拖动及ROI绘制
C#与Halcon联合编程实现鼠标控制图像缩放、拖动及ROI绘制
2791 0
|
存储 安全 编译器
学懂C#编程:属性(Property)的概念定义及使用详解
通过深入理解和使用C#的属性,可以编写更清晰、简洁和高效的代码,为开发高质量的应用程序奠定基础。
1718 12
|
设计模式 C# 图形学
Unity 游戏引擎 C# 编程:一分钟浅谈
本文介绍了在 Unity 游戏开发中使用 C# 的基础知识和常见问题。从 `MonoBehavior` 类的基础用法,到变量和属性的管理,再到空引用异常、资源管理和性能优化等常见问题的解决方法。文章还探讨了单例模式、事件系统和数据持久化等高级话题,旨在帮助开发者避免常见错误,提升游戏开发效率。
907 4
|
安全 程序员 编译器
C#一分钟浅谈:泛型编程基础
在现代软件开发中,泛型编程是一项关键技能,它使开发者能够编写类型安全且可重用的代码。C# 自 2.0 版本起支持泛型编程,本文将从基础概念入手,逐步深入探讨 C# 中的泛型,并通过具体实例帮助理解常见问题及其解决方法。泛型通过类型参数替代具体类型,提高了代码复用性和类型安全性,减少了运行时性能开销。文章详细介绍了如何定义泛型类和方法,并讨论了常见的易错点及解决方案,帮助读者更好地掌握这一技术。
548 11