C# 图片位深度转至8位灰度图像,8位灰度图像转为1位灰度图像
#region 二值化
#region Otsu阈值法二值化模块
/// <summary>
/// Otsu阈值
/// </summary>
/// <param name="b">位图流</param>
/// <returns></returns>
public Bitmap OtsuThreshold(Bitmap bitmap)
{
// 图像灰度化
// b = Gray(b);
int width = bitmap.Width;
int height = bitmap.Height;
byte threshold = 0;
int[] hist = new int[256];
int AllPixelNumber = 0, PixelNumberSmall = 0, PixelNumberBig = 0;
double MaxValue, AllSum = 0, SumSmall = 0, SumBig, ProbabilitySmall, ProbabilityBig, Probability;
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
unsafe
{
byte* p = (byte*)bmpData.Scan0;
int offset = bmpData.Stride - width * 4;
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
hist[p[0]]++;
p += 4;
}
p += offset;
}
bitmap.UnlockBits(bmpData);
}
//计算灰度为I的像素出现的概率
for (int i = 0; i < 256; i++)
{
AllSum += i * hist[i]; // 质量矩
AllPixelNumber += hist[i]; // 质量
}
MaxValue = -1.0;
for (int i = 0; i < 256; i++)
{
PixelNumberSmall += hist[i];
PixelNumberBig = AllPixelNumber - PixelNumberSmall;
if (PixelNumberBig == 0)
{
break;
}
SumSmall += i * hist[i];
SumBig = AllSum - SumSmall;
ProbabilitySmall = SumSmall / PixelNumberSmall;
ProbabilityBig = SumBig / PixelNumberBig;
Probability = PixelNumberSmall * ProbabilitySmall * ProbabilitySmall + PixelNumberBig * ProbabilityBig * ProbabilityBig;
if (Probability > MaxValue)
{
MaxValue = Probability;
threshold = (byte)i;
}
}
this.Threshoding(bitmap, threshold);
bitmap = twoBit(bitmap);
return bitmap; ;
}
#endregion
#region 固定阈值法二值化模块
public Bitmap Threshoding(Bitmap b, byte threshold)
{
int width = b.Width;
int height = b.Height;
BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
unsafe
{
byte* p = (byte*)data.Scan0;
int offset = data.Stride - width * 4;
byte R, G, B, gray;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
R = p[2];
G = p[1];
B = p[0];
gray = (byte)((R * 19595 + G * 38469 + B * 7472) >> 16);
if (gray >= threshold)
{
p[0] = p[1] = p[2] = 255;
}
else
{
p[0] = p[1] = p[2] = 0;
}
p += 4;
}
p += offset;
}
b.UnlockBits(data);
return b;
}
}
#endregion
#region 创建1位图像
/// <summary>
/// 创建1位图像
/// </summary>
/// <param name="srcBitmap"></param>
/// <returns></returns>
public Bitmap twoBit(Bitmap srcBitmap)
{
int midrgb = System.Drawing.Color.FromArgb(128, 128, 128).ToArgb();
int stride;//简单公式((width/8)+3)&(~3)
stride = (srcBitmap.Width % 8) == 0 ? (srcBitmap.Width / 8) : (srcBitmap.Width / 8) + 1;
stride = (stride % 4) == 0 ? stride : ((stride / 4) + 1) * 4;
int k = srcBitmap.Height * stride;
byte[] buf = new byte[k];
int x = 0, ab = 0;
for (int j = 0; j < srcBitmap.Height; j++)
{
k = j * stride;//因图像宽度不同、有的可能有填充字节需要跳越
x = 0;
ab = 0;
for (int i = 0; i < srcBitmap.Width; i++)
{
//从灰度变单色(下法如果直接从彩色变单色效果不太好,不过反相也可以在这里控制)
if ((srcBitmap.GetPixel(i, j)).ToArgb() > midrgb)
{
ab = ab * 2 + 1;
}
else
{
ab = ab * 2;
}
x++;
if (x == 8)
{
buf[k++] = (byte)ab;//每字节赋值一次,数组buf中存储的是十进制。
ab = 0;
x = 0;
}
}
if (x > 0)
{
//循环实现:剩余有效数据不满1字节的情况下须把它们移往字节的高位部分
for (int t = x; t < 8; t++) ab = ab * 2;
buf[k++] = (byte)ab;
}
}
int width = srcBitmap.Width;
int height = srcBitmap.Height;
Bitmap dstBitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
BitmapData dt = dstBitmap.LockBits(new Rectangle(0, 0, dstBitmap.Width, dstBitmap.Height), ImageLockMode.ReadWrite, dstBitmap.PixelFormat);
Marshal.Copy(buf, 0, dt.Scan0, buf.Length);
dstBitmap.UnlockBits(dt);
return dstBitmap;
}
#endregion
#endregion
/// <summary>
/// 创建8位灰度图像
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static Bitmap CreateGrayscaleImage(int width, int height)
{
// create new image
Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
// set palette to grayscale
SetGrayscalePalette(bmp);
// return new image
return bmp;
}
///<summary>
/// Set pallete of the image to grayscale
///</summary>
public static void SetGrayscalePalette(Bitmap srcImg)
{
// check pixel format
if (srcImg.PixelFormat != System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
throw new ArgumentException();
// get palette
ColorPalette cp = srcImg.Palette;
// init palette
for (int i = 0; i < 256; i++)
{
cp.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
}
srcImg.Palette = cp;
}
//#region 转为位深度为8的灰度图像
/// <summary>
/// 转为灰度图像,位深度也改变
/// </summary>
/// <param name="srcBitmap"></param>
/// <returns></returns>
public static Bitmap RGB2Gray(Bitmap srcBitmap)
{
int wide = srcBitmap.Width;
int height = srcBitmap.Height;
Rectangle rect = new Rectangle(0, 0, wide, height);
//将Bitmap锁定到系统内存中,获得BitmapData
BitmapData srcBmData = srcBitmap.LockBits(rect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//创建Bitmap
Bitmap dstBitmap = CreateGrayscaleImage(wide, height);//这个函数在后面有定义
BitmapData dstBmData = dstBitmap.LockBits(rect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
//位图中第一个像素数据的地址。它也可以看成是位图中的第一个扫描行
System.IntPtr srcPtr = srcBmData.Scan0;
System.IntPtr dstPtr = dstBmData.Scan0;
//将Bitmap对象的信息存放到byte数组中
int src_bytes = srcBmData.Stride * height;
byte[] srcValues = new byte[src_bytes];
int dst_bytes = dstBmData.Stride * height;
byte[] dstValues = new byte[dst_bytes];
//复制GRB信息到byte数组
System.Runtime.InteropServices.Marshal.Copy(srcPtr, srcValues, 0, src_bytes);
System.Runtime.InteropServices.Marshal.Copy(dstPtr, dstValues, 0, dst_bytes);
//根据Y=0.299*R+0.114*G+0.587B,Y为亮度
for (int i = 0; i < height; i++)
for (int j = 0; j < wide; j++)
{
//只处理每行中图像像素数据,舍弃未用空间
//注意位图结构中RGB按BGR的顺序存储
int k = 3 * j;
byte temp = (byte)(srcValues[i * srcBmData.Stride + k + 2] * .299 + srcValues[i * srcBmData.Stride + k + 1] * .587 + srcValues[i * srcBmData.Stride + k] * .114);
dstValues[i * dstBmData.Stride + j] = temp;
}
System.Runtime.InteropServices.Marshal.Copy(dstValues, 0, dstPtr, dst_bytes);
//解锁位图
srcBitmap.UnlockBits(srcBmData);
dstBitmap.UnlockBits(dstBmData);
return dstBitmap;
}
C# 图片位深度转至8位灰度图像,8位灰度图像转为1位灰度图像相关推荐
- C#图片灰度处理(位深度24→位深度8),用灰度数组byte[]新建一个8位灰度图像Bitmap 。...
原文:C#图片灰度处理(位深度24→位深度8) #region 灰度处理/// <summary>/// 将源图像灰度化,并转化为8位灰度图像./// </summary>// ...
- matlab修改图片位深度_如何利用matlab统一处理照片亮度对比度
第一:下软件MATLAB,和插件SHINEtoolbox.安装路径最好不要有中文. 将插件复制到文件夹下见图片 注意:不用学习matlab的具体应用,会使用插件就行了,插件是已经做好的编程,仅需把插件 ...
- android 将bitmap存为 bmp格式图片大小,把bitmap保存成 BMP的格式 并且位深度为1
生成图片的要求:图像格式采用单色位图文件格式(BMP) 要求bmp的位深度为1 参考: 代码有点小瑕疵 : bitmap的图片宽度要求是:8的整数倍 /** * 将Bitmap存为 .bmp格式图片 ...
- 关于修改ico图片位深度的方法
在vs08中载入待修改的图片,并打开,右键选择新建图片类型,然后选择自定义,定义自己需要的位深度的图片.再把以前的那个类型删掉就好了
- 深度学习--------图片的位深度含义
以前没接触深度学习的时候没注意过图片位深度的问题,最近研究深度学习图片输入弄的也是莫名奇妙,焦头烂额.记录一下自己搜的资料的总结.首先要明白计算机的储存方式位二进制,只有0和1,因此图片的像素矩阵也不 ...
- matlab修改图片位深度_Matlab 图像处理基本操作
http://blog.sina.com.cn/s/blog_4a93ccea0100d1lw.html 一.图像基本操作 1.读取图像并显示: >> clear;close all ...
- 位深度、色深的区别以及图片大小的计算
首先我们先来理解下位深度与色深这两个概念. 1.位深度指的是存储每个像素所用的位数,主要用于存储 2.色深指的是每一个像素点用多少bit存储颜色,属于图片自身的一种属性 Android Bitmap中 ...
- 32位java怎么改_java修改24位深度png图片为32位深度
前言 在做图片切割时,发现切除来的图片空白部分变成了黑色背景,发现属性是24位深度 解决方案 1.先上代码 BufferedImage image = readImage(url); Buffered ...
- 还在纠结通道数、位深度?实验带你看懂关于灰度图像,二值化图像,彩色图像、图片通道数,位深度的全部内容
数据加载相关 前言 图片的通道数.位深度 单通道 三通道 四通道 通道数之间的转化 二值化图像 小结 灰度图像 小结 pillow库 相关全部代码 前言 首先我们都知道,图像是由一个个像素点组成的.图 ...
- 【图片位深度改变】24位深度转8位深度
代码如下: """ 2020.09.26:alian 改变图片的位深度 """ from PIL import Image import n ...
最新文章
- 2019微生物组—宏基因组分析技术专题研讨会第四期
- android魅族轮播图,用angularjs模仿魅族官网的图片轮播功能
- VuePress 添加百度统计代码
- CodeForces 1616H Keep XOR Low {a^b≤x} / CodeForces gym102331 Bitwise Xor {a^b≥x}(trie树 + 计数)
- linux 查看path文件,linux入门之环境变量与文件查找
- guid怎么做到唯一_怎么做成为一个好女人呢?
- pandas.describe()参数的意义
- Docker 深入理解概念
- 23种设计模式(1)-单例模式
- codevs 1683 车厢重组
- python-图书管理系统5-全部代码
- 一步一步安装 Windows Server 2008 Beta3(Code Name Longhorn)
- 华为交换机关闭网口_华为交换机关闭端口号
- 牛客练习赛73 遥远的记忆(理解)
- 百度bae定时任务使用方法
- 2019世界移动通信大会--中国5G迎来高光时刻
- YOLO系列算法原理介绍
- 易语言让我东山再起 邓学彬(优秀文章)
- 一份致敬所有通信行业的老炮儿的信。
- war项目连接linux数据库,springboot项目war包部署到Linux
热门文章
- 水深注记采用渐变颜色的编程实现
- 书评第003篇:《0day安全:软件漏洞分析技术(第2版)》
- 解析新时代人工智能机器人的工作原理
- 【无标题】打印水仙花(pyth)
- mysql中如何去除重复数据_MySQL如何去除重复数据?
- 『Java安全』反序列化-浅析Hessian反序列化POP链
- reporting php,php error_reporting函数怎么用
- python判断手机号运营商_基于python的-使用正则表达式验证手机号并匹配运营商和所述地域...
- 美团外卖用户差评情况分析
- 为什么手机网速太慢_手机网速慢怎么回事?一分钟就提速两倍的方法