C#,图像二值化(02)——用于图像二值化处理的一些基本图像处理函数之C#源代码
上一篇:
1、Image binarization
The binarization process is a conversion of a color or grayscale image into a two-color black and white image. The main parameter of this conversion is the threshold t, with the value of which the brightness of all is then compared.
Binarization is important in digital image processing, mainly in computer vision applications. Thresholding is an efficient technique in binarization. The choice of thresholding technique is crucial in binarization. There are various thresholding algorithms have been proposed to define the optimal threshold value.
Binarization can be used in recognising text and symbols, e.g. document processing. Identifying objects with distinctive silhouettes, e.g. components on a conveyor in a manufacturing plant, and determining the orientation of objects are some other examples of binarization applications. Binarization generally involves two steps including the determination of a gray threshold according to some objective criteria and assigning each pixel to one class of background or foreground. If the intensity of the pixel is greater than the determined threshold then it belongs to the foreground class and otherwise to the background. The main problem in binarization is the choice of thresholding technique.
2、Grayscale image
A grayscale image is the one that has the same value for each channel. Grayscale images have only colours of gray shades, Gray shades are not only black and white. If the image has 8-bit depth. It can have 255 shades of gray between dark and white. Sometimes grayscale images are also called black and white images.
3、Some Support C# Sourcecodes POWER BY TRUFFER.CN
本页面的C#代码包括但不限于:图像Bitmap转byte[]数组;灰度直方图计算;灰度最大值、灰度最小值计算;直方图归一化计算等等。
using System;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
namespace Legalsoft.Truffer.ImageTools
{
public static partial class BinarizationHelper
{
#region 基础函数
/// <summary>
/// 图片转存为byte数组
/// </summary>
/// <param name="bitmap"></param>
/// <returns></returns>
public static byte[] BitmapToArray(Bitmap bitmap, out int img_stride)
{
try
{
// 定义锁定bitmap的rect的指定范围区域
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
// 加锁区域像素
var bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// 位图的首地址
var ptr = bitmapData.Scan0;
// stride:扫描行
img_stride = bitmapData.Stride;
int len = bitmapData.Stride * bitmap.Height;
byte[] img_bytes = new byte[len];
// 锁定区域的像素值copy到byte数组中
Marshal.Copy(ptr, img_bytes, 0, len);
// 解锁
bitmap.UnlockBits(bitmapData);
return img_bytes;
}
catch (Exception ex)
{
throw new Exception("BitmapHelper.BitmapToArray ERROR:" + ex.Message);
}
}
/// <summary>
/// 极大极小值限定(0-255)
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
public static double Clamp(double v)
{
v = (v > 255.0) ? 255.0 : ((v < 0.0) ? 0.0 : v);
return v;
}
/// <summary>
/// 灰度计算(简单)
/// </summary>
/// <param name="r"></param>
/// <param name="g"></param>
/// <param name="r"></param>
/// <returns></returns>
public static byte GreyByte(byte r, byte g, byte b)
{
return (byte)Clamp(r * 0.299 + g * 0.587 + b * 0.114);
}
/// <summary>
/// 彩色图转灰度图
/// </summary>
/// <param name="data"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="stride"></param>
/// <param name="channels"></param>
public static byte[,] ColorToGrayArray(byte[] data, int width, int height, int stride, int channels = 3)
{
byte[,] grayArray = new byte[height, width];
for (int bz = 0, y = 0; y < height; y++)
{
for (int zz = bz, x = 0; x < width; x++)
{
byte g = GreyByte(data[zz + 2], data[zz + 1], data[zz + 0]);
grayArray[y, x] = g;
zz += channels;
}
bz += stride;
}
return grayArray;
}
/// <summary>
/// 最小与最大灰度
/// </summary>
/// <param name="data"></param>
/// <param name="min"></param>
/// <param name="max"></param>
public static void Minium_Maxium(byte[,] data, out int min, out int max)
{
int height = data.GetLength(0);
int width = data.GetLength(1);
max = 0;
min = 255;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
byte bt = data[y, x];
if (bt < min)
{
min = bt;
}
if (bt > max)
{
max = bt;
}
}
}
}
/// <summary>
/// 灰度直方图统计
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static int[] Gray_Histogram(byte[,] data)
{
int height = data.GetLength(0);
int width = data.GetLength(1);
int[] hv = new int[256];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
hv[data[y, x]]++;
}
}
return hv;
}
/// <summary>
/// 左端第一个非零
/// </summary>
/// <param name="histogram"></param>
/// <returns></returns>
public static int Histogram_Left(int[] histogram)
{
for (int i = 0; i < histogram.Length; i++)
{
if (histogram[i] > 0)
{
return i;
}
}
return 0;
}
/// <summary>
/// 右端第一个非零
/// </summary>
/// <param name="histogram"></param>
/// <returns></returns>
public static int Histogram_Right(int[] histogram)
{
for (int i = histogram.Length - 1; i >= 0; i--)
{
if (histogram[i] > 0)
{
return i;
}
}
return 0;
}
/// <summary>
/// 直方图和值
/// </summary>
/// <param name="histogram"></param>
/// <returns></returns>
public static int Histogram_Sum(int[] histogram)
{
return histogram.Sum();
}
/// <summary>
/// 加权和值
/// </summary>
/// <param name="histogram"></param>
/// <param name="right"></param>
/// <param name="grade"></param>
public static double Histogram_Sum(int[] histogram, int grade = 0, int right = 255)
{
double sum = 0.0;
for (int i = 0; i < histogram.Length && i <= right; i++)
{
double v = (double)histogram[i];
if (grade >= 1) v *= (double)i;
if (grade >= 2) v *= (double)i;
if (grade >= 3) v *= (double)i;
sum += v;
}
return sum;
}
/// <summary>
/// 直方图数据转为浮点数数组
/// </summary>
/// <param name="histogram"></param>
/// <returns></returns>
public static double[] Histogram_To_Float(int[] histogram)
{
double[] hv = new double[histogram.Length];
for (int i = 0; i < histogram.Length; i++)
{
hv[i] = histogram[i];
}
return hv;
}
/// <summary>
/// 直方图归一化
/// </summary>
/// <param name="histogram"></param>
public static double[] Histogram_Normalize(int[] histogram)
{
double sum = 1.0 / histogram.Sum();
double[] hn = new double[256];
for (int i = 0; i < histogram.Length; i++)
{
hn[i] = (double)histogram[i] * sum;
}
return hn;
}
/// <summary>
/// 灰度图的直方图均衡化处理
/// </summary>
/// <param name="data"></param>
public static void Gray_Equalization(byte[,] data)
{
int height = data.GetLength(0);
int width = data.GetLength(1);
int[] histogram = Gray_Histogram(data);
double[] density = Histogram_Normalize(histogram);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
byte c = data[y, x];
double dsum = 0.0;
for (int k = 0; k <= c; k++)
{
dsum += density[k];
}
data[y, x] = (byte)Math.Round(255.0 * dsum);
}
}
}
#endregion
}
}
这些函数将被后续发布的二十三种二值化算法所使用。
The thresholding algorithms can be categorized into different classes:
Histogram shape-based methods
. Clustering-based methods
. Entropy-based methods
. Object attribute-based methods
. Spatial methods and local methods are based on the local characteristics of each pixel.
The binarization process is a conversion of a color or grayscale image into a two-color black and white image. The main parameter of this conversion is the threshold t, with the value of which the brightness of all is then compared.
Binarization is important in digital image processing, mainly in computer vision applications. Thresholding is an efficient technique in binarization. The choice of thresholding technique is crucial in binarization. There are various thresholding algorithms have been proposed to define the optimal threshold value.
下一篇:
C#,图像二值化(03)——全局阈值的基本算法及其源程序https://blog.csdn.net/beijinghorn/article/details/128426857
相关文章
- 9.1C#中类的定义
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解
- C#订阅与发布标准实现 visual studio code .net 开发 设计模式之☞策略模式 C#字符串转二进制、二进制转字符串 c# 接口的协变和逆变 c# 使用迭代器来创建可枚举类型 博客园首页新随笔联系订阅管理 随笔 - 117 文章 - 0 评论 - 57 c# 创建,加载,修改XML文档
- c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)
- 请求大神,C#如何截取字符串中指定字符之间的部分 按指定字符串分割 一分为二 c# 去除字符串中的某个已知字符
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对
- 利用反射快速给Model实体赋值 使用 Task 简化异步编程 Guid ToString 格式知多少?(GUID 格式) Parallel Programming-实现并行操作的流水线(生产者、消费者) c# 无损高质量压缩图片代码 8种主要排序算法的C#实现 (一) 8种主要排序算法的C#实现 (二)
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- 浅谈c#的三个高级参数ref out 和Params C#中is与as的区别分析 “登陆”与“登录”有何区别 经典SQL语句大全(绝对的经典)
- 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂
- api 和 C# 里的接口的区别?
- Word控件Spire.Doc 转换教程(十六):在 C# 中将 Word 转换为 PDF/A
- Word处理控件Aspose.Words功能演示:在 C# 中将 Word 文档转换为 EPUB
- 【C#】request各个获取参数方法间的区别
- c# Linq 求和,求平均值,求最大,求最小,分组,计数
- c# HttpClient禁止缓存
- C#【通用方法】ini文件的读和写
- C#,数值计算(Numerical Recipes in C#),矩阵的奇异值分解(SVD,Singular Value Decomposition)算法与源代码
- 【转】C#时间格式
- c#操作mysql数据库
- C# 图像处理
- c# 静态变量
- 用c#开发微信 (16) 微活动 2 刮刮卡
- C#生成城市按照一定格式且按字母顺序的方法
- c#面向对象 --相关知识
- 转:C#与SQl数据的对应关系(tinyint、smallint、int、bigint)