/// <summary>
/// 二值化图像
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
private static unsafe Bitmap Binaryzation(Bitmap bmp)
{
BitmapData dstData = bmp.LockBits(new Rectangle(, , bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
byte* data = (byte*)(dstData.Scan0);
//将图像转换为0,1二值得图像;
int step = dstData.Stride; int means = getThreshold(data, bmp.Height * step);
for (int y = ; y < bmp.Height; y++)
{
for (int x = ; x < bmp.Width * ; x += )
{
if (data[y * step + x + ] > means)
data[y * step + x]
= data[y * step + x + ]
= data[y * step + x + ]
= ;
else
data[y * step + x]
= data[y * step + x + ]
= data[y * step + x + ]
= ;
}
}
bmp.UnlockBits(dstData);
return bmp;
} /// <summary>
/// 图像二值化 获取阀值
/// </summary>
/// <param name="inPixels"></param>
/// <param name="length">height * Stride</param>
/// <returns></returns>
private static unsafe int getThreshold(byte* inPixels, int length)
{
int inithreshold = ;
int finalthreshold = ;
List<int> temp = new List<int>();
for (int index = ; index < length; index += )
{
temp.Add(inPixels[index + ]);
}
List<int> sub1 = new List<int>();
List<int> sub2 = new List<int>();
int means1 = , means2 = ;
while (finalthreshold != inithreshold)
{
finalthreshold = inithreshold;
for (int i = ; i < temp.Count(); i++)
{
if (temp[i] <= inithreshold)
{
sub1.Add(temp[i]);
}
else
{
sub2.Add(temp[i]);
}
}
means1 = getMeans(sub1);
means2 = getMeans(sub2);
sub1.Clear();
sub2.Clear();
inithreshold = (means1 + means2) / ;
}
return finalthreshold;
} /// <summary>
/// 图像二值化 获取Means
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private static int getMeans(List<int> data)
{
int result = ;
int size = data.Count();
foreach (int i in data)
{
result += i;
}
return (result / size);
}

C# 指针操作图像 二值化处理的更多相关文章

  1. OpenCV_基于局部自适应阈值的图像二值化

    在图像处理应用中二值化操作是一个很常用的处理方式,例如零器件图片的处理.文本图片和验证码图片中字符的提取.车牌识别中的字符分割,以及视频图像中的运动目标检测中的前景分割,等等. 较为常用的图像二值化方 ...

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

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

  3. python实现图像二值化

    1.什么是图像二值化 彩色图像: 有blue,green,red三个通道,取值范围均为0-255 灰度图:只有一个通道0-255,所以一共有256种颜色 二值图像:只有两种颜色,黑色和白色,二值化就是 ...

  4. openCV_java 图像二值化

    较为常用的图像二值化方法有:1)全局固定阈值:2)局部自适应阈值:3)OTSU等. 局部自适应阈值则是根据像素的邻域块的像素值分布来确定该像素位置上的二值化阈值.这样做的好处在于每个像素位置处的二值化 ...

  5. MATLAB:图像二值化、互补图(反运算)(im2bw,imcomplement函数)

    图像二值化.反运算过程涉及到im2bw,imcomplement函数,反运算可以这么理解:原本黑的区域变为白的区域,白的区域变为黑的区域. 实现过程如下: close all; %关闭当前所有图形窗口 ...

  6. Python+OpenCV图像处理(十)—— 图像二值化

    简介:图像二值化就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的黑白效果的过程. 一.普通图像二值化 代码如下: import cv2 as cv import numpy ...

  7. [python-opencv]图像二值化【图像阈值】

    图像二值化[图像阈值]简介: 如果灰度图像的像素值大于阈值,则为其分配一个值(可以是白色255),否则为其分配另一个值(可以是黑色0) 图像二值化就是将灰度图像上的像素值设置为0或255,也就是将整个 ...

  8. Win8 Metro(C#)数字图像处理--2.59 P分位法图像二值化

    原文:Win8 Metro(C#)数字图像处理--2.59 P分位法图像二值化  [函数名称]   P分位法图像二值化 [算法说明]   所谓P分位法图像分割,就是在知道图像中目标所占的比率Rat ...

  9. Win8 Metro(C#)数字图像处理--2.55OSTU法图像二值化

    原文:Win8 Metro(C#)数字图像处理--2.55OSTU法图像二值化  [函数名称] Ostu法图像二值化      WriteableBitmap OstuThSegment(Writ ...

随机推荐

  1. python to be Windows Daemon

    参考:http://assback.iteye.com/blog/1731565 安装 pywin32-.win32-py2..exe #32bit pywin32-.win-amd64-py2..e ...

  2. ssh免密码登陆设置

    服务器端 CentOS 6.5下编辑/etc/ssh/sshd_config MacOSx下编辑/etc/sshd_config #开启公钥验证 RSAAuthentication yes Pubke ...

  3. codecademy-command line-inputoutput

    What happens when you type this command? $ echo "Hello" Hello The echo command accepts the ...

  4. 用C#读取txt文件的方法

    1.使用FileStream读写文件 文件头: using System;using System.Collections.Generic;using System.Text;using System ...

  5. SQL Server case表达式的用法

    ★CASE表达式是一个标量表达式,它基于条件逻辑来返回一个值.因为CASE是一个标量表达式,所以它可以应用在SELECT.WHERE.HAVING以及ORDER BY子句中. CASE表达式有两种格式 ...

  6. 【HOG】

    http://blog.csdn.net/masibuaa/article/details/12917961 把这份资料大概看完了 大概了解Hog了

  7. 安装Odoo9出现的could not execute command "lessc"问题

    解决方案: apt-get install node-less

  8. 使用a标签删除进行提示

    一句话搞定: <a href="Login.aspx" target="mainFrame" class="STYLE4" oncli ...

  9. [Android Pro] Android以root起一个process[shell脚本的方法]

    reference to :  http://***/Article/11768 有时候我们写的app要用uid=0的方式启动一个process,framework层和app层是做不到的,只有通过写脚 ...

  10. 图像特征提取之LBP特征

    LBP(Local Binary Pattern,局部二值模式)是一种用来描述图像局部纹理特征的算子:它具有旋转不变性和灰度不变性等显著的优点.它是首先由T. Ojala, M.Pietik?inen ...