浅谈.Net GDI+(Graphics Device Interface Plus)Bitmap类的用法以及与OpenCV Mat(C++)和NDarray(Python)以及HALCON的比较

图像处理算法库比较出名的有OpenCV和HALCON ,那么最重要的就是它们以何种方式表示图像以及进行图像的读存和操作等。
OpenCV C++使用的是Mat 类,即矩阵类,OpenCV Python 使用的是NDarray 类,是Python科学计算包Numpy 模块其中之一的一个类。
HALCON 在.NET C# 中使用的是HObject 类,此类是HALCON 三大核心region image xlds 的基类,因此HALCON 使用HObject 来处理图像。
那么,.NET FRAMEWORK 自身有一种类来处理图像吗,就像Python 有一个内置图像处理模块PIL 一样呢?
答案是有的,这就是Bitmap 类,以下是此类在.NET 中的继承层次架构:

System.Object 
   System.MarshalByRefObject
     System.Drawing.Image
       System.Drawing.Bitmap

我们可以在某些相机的.NET程序中找到Bitmap 的影子,没错,很多相机来拟合NET开发时就是使用的是Bitmap 类。
Bitmap 类也可以实现处理图像的很多基本操作,虽然没有专业的OPENCV 和HALCON 强大。
以下是Bitmap 类的构造函数和成员函数:
所有解释来自NET 的注释:

#region 程序集 System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Drawing.dll
#endregion

using System.ComponentModel;
using System.Drawing.Design;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;

