c#实现图片二值化例子(黑白效果)

简介: C#将图片2值化示例代码,原图及二值化后的图片如下: 原图: 二值化后的图像: 实现代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2...

C#将图片2值化示例代码,原图及二值化后的图片如下:

原图:

二值化后的图像:

实现代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Drawing;
namespace BMP2Grey
{
   class Program
   {
     static void ToGrey(Bitmap img1)
     {
       for ( int i = 0; i < img1.Width; i++)
       {
         for ( int j = 0; j < img1.Height; j++)
         {
           Color pixelColor = img1.GetPixel(i, j);
           //计算灰度值
           int grey = ( int )(0.299 * pixelColor.R + 0.587 * pixelColor.G + 0.114 * pixelColor.B);
           Color newColor = Color.FromArgb(grey, grey, grey);
           img1.SetPixel(i, j, newColor);
         }
       }
     }
     static void Thresholding(Bitmap img1)
     {
       int [] histogram = new int [256];
       int minGrayValue=255, maxGrayValue=0;
       //求取直方图
       for ( int i = 0; i < img1.Width; i++)
       {
         for ( int j = 0; j < img1.Height; j++)
         {
           Color pixelColor = img1.GetPixel(i, j);
           histogram[pixelColor.R]++;
           if (pixelColor.R > maxGrayValue) maxGrayValue = pixelColor.R;
           if (pixelColor.R < minGrayValue) minGrayValue = pixelColor.R;
         }
       }
       //迭代计算阀值
       int threshold = -1;
       int newThreshold = (minGrayValue + maxGrayValue) / 2;
       for ( int iterationTimes = 0; threshold != newThreshold && iterationTimes < 100; iterationTimes++)
       {
         threshold = newThreshold;
         int lP1 =0;
         int lP2 =0;
         int lS1 = 0;
         int lS2 = 0;
         //求两个区域的灰度的平均值
         for ( int i = minGrayValue;i < threshold;i++)
         {
           lP1 += histogram[i] * i;
           lS1 += histogram[i];
         }
         int mean1GrayValue = (lP1 / lS1);
         for ( int i = threshold+1;i < maxGrayValue;i++)
         {
           lP2 += histogram[i] * i;
           lS2 += histogram[i];
         }
         int mean2GrayValue = (lP2 / lS2);
         newThreshold = (mean1GrayValue + mean2GrayValue) / 2;
       }
       //计算二值化
       for ( int i = 0; i < img1.Width; i++)
       {
         for ( int j = 0; j < img1.Height; j++)
         {
           Color pixelColor = img1.GetPixel(i, j);
           if (pixelColor.R > threshold) img1.SetPixel(i, j, Color.FromArgb(255, 255, 255));
           else img1.SetPixel(i, j, Color.FromArgb(0, 0, 0));
         }
       }
     }
     static void Main( string [] args)
     {
       try
       {
         //打开位图文件
         Bitmap img1 = new Bitmap( "test.jpg" , true );
         //灰度化
         ToGrey(img1);
         //二值化
         Thresholding(img1);
         //写回位图文件
         img1.Save( "output.jpg" );
         Console.WriteLine( "Converted." );
       }
       catch (ArgumentException)
       {
         Console.WriteLine( "Invalid usage!" );
         Console.WriteLine( "Usage: bmp2grey source object" );
       }
     }
   }
}
目录
相关文章
|
测试技术 C# Windows
C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试
原文:C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试 WPF 没有用到 PictureBox, 而是用Image代替. 下面我试着加载显示一个图片 。 XAML CS Attempt 1: ImageMy_Image=newImage(Openfile.
1768 0
|
2月前
|
API C# 数据安全/隐私保护
C# 实现网页内容保存为图片并生成压缩包
C# 实现网页内容保存为图片并生成压缩包
|
7月前
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
选中项目,点击右上角的显示全部文件按钮,会将默认隐藏的文件显示出来,选中所需图片,右键,添加到项目,然后选择图片查看属性,生成操作选择resource。完毕。本人目前的解决方案。
261 41
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
|
5月前
|
API C#
C# 调用系统“API“设置图片为“桌面壁纸“
C# 调用系统“API“设置图片为“桌面壁纸“
|
7月前
|
C#
C# 图片RGB处理判断
C# 图片RGB处理判断 需要:根据一张原始图的RGB平均值和新的图片的RGB平均值的差距,来判断图中是否出现除原图中物体外的其他物体 前提:.Net framework 4.8 及以上 示例代码: 程序集:using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imagin...
22 0
|
10月前
|
人工智能 文字识别 API
C# 10分钟完成百度图片提取文字(文字识别)——入门篇
C# 10分钟完成百度图片提取文字(文字识别)——入门篇
|
算法 定位技术 C#
C#开发:不规则裁切图片
C#开发:不规则裁切图片
121 0
|
区块链 C#
C#实现把图片转换为ico格式
C#实现把图片转换为ico格式
600 0
|
C# 图形学
C#裁剪图片的方法
C#裁剪图片的方法
256 0