Windows Forms:在C#中将图像转换成灰度图

Windows Forms:在C#中将图像转换成灰度图

本文翻译自Windows Forms: Convert an image into grayscale in C#
这篇文章向你展示在C# Windows窗体应用程序中如何将图像转换成灰度图。
创建一个新的Windows窗体应用程序项目,然后创建一个允许你可以打开图像,然后将图像转换成黑白图像的简单的UI,如下图所示:

Convert Image

Open按钮添加单击事件处理,允许你选择一个图像文件,然后将图像显示到PictureBox控件中。
对应的代码如下所示:

private void btnOpen_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDlg = new OpenFileDialog() { Filter = "Images|*.jpg" })
            {
                if (openFileDlg.ShowDialog() == DialogResult.OK)
                {
                    picOriginal.Image = Image.FromFile(openFileDlg.FileName);
                }
            }
        }

下一步,创建一个MakeGrayscale方法允许你在C#中将图像转换成灰度图如下:

//  convert an image into grayscale in c#
        public Bitmap MakeGrayscale(Bitmap original)
        {
            // You need to create a new bitmap with size the same as image original, 
            // then create a color matrix and convert a color image to grayscale with C#.
            Bitmap newBmp = new Bitmap(original.Width, original.Height);
            Graphics g = Graphics.FromImage(newBmp);
            ColorMatrix colorMatrix = new ColorMatrix(
               new float[][]
               {
                   new float[] {.3f, .3f, .3f, 0, 0},
                   new float[] {.59f, .59f, .59f, 0, 0},
                   new float[] {.11f, .11f, .11f, 0, 0},
                   new float[] {0, 0, 0, 1, 0},
                   new float[] {0, 0, 0, 0, 1}
               });
            ImageAttributes img = new ImageAttributes();
            img.SetColorMatrix(colorMatrix);
            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, img);
            g.Dispose();
            return newBmp;
        }

你需要创建一个和原图像一样大小的位图,然后创建一个颜色矩阵,并在C#中将彩色图转换成灰度图。
最后,为Convert按钮添加事件处理,允许你将图像转换成灰度图:

private void btnConvert_Click(object sender, EventArgs e)
        {
            picConvert.Image = MakeGrayscale((Bitmap)picOriginal.Image);
        }

下面是窗体的完整实现代码:

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

namespace ConvertImageIntoGrayscale
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //  convert an image into grayscale in c#
        public Bitmap MakeGrayscale(Bitmap original)
        {
            // You need to create a new bitmap with size the same as image original, 
            // then create a color matrix and convert a color image to grayscale with C#.
            Bitmap newBmp = new Bitmap(original.Width, original.Height);
            Graphics g = Graphics.FromImage(newBmp);
            ColorMatrix colorMatrix = new ColorMatrix(
               new float[][]
               {
                   new float[] {.3f, .3f, .3f, 0, 0},
                   new float[] {.59f, .59f, .59f, 0, 0},
                   new float[] {.11f, .11f, .11f, 0, 0},
                   new float[] {0, 0, 0, 1, 0},
                   new float[] {0, 0, 0, 0, 1}
               });
            ImageAttributes img = new ImageAttributes();
            img.SetColorMatrix(colorMatrix);
            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, img);
            g.Dispose();
            return newBmp;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDlg = new OpenFileDialog() { Filter = "Images|*.jpg" })
            {
                if (openFileDlg.ShowDialog() == DialogResult.OK)
                {
                    picOriginal.Image = Image.FromFile(openFileDlg.FileName);
                }
            }
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            picConvert.Image = MakeGrayscale((Bitmap)picOriginal.Image);
        }
    }
}

lena图像素材下载

图像处理中经常使用到的Lena图片 下载地址:
https://bellard.org/bpg/lena.html

http://www.lenna.org/

Lena的图片

这个用于图像处理的Lena图像是用于图像处理中很好的研究素材,如下图所示:
http://www.lenna.org/

程序效果

程序效果展示

参考资料

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容