namespace System.Drawing
{
    
    
    //
    // 摘要:
    //     封装 GDI+ 包含图形图像和其属性的像素数据的位图。 一个 System.Drawing.Bitmap 是用来处理图像像素数据所定义的对象。
    [ComVisible(true)]
    [Editor("System.Drawing.Design.BitmapEditor, System.Drawing.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
    public sealed class Bitmap : Image
    {
    
    
        //
        // 摘要:
        //     从指定的文件初始化 System.Drawing.Bitmap 类的新实例。
        //
        // 参数:
        //   filename:
        //     位图文件的名称和路径中。
        //
        // 异常:
        //   T:System.IO.FileNotFoundException:
        //     找不到指定的文件。
        public Bitmap(string filename);
        //
        // 摘要:
        //     从指定的数据流初始化 System.Drawing.Bitmap 类的新实例。
        //
        // 参数:
        //   stream:
        //     用于加载图像的数据流。
        //
        // 异常:
        //   T:System.ArgumentException:
        //     stream 不包含图像数据,或者是 null。 - 或 - stream 包含单个维度大于 65535 像素的 PNG 图像文件。
        public Bitmap(Stream stream);
        //
        // 摘要:
        //     新实例初始化 System.Drawing.Bitmap 类从指定的现有图像。
        //
        // 参数:
        //   original:
        //     从中创建新 System.Drawing.Bitmap 的 System.Drawing.Image。
        public Bitmap(Image original);
        //
        // 摘要:
        //     从指定的文件初始化 System.Drawing.Bitmap 类的新实例。
        //
        // 参数:
        //   filename:
        //     位图文件的名称。
        //
        //   useIcm:
        //     true 要用于此颜色校正 System.Drawing.Bitmap; 否则为 false。
        public Bitmap(string filename, bool useIcm);
        //
        // 摘要:
        //     新实例初始化 System.Drawing.Bitmap 类从指定的资源。
        //
        // 参数:
        //   type:
        //     用于提取资源的类。
        //
        //   resource:
        //     资源的名称。
        public Bitmap(Type type, string resource);
        //
        // 摘要:
        //     从指定的数据流初始化 System.Drawing.Bitmap 类的新实例。
        //
        // 参数:
        //   stream:
        //     用于加载图像的数据流。
        //
        //   useIcm:
        //     true 要用于此颜色校正 System.Drawing.Bitmap; 否则为 false。
        //
        // 异常:
        //   T:System.ArgumentException:
        //     stream 不包含图像数据,或者是 null。 - 或 - stream 包含单个维度大于 65535 像素的 PNG 图像文件。
        public Bitmap(Stream stream, bool useIcm);
        //
        // 摘要:
        //     新实例初始化 System.Drawing.Bitmap 类具有指定大小。
        //
        // 参数:
        //   width:
        //     宽度,以像素为单位,新的 System.Drawing.Bitmap。
        //
        //   height:
        //     高度,以像素为单位,新的 System.Drawing.Bitmap。
        //
        // 异常:
        //   T:System.Exception:
        //     操作失败。
        public Bitmap(int width, int height);
        //
        // 摘要:
        //     新实例初始化 System.Drawing.Bitmap 类从指定的现有图像,缩放到指定的大小。
        //
        // 参数:
        //   original:
        //     从中创建新 System.Drawing.Bitmap 的 System.Drawing.Image。
        //
        //   newSize:
        //     System.Drawing.Size 结构,它表示大小的新 System.Drawing.Bitmap。
        //
        // 异常:
        //   T:System.Exception:
        //     操作失败。
        public Bitmap(Image original, Size newSize);
        //
        // 摘要:
        //     新实例初始化 System.Drawing.Bitmap 类,具有指定的大小和格式。
        //
        // 参数:
        //   width:
        //     宽度,以像素为单位,新的 System.Drawing.Bitmap。
        //
        //   height:
        //     高度,以像素为单位,新的 System.Drawing.Bitmap。
        //
        //   format:
        //     新的像素格式 System.Drawing.Bitmap。 这必须指定一个值,开头 Format。
        //
        // 异常:
        //   T:System.ArgumentException:
        //     一个 System.Drawing.Imaging.PixelFormat 未指定值,其名称不以开头 格式。 例如,指定 System.Drawing.Imaging.PixelFormat.Gdi
        //     将导致 System.ArgumentException, ,但 System.Drawing.Imaging.PixelFormat.Format48bppRgb
        //     将不会。
        public Bitmap(int width, int height, PixelFormat format);
        //
        // 摘要:
        //     新实例初始化 System.Drawing.Bitmap 类具有指定大小,并使用指定的解决方法 System.Drawing.Graphics 对象。
        //
        // 参数:
        //   width:
        //     宽度,以像素为单位,新的 System.Drawing.Bitmap。
        //
        //   height:
        //     高度,以像素为单位,新的 System.Drawing.Bitmap。
        //
        //   g:
        //     System.Drawing.Graphics 对象,它指定为新的分辨率 System.Drawing.Bitmap。
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     g 为 null。
        public Bitmap(int width, int height, Graphics g);
        //
        // 摘要:
        //     新实例初始化 System.Drawing.Bitmap 类从指定的现有图像,缩放到指定的大小。
        //
        // 参数:
        //   original:
        //     从中创建新 System.Drawing.Bitmap 的 System.Drawing.Image。
        //
        //   width:
        //     宽度,以像素为单位,新的 System.Drawing.Bitmap。
        //
        //   height:
        //     高度,以像素为单位,新的 System.Drawing.Bitmap。
        //
        // 异常:
        //   T:System.Exception:
        //     操作失败。
        public Bitmap(Image original, int width, int height);
        //
        // 摘要:
        //     新实例初始化 System.Drawing.Bitmap 用指定的大小、 像素格式和像素数据的类。
        //
        // 参数:
        //   width:
        //     宽度,以像素为单位,新的 System.Drawing.Bitmap。
        //
        //   height:
        //     高度,以像素为单位,新的 System.Drawing.Bitmap。
        //
        //   stride:
        //     指定两次一次扫描行的开头之间的字节偏移量的整数。 这通常 (但不是一定) 是像素格式 (例如,16 位 / 像素的 2) 中的字节数乘以位图的宽度。 传递给此参数的值必须是四的倍数...
        //
        //   format:
        //     新的像素格式 System.Drawing.Bitmap。 这必须指定一个值,开头 Format。
        //
        //   scan0:
        //     指向包含像素数据的字节数组的指针。
        //
        // 异常:
        //   T:System.ArgumentException:
        //     一个 System.Drawing.Imaging.PixelFormat 未指定值,其名称不以开头 格式。 例如,指定 System.Drawing.Imaging.PixelFormat.Gdi
        //     将导致 System.ArgumentException, ,但 System.Drawing.Imaging.PixelFormat.Format48bppRgb
        //     将不会。
        public Bitmap(int width, int height, int stride, PixelFormat format, IntPtr scan0);

        //
        // 摘要:
        //     创建 System.Drawing.Bitmap 从图标的 Windows 句柄。
        //
        // 参数:
        //   hicon:
        //     指向一个图标的句柄。
        //
        // 返回结果:
        //     此方法创建的 System.Drawing.Bitmap。
        public static Bitmap FromHicon(IntPtr hicon);
        //
        // 摘要:
        //     创建 System.Drawing.Bitmap 从指定的 Windows 资源。
        //
        // 参数:
        //   hinstance:
        //     包含资源的可执行文件的实例句柄。
        //
        //   bitmapName:
        //     一个字符串,包含资源位图的名称。
        //
        // 返回结果:
        //     此方法创建的 System.Drawing.Bitmap。
        public static Bitmap FromResource(IntPtr hinstance, string bitmapName);
        //
        // 摘要:
        //     创建此分区的副本 System.Drawing.Bitmap 定义与指定 System.Drawing.Imaging.PixelFormat 枚举。
        //
        // 参数:
        //   rect:
        //     定义此部分 System.Drawing.Bitmap 复制。
        //
        //   format:
        //     指定 System.Drawing.Imaging.PixelFormat 枚举为目标 System.Drawing.Bitmap。
        //
        // 返回结果:
        //     此方法创建的 System.Drawing.Bitmap。
        //
        // 异常:
        //   T:System.OutOfMemoryException:
        //     rect 位于源位图边界之外。
        //
        //   T:System.ArgumentException:
        //     高度或宽度 rect 为 0。
        public Bitmap Clone(RectangleF rect, PixelFormat format);
        //
        // 摘要:
        //     创建此分区的副本 System.Drawing.Bitmap 由定义 System.Drawing.Rectangle 结构并使用指定 System.Drawing.Imaging.PixelFormat
        //     枚举。
        //
        // 参数:
        //   rect:
        //     定义此部分 System.Drawing.Bitmap 复制。 坐标是相对于此 System.Drawing.Bitmap。
        //
        //   format:
        //     新的像素格式 System.Drawing.Bitmap。 这必须指定一个值,开头 Format。
        //
        // 返回结果:
        //     此方法创建的新 System.Drawing.Bitmap。
        //
        // 异常:
        //   T:System.OutOfMemoryException:
        //     rect 位于源位图边界之外。
        //
        //   T:System.ArgumentException:
        //     高度或宽度 rect 为 0。 - 或 - 一个 System.Drawing.Imaging.PixelFormat 未指定值,其名称不以开头 格式。
        //     例如,指定 System.Drawing.Imaging.PixelFormat.Gdi 将导致 System.ArgumentException, ,但
        //     System.Drawing.Imaging.PixelFormat.Format48bppRgb 将不会。
        public Bitmap Clone(Rectangle rect, PixelFormat format);
        //
        // 摘要:
        //     创建 GDI 从此位图对象 System.Drawing.Bitmap。
        //
        // 返回结果:
        //     句柄 GDI 此方法创建的位图对象。
        //
        // 异常:
        //   T:System.ArgumentException:
        //     高度或位图的宽度是否大于 System.Int16.MaxValue。
        //
        //   T:System.Exception:
        //     操作失败。
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public IntPtr GetHbitmap();
        //
        // 摘要:
        //     创建 GDI 从此位图对象 System.Drawing.Bitmap。
        //
        // 参数:
        //   background:
        //     一个 System.Drawing.Color 结构,它指定背景色。 如果是完全不透明的位图,则忽略此参数。
        //
        // 返回结果:
        //     句柄 GDI 此方法创建的位图对象。
        //
        // 异常:
        //   T:System.ArgumentException:
        //     高度或位图的宽度是否大于 System.Int16.MaxValue。
        //
        //   T:System.Exception:
        //     操作失败。
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public IntPtr GetHbitmap(Color background);
        //
        // 摘要:
        //     返回一个图标的句柄。
        //
        // 返回结果:
        //     同一个图像作为带图标的 Windows 句柄 System.Drawing.Bitmap。
        //
        // 异常:
        //   T:System.Exception:
        //     操作失败。
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public IntPtr GetHicon();
        //
        // 摘要:
        //     在此获取指定的像素的颜色 System.Drawing.Bitmap。
        //
        // 参数:
        //   x:
        //     要检索的像素 x 坐标。
        //
        //   y:
        //     要检索的像素 y 坐标。
        //
        // 返回结果:
        //     一个 System.Drawing.Color 结构,它表示指定的像素的颜色。
        //
        // 异常:
        //   T:System.ArgumentOutOfRangeException:
        //     x 小于 0,或大于或等于 System.Drawing.Image.Width。 - 或 - y 小于 0,或大于或等于 System.Drawing.Image.Height。
        //
        //   T:System.Exception:
        //     操作失败。
        public Color GetPixel(int x, int y);
        //
        // 摘要:
        //     锁定 System.Drawing.Bitmap 到系统内存。
        //
        // 参数:
        //   rect:
        //     一个 System.Drawing.Rectangle 结构,它指定的部分 System.Drawing.Bitmap 为锁。
        //
        //   flags:
        //     System.Drawing.Imaging.ImageLockMode 枚举,用于为指定的访问级别 (读/写) System.Drawing.Bitmap。
        //
        //   format:
        //     一个 System.Drawing.Imaging.PixelFormat 枚举,用于指定的数据格式 System.Drawing.Bitmap。
        //
        // 返回结果:
        //     一个 System.Drawing.Imaging.BitmapData ,包含有关该锁定操作信息。
        //
        // 异常:
        //   T:System.ArgumentException:
        //     System.Drawing.Imaging.PixelFormat 不是特定的每像素位值。 - 或 - 不正确 System.Drawing.Imaging.PixelFormat
        //     传入的位图。
        //
        //   T:System.Exception:
        //     操作失败。
        public BitmapData LockBits(Rectangle rect, ImageLockMode flags, PixelFormat format);
        //
        // 摘要:
        //     锁定 System.Drawing.Bitmap 到系统内存
        //
        // 参数:
        //   rect:
        //     矩形结构,它指定的部分 System.Drawing.Bitmap 为锁。
        //
        //   flags:
        //     其中一个 System.Drawing.Imaging.ImageLockMode 为指定的访问级别 (读/写) 的值 System.Drawing.Bitmap。
        //
        //   format:
        //     其中一个 System.Drawing.Imaging.PixelFormat 值,该值指定的数据格式 System.Drawing.Bitmap。
        //
        //   bitmapData:
        //     一个 System.Drawing.Imaging.BitmapData ,包含有关该锁定操作信息。
        //
        // 返回结果:
        //     一个 System.Drawing.Imaging.BitmapData ,包含有关该锁定操作信息。
        //
        // 异常:
        //   T:System.ArgumentException:
        //     System.Drawing.Imaging.PixelFormat 值不是特定的每像素位值。 - 或 - 不正确 System.Drawing.Imaging.PixelFormat
        //     传入的位图。
        //
        //   T:System.Exception:
        //     操作失败。
        public BitmapData LockBits(Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData);
        //
        // 摘要:
        //     让 System.Drawing.Bitmap 的默认透明颜色为透明。
        //
        // 异常:
        //   T:System.InvalidOperationException:
        //     System.Drawing.Bitmap 的图像格式为图标格式。
        //
        //   T:System.Exception:
        //     操作失败。
        public void MakeTransparent();
        //
        // 摘要:
        //     使指定的颜色透明此 System.Drawing.Bitmap。
        //
        // 参数:
        //   transparentColor:
        //     System.Drawing.Color 结构,它表示要将设置为透明的颜色。
        //
        // 异常:
        //   T:System.InvalidOperationException:
        //     图像格式的 System.Drawing.Bitmap 是一种图标格式。
        //
        //   T:System.Exception:
        //     操作失败。
        public void MakeTransparent(Color transparentColor);
        //
        // 摘要:
        //     在此 System.Drawing.Bitmap 中设置指定像素的颜色。
        //
        // 参数:
        //   x:
        //     要设置的像素的 x 坐标。
        //
        //   y:
        //     要设置的像素的 y 坐标。
        //
        //   color:
        //     代表分配到指定像素的颜色的 System.Drawing.Color 结构。
        //
        // 异常:
        //   T:System.Exception:
        //     操作失败。
        public void SetPixel(int x, int y, Color color);
        //
        // 摘要:
        //     此设置的分辨率 System.Drawing.Bitmap。
        //
        // 参数:
        //   xDpi:
        //     水平分辨率,以每英寸点数为单位的 System.Drawing.Bitmap。
        //
        //   yDpi:
        //     垂直分辨率,以每英寸点数为单位的 System.Drawing.Bitmap。
        //
        // 异常:
        //   T:System.Exception:
        //     操作失败。
        public void SetResolution(float xDpi, float yDpi);
        //
        // 摘要:
        //     解锁此 System.Drawing.Bitmap 于系统内存。
        //
        // 参数:
        //   bitmapdata:
        //     一个 System.Drawing.Imaging.BitmapData ,它指定该锁定操作有关的信息。
        //
        // 异常:
        //   T:System.Exception:
        //     操作失败。
        public void UnlockBits(BitmapData bitmapdata);
    }
}

以下是Bitmap 类的属性:

公共属性 Flags 获取该 Image 的像素数据的特性标志。 (继承自 Image。)
公共属性 FrameDimensionsList 获取 GUID 的数组,这些 GUID 表示此 Image 中帧的维数。 (继承自 Image。)
公共属性 Height 获取此 Image 的高度(以像素为单位)。 (继承自 Image。)
公共属性 HorizontalResolution 获取此 Image 的水平分辨率(以“像素/英寸”为单位)。 (继承自 Image。)
公共属性 Palette 获取或设置用于此 Image 的调色板。 (继承自 Image。)
公共属性 PhysicalDimension 获取此图像的宽度和高度。 (继承自 Image。)
公共属性 PixelFormat 获取此 Image 的像素格式。 (继承自 Image。)
公共属性 PropertyIdList 获取存储于该 Image 中的属性项的 ID。 (继承自 Image。)
公共属性 PropertyItems 获取存储于该 Image 中的所有属性项(元数据片)。 (继承自 Image。)
公共属性 RawFormat 获取此 Image 的文件格式。 (继承自 Image。)
公共属性 Size 获取此图像的以像素为单位的宽度和高度。 (继承自 Image。)
公共属性 Tag 获取或设置提供有关图像附加数据的对象。 (继承自 Image。)
公共属性 VerticalResolution 获取此 Image 的垂直分辨率(以“像素/英寸”为单位)。 (继承自 Image。)
公共属性 Width 获取此 Image 的宽度(以像素为单位)。 (继承自 Image。)

位图由图形图像及其特性的像素数据组成。 可使用许多标准格式将位图保存到文件中。 GDI+ 支持下列文件格式:BMP、GIF、EXIF、JPG、PNG 和 TIFF。 可以使用 Bitmap 构造函数中的一种来从文件、流和其他源创建图像,然后使用 Save 方法将这些图像保存到流或文件系统中。

下面的代码示例演示了如何使用 GetPixel 和 SetPixel 方法从文件构造新的 Bitmap,为图像重新着色。 它还使用 PixelFormat、Width 和 Height 属性。
此示例旨在用于包含名为 Label1 的 Label、名为 PictureBox1 的 PictureBox 和名为 Button1 的 Button 的 Windows 窗体。 将代码粘贴到该窗体中,并将 Button1_Click 方法与按钮的 Click 事件关联。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BitMap_Net
{
    
    
    
    public partial class Form1 : Form
    {
    
    
        Bitmap image1;
        public Form1()
        {
    
    
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
    
    
        }

        private void button1_Click(object sender, EventArgs e)
        {
    
    
            try
            {
    
    
                // Retrieve the image.
                image1 = new Bitmap(@"C:\Python\OpenCV\Cat\5.jpg", true);
                int x, y;
                // Loop through the images pixels to reset color.
                for (x = 0; x < image1.Width; x++)
                {
    
    
                    for (y = 0; y < image1.Height; y++)
                    {
    
    
                        Color pixelColor = image1.GetPixel(x, y);
                        Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
                        image1.SetPixel(x, y, newColor);
                    }
                }

                // Set the PictureBox to display the image.
                pictureBox1.Image = image1;

                // Display the pixel format in Label1.
                label1.Text = "Pixel format: " + image1.PixelFormat.ToString();

            }
            catch (ArgumentException)
            {
    
    
                MessageBox.Show("There was an error. Check the path to the image file.");
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
    
    

        }
    }
}

以下为程序运行结果:
在这里插入图片描述
关于Bitmap 类,还有很多未被介绍,笔者会及时进行更新。
注:部分引用自:
https://blog.csdn.net/yingzhaom/article/details/7743429

猜你喜欢

转载自blog.csdn.net/m0_47472749/article/details/112973377