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#实现图片二值化例子(黑白效果)的更多相关文章

  1. python图片二值化提高识别率

    import cv2from PIL import Imagefrom pytesseract import pytesseractfrom PIL import ImageEnhanceimport ...

  2. C#图片灰度处理(位深度24→位深度8)、C#图片二值化处理(位深度8→位深度1)

    C#图片灰度处理(位深度24→位深度8) #region 灰度处理 /// <summary> /// 将源图像灰度化,并转化为8位灰度图像. /// </summary> / ...

  3. [置顶] c#验证码识别、图片二值化、分割、分类、识别

    c# 验证码的识别主要分为预处理.分割.识别三个步骤 首先我从网站上下载验证码 处理结果如下: 1.图片预处理,即二值化图片 *就是将图像上的像素点的灰度值设置为0或255. 原理如下: 代码如下: ...

  4. 验证码图片二值化问题 BitmapData 怎么解决

    对不起,这算是一篇求助啦,先上图,防止不清楚,放大了一点,下面是图片,上面是没有二值化的,下面是二值化之后的,我其实不懂什么是二值化啦,就是一定范围变黑,变白 问题: 为什么我的结果上面还是有很多彩色 ...

  5. 机器学习进阶-项目实战-信用卡数字识别 1.cv2.findContour(找出轮廓) 2.cv2.boudingRect(轮廓外接矩阵位置) 3.cv2.threshold(图片二值化操作) 4.cv2.MORPH_TOPHAT(礼帽运算突出线条) 5.cv2.MORPH_CLOSE(闭运算图片内部膨胀) 6. cv2.resize(改变图像大小) 7.cv2.putText(在图片上放上文本)

    7. cv2.putText(img, text, loc, text_font, font_scale, color, linestick) # 参数说明:img表示输入图片,text表示需要填写的 ...

  6. OpenCV - 图片二值化,计算白色像素点的个数

    直接上代码吧: import cv2 import numpy as np from PIL import Image area = def getWhitePixel(img): global ar ...

  7. python的N个小功能(图片预处理:打开图片,滤波器,增强,灰度图转换,去噪,二值化,切割,保存)

    ############################################################################################# ###### ...

  8. 基于Java对图片进行二值化处理

    一直以来对Java的图形处理能力表无力,但好像又不是那么一回事,之前用PHP做过一些应用,涉及到验证码的识别,其中有个图片二值化的步骤,今天换成Java来实现下 在java的扩展包javax.imag ...

  9. 致敬学长!J20航模遥控器开源项目计划【开局篇】 | 先做一个开机界面 | MATLAB图像二值化 | Img2Lcd图片取模 | OLED显示图片

    我们的开源宗旨:自由 协调 开放 合作 共享 拥抱开源,丰富国内开源生态,开展多人运动,欢迎加入我们哈~ 和一群志同道合的人,做自己所热爱的事! 项目开源地址:https://github.com/C ...

随机推荐

  1. C#访问MySQL数据库帮助类

    MySQL数据库访问帮助类 1.项目添加引用官方MySQL动态库MySql.Data.dll 下载地址:MySql.Data.dll(也可以到官网下载动态库)项目添加引用 这里有一个Mysql帮助类的 ...

  2. Python pyQt4/pyQt5 学习笔记2(状态栏、菜单栏和工具栏)

    例子:状态栏.菜单栏和工具栏 import sys from PyQt4 import QtGui class Example(QtGui.QMainWindow): def __init__(sel ...

  3. Excel中countif函数的使用方法

    1.countif函数的含义 在指定区域中按指定条件对单元格进行计数(单条件计数) 建议和countifs函数结合起来学习,可以观看小编的经验Excel中countifs函数的使用方法. END 2. ...

  4. jenkins部署war包到远程服务器的tomcat

    一.目的 jenkins上将war包,部署到远程服务器的tomcat上. 这边tomcat在windows 主机A上,版本apache-tomcat-8.5.23. jenkins在主机B上,cent ...

  5. jquery validate 多种使用方式

    前言:jQuery.validator是一款非常不错的表单验证插件,验证方式非常简单方便,它还对HTML5做了兼容处理,了解了验证规则,就基本掌握了它的使用,下面就让我一一道来 jQuery.vali ...

  6. github命令行下载项目源码

    一.git clone [URL] 下载指定ur的源码 $ git clone https://github.com/jquery/jquery 二.指定参数, -b是分支, --depth 1 最新 ...

  7. HTML display:inline-block

    元素转换 display:block;   行内转块 Display:inline;   块转行内 Display:inline-block;  块或行内转行内块 链接伪类 a:link{属性:值;} ...

  8. Thinkphp框架下设置session的过期时间

    打开项目中的配置文件,添加session的过期配置,如下: 'SESSION_OPTIONS' => array( 'name' => 'BJYSESSION', //设置session名 ...

  9. oracle如何设置表空间autoextensible自动扩容

    SELECT a.tablespace_name "表空间名", total / 1024 / 1024 "表空间大小单位M", free / 1024 / 1 ...

  10. 慕课学习--OSI与TCP/IP网络协议

    **OSI:开放系统互连参考模型 (Open System Interconnect 简称OSI)是国际标准化组织(ISO)和国际电报电话咨询委员会(CCITT)联合制定的开放系统互连参考模型,为开放 ...