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 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 | usingSystem;usingSystem.Drawing;namespaceBMP2Grey{  classProgram  {    staticvoidToGrey(Bitmap img1)    {      for(inti = 0; i < img1.Width; i++)      {        for(intj = 0; j < img1.Height; j++)        {          Color pixelColor = img1.GetPixel(i, j);          //计算灰度值          intgrey = (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);        }      }    }    staticvoidThresholding(Bitmap img1)    {      int[] histogram = newint[256];      intminGrayValue=255, maxGrayValue=0;      //求取直方图      for(inti = 0; i < img1.Width; i++)      {        for(intj = 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;        }      }      //迭代计算阀值      intthreshold = -1;      intnewThreshold = (minGrayValue + maxGrayValue) / 2;      for(intiterationTimes = 0; threshold != newThreshold && iterationTimes < 100; iterationTimes++)      {        threshold = newThreshold;        intlP1 =0;        intlP2 =0;        intlS1 = 0;        intlS2 = 0;        //求两个区域的灰度的平均值        for(inti = minGrayValue;i < threshold;i++)        {          lP1 += histogram[i] * i;          lS1 += histogram[i];        }        intmean1GrayValue = (lP1 / lS1);        for(inti = threshold+1;i < maxGrayValue;i++)        {          lP2 += histogram[i] * i;          lS2 += histogram[i];        }        intmean2GrayValue = (lP2 / lS2);        newThreshold = (mean1GrayValue + mean2GrayValue) / 2;      }      //计算二值化      for(inti = 0; i < img1.Width; i++)      {        for(intj = 0; j < img1.Height; j++)        {          Color pixelColor = img1.GetPixel(i, j);          if(pixelColor.R > threshold) img1.SetPixel(i, j, Color.FromArgb(255, 255, 255));          elseimg1.SetPixel(i, j, Color.FromArgb(0, 0, 0));        }      }    }    staticvoidMain(string[] args)    {      try      {        //打开位图文件        Bitmap img1 = newBitmap("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#实现图片二值化例子(黑白效果)的更多相关文章
- python图片二值化提高识别率
		import cv2from PIL import Imagefrom pytesseract import pytesseractfrom PIL import ImageEnhanceimport ... 
- C#图片灰度处理(位深度24→位深度8)、C#图片二值化处理(位深度8→位深度1)
		C#图片灰度处理(位深度24→位深度8) #region 灰度处理 /// <summary> /// 将源图像灰度化,并转化为8位灰度图像. /// </summary> / ... 
- [置顶] c#验证码识别、图片二值化、分割、分类、识别
		c# 验证码的识别主要分为预处理.分割.识别三个步骤 首先我从网站上下载验证码 处理结果如下: 1.图片预处理,即二值化图片 *就是将图像上的像素点的灰度值设置为0或255. 原理如下: 代码如下: ... 
- 验证码图片二值化问题 BitmapData 怎么解决
		对不起,这算是一篇求助啦,先上图,防止不清楚,放大了一点,下面是图片,上面是没有二值化的,下面是二值化之后的,我其实不懂什么是二值化啦,就是一定范围变黑,变白 问题: 为什么我的结果上面还是有很多彩色 ... 
- 机器学习进阶-项目实战-信用卡数字识别 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表示需要填写的 ... 
- OpenCV - 图片二值化,计算白色像素点的个数
		直接上代码吧: import cv2 import numpy as np from PIL import Image area = def getWhitePixel(img): global ar ... 
- python的N个小功能(图片预处理:打开图片,滤波器,增强,灰度图转换,去噪,二值化,切割,保存)
		############################################################################################# ###### ... 
- 基于Java对图片进行二值化处理
		一直以来对Java的图形处理能力表无力,但好像又不是那么一回事,之前用PHP做过一些应用,涉及到验证码的识别,其中有个图片二值化的步骤,今天换成Java来实现下 在java的扩展包javax.imag ... 
- 致敬学长!J20航模遥控器开源项目计划【开局篇】 | 先做一个开机界面 | MATLAB图像二值化 | Img2Lcd图片取模 | OLED显示图片
		我们的开源宗旨:自由 协调 开放 合作 共享 拥抱开源,丰富国内开源生态,开展多人运动,欢迎加入我们哈~ 和一群志同道合的人,做自己所热爱的事! 项目开源地址:https://github.com/C ... 
随机推荐
- sencha touch Model validations(模型验证,自定义验证)
			model Ext.define('app.model.Register', { extend: 'Ext.data.Model', requires: ['Ext.data.JsonP'], con ... 
- thinkphp 构建子查询
			thinkphp构建子查询sql语句写法 从3.0版本开始新增了子查询支持,有两种使用方式: 1.使用select方法 当select方法的参数为false的时候,表示不进行查询只是 ... 
- Visual Studio 2012创建SQL Server Database Project提示失败解决方法
			新建一个SQL Server Database Project,提示: Unable to open Database project This version of SQL Server Data ... 
- Egret动态设置按钮的图片
			参考: 动态设置Button按钮的状态图片 按钮有3个状态,up down disabled.这里区别于source,source.down,source.disabled,而是每个状态单独一个ima ... 
- 【CF845F】Guards In The Storehouse 插头DP
			[CF845F]Guards In The Storehouse 题意:一个n*m的房间,每个格子要么是障碍要么是空地.对于每个空地你可以选择放或者不放守卫.一个守卫能保护到的位置是:他右面的一行空地 ... 
- iOS的socket开发基础
			目录[-] socket简介 tcp和udp的区别 TCP三次握手和四次挥手 TCP三次握手 tcp四次挥手 tcpsocket和udpsocket的具体实现 tcpsocket的具体实现 udpso ... 
- springMVC 几种页面跳转方式
			今天主要写一下响应界面跳转的几种方式 1.在注解的方式中 1.1通过HttpServletResponse的API直接输出(不需要配置渲染器) controller类的主要代码 @Controller ... 
- Twig---和vue或angular前端框架并存
			<h1> {% verbatim %} {{message}} {% endverbatim %} </h1> 上面这种方式虽然能够解决,前台渲染的问题,但是还是会报错: 第二 ... 
- poj3264 balanced lineup【线段树】
			For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ... 
- Django的quarySet
			models.py 代码 from django.db import models # Create your models here. class Author(models.Model): nam ... 
