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

作者:junjie 时间:2023-02-19 11:52:46 

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

原图:

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

二值化后的图像:

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

实现代码:


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#,图片,二值化
0
投稿

猜你喜欢

  • java日志打印的完全使用指南

    2023-07-02 15:02:28
  • Java建造者设计模式详解

    2022-09-19 13:14:11
  • java多线程加锁以及Condition类的使用实例解析

    2023-08-07 07:25:30
  • Java实现CORS跨域请求的实现方法

    2022-04-01 19:49:18
  • C#将Word转换成PDF方法汇总(基于Office和WPS)

    2021-09-12 22:52:58
  • Java实现图片拼接

    2023-02-28 23:01:27
  • java操作mongodb之多表联查的实现($lookup)

    2023-08-08 10:24:07
  • Android webview与js交换JSON对象数据示例

    2022-10-19 18:36:18
  • Android控件PopupWindow模仿ios底部弹窗

    2023-03-09 20:42:10
  • Android使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信界面

    2021-09-04 15:39:22
  • 深入Android Handler,MessageQueue与Looper关系

    2023-01-24 03:34:25
  • C#支付宝新版支付请求接口调用

    2023-02-23 15:50:45
  • 详解Java springboot 整合Shiro框架

    2022-12-14 15:56:21
  • Java将String字符串带括号转成List的简单方法

    2022-10-26 18:20:17
  • Spring内存缓存Caffeine的基本使用教程分享

    2023-05-26 00:30:33
  • Android仿QQ圆形头像个性名片

    2021-08-24 02:40:46
  • 微信公众平台开发实战Java版之微信获取用户基本信息

    2023-06-02 23:05:50
  • android6.0权限动态申请框架permissiondispatcher的方法

    2023-07-31 10:51:57
  • spring cloud consul注册的服务报错critical的解决

    2021-05-28 14:13:14
  • Android GestureDetector手势滑动使用实例讲解

    2022-01-18 01:34:12
  • asp之家 软件编程 m.aspxhome.com