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

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

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图像是用于图像处理中很好的研究素材,如下图所示:

程序效果

参考资料

  • Windows Forms: Convert an image into grayscale in C#
  • https://bellard.org/bpg/lena.html
  • http://www.lenna.org/

Windows Forms:在C#中将图像转换成灰度图相关推荐

  1. 彩色BMP转换成灰度图的原理

    图像处理中,大部分的处理方法都需要事先把彩色图转换成灰度图才能进行相关的计算.识别. 彩色图转换灰度图的原理如下: 我们知道彩色位图是由R/G/B三个分量组成,其文件存储格式为 BITMAPFILEH ...

  2. 彩色图批量转换成灰度图、批量格式转换、批量重命名

    参考:http://blog.csdn.net/jjff46/article/details/38948621 代码实现的功能:把图片进行批量转换 (1)彩色图片转换成灰度图 (2)图片进行格式转换 ...

  3. OprenCV学习之路一:将彩色图片转换成灰度图

    //将一张彩色图片转成灰度图:#include<cv.h> #include<cvaux.h> #include<highgui.h> #include<ml ...

  4. Intel Realsense D435 opencv 为什么将color图转换成灰度图后,再与depth图水平堆叠,其结果一片黑色?(数据未map到0-255)

    相关代码 # -*- coding: utf-8 -*- """ @File : obstacle_detection.py @Time : 2019/12/11 10: ...

  5. 利用OpenCV把一幅彩色图像转换成灰度图

    图像灰度化的目的是为了简化矩阵,提高运算速度. 彩色图像中的每个像素颜色由R.G.B三个分量来决定,而每个分量的取值范围都在0-255之间,这样对计算机来说,彩色图像的一个像素点就会有256*256* ...

  6. python opencv cv2.cvtColor()方法(将图像从一种颜色空间转换为另一种颜色空间)(转换成灰度图)

    def cvtColor(src, code, dst=None, dstCn=None): # real signature unknown; restored from __doc__" ...

  7. Javascript图像处理之将彩色图转换成灰度图

    最近看了Justany_WhiteSnow的Javascript图像处理一文,写的非常好,于是就练练手,略做了一些封装: (function () {function imageToGray(iCan ...

  8. opencv:把三通道图转换成灰度图、二值图

    #include <opencv2/core/core.hpp> #include<opencv2/highgui/highgui.hpp> #include<openc ...

  9. 如何将 WORD中将彩色图片变成灰度图 或 黑白图 ?

    文章目录 1 灰度图像与黑白图像的区别 2 彩色图 灰度图 相互变换 2.1 彩色图 变 灰度图 2.2 灰度图 变 彩色图 3 彩色图 黑白图 相互变换 3.1 彩色图 变 黑白图 3.1 黑白图 ...

最新文章

  1. php中如何写js代码提示_PHP 如何编写类似js中alert() 提示框
  2. java 数据库数据写接口_Java读取接口数据并保存到数据库
  3. 可重入锁(递归锁) 互斥锁属性设置
  4. Android selector中的item的顺序
  5. c语言中post协议,c/c++的http协议的get和post方法
  6. 在安卓模拟器中,adb安装apk常见错误
  7. 使用saltstack编译安装nginx
  8. 线程池很难么?带你从头到尾捋一遍,不信你听不懂!
  9. Bean获取Spring容器
  10. mysql 双1设置_2020-10-15:mysql的双1设置是什么?
  11. 转载《港股基础知识大全》
  12. 如何基于TwinCAT3实现伺服电机控制(一)
  13. 数字类型转换以及函数全介绍
  14. 商场触摸互动广告机有哪些功能
  15. webotAI网页版上线啦!
  16. 【Android Gradle 插件】build.gradle 中的 android 配置 ( 配置项 | compileSdkVersion 配置 | buildToolsVersion 配置 )
  17. 乌镇互联网大会部分嘉宾分享要点实录
  18. Vue2.0的页面模板
  19. 2021年安全生产模拟考试(建筑安全员B证-项目负责人模拟考试题库)安考星
  20. Java同步之synchronized

热门文章

  1. 自从来了个印度大老板
  2. 出口乌干达符合性评定PVOC认证
  3. 投了Hindawi下的两个SCI的特刊,不是说特刊挺快的嘛?
  4. 图像深度估计的一些基本概念
  5. 复旦计算机系本硕连读几年,总算晓得比较好的本硕连读的大学及专业
  6. 【垃圾回收】喂,GC你晓得伐?
  7. javascript中的!~是什么意思
  8. springboot的REST风格
  9. Just Trick!
  10. 射频识别(RFID)技术原理