如何使用 AForge 过滤特定颜色

Posted

技术标签:

【中文标题】如何使用 AForge 过滤特定颜色【英文标题】:How to filter a specific colour with AForge 【发布时间】:2021-05-24 15:07:53 【问题描述】:

我正在使用库 AForge 并尝试将所有绿色和黄色像素更改为白色。但是红色像素应该保持不变。我尝试了一些东西,但不幸的是没有得到它。 我使用 EuclideanColorFiltering 类来过滤位图对象,但是我选择了 CenterColor 和 Radius,我没有达到我的目标。 有人可以解释一下这种过滤是如何工作的吗?

【问题讨论】:

【参考方案1】:

有getpixel和setpixel方式。 但为了快速处理,您应该使用 lockbit 并在指针中获取 Scan0

在 lockbit 之后,您可以访问所有位图字节和颜色 例如,您可以像这样从位图中读取红色像素

    IntPtr Iptr = bitmapData.Scan0;
    for(int i = 0 ; i<frame.Height; i++)
    
       for(int j = 0 ; j < frame.Width ; j++)
       
         int index = ((i * frame.Width) + j) * 
                    (Bitmap.GetPixelFormatSize(frame.PixelFormat)/8);
         byte red = Marshal.ReadByte(Iptr + index + 2 ) ; 
         byte green= Marshal.ReadByte(Iptr + index +1);
         byte blue= Marshal.ReadByte(Iptr + index );
          // Also after get the colors you can Use marshal.writebyte to set the pixel your own color
         
        

【讨论】:

以上是关于如何使用 AForge 过滤特定颜色的主要内容,如果未能解决你的问题,请参考以下文章

C# AForge的简单使用

如何使用 Aforge.Net 计算 blob 中连接组件的数量

如何在另一个图像中找到模板图像? Kinect和AForge首选

如何仅使用 CSS 过滤器将黑色转换为任何给定的颜色

悬停在链接上时如何反转整个页面的颜色

如何在将在 RemoteView 中使用的可绘制对象上设置颜色过滤器?