/// <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. JS中的timestamp

    http://blog.163.com/lijy_980720@126/blog/static/75574626201261783343834/

  2. MMO可见格子算法

    看注释吧,写的很清楚了 using System; using System.Collections.Generic; using System.Diagnostics; using System.L ...

  3. Java for LeetCode 236 Lowest Common Ancestor of a Binary Tree

    解题思路一: DFS到每个节点的路径,根据路径算出LCA: public class Solution { public TreeNode lowestCommonAncestor(TreeNode ...

  4. ABAP 行列稳定刷新语句

    DATA stbl TYPE lvc_s_stbl. stbl-row = 'X'." 基于行的稳定刷新         stbl-col = 'X'." 基于列稳定刷新      ...

  5. 7.django之自定义分页记录

    只是大概记录下步骤: 1.表结构: class UserProfile(models.Model): ''' 用户表 ''' user = models.OneToOneField(User,verb ...

  6. Mysql 基础2

    创建数据库:     create  database/*条件*/+ text3/*数据库名称*/ 创建数据库  步骤:查询   创建查询  查询编辑器 (写代码) 删除数据库: drop datab ...

  7. 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  8. 【编程题目】请修改 append 函数,利用这个函数实现两个非降序链表的并集

    42.请修改 append 函数,利用这个函数实现(链表):两个非降序链表的并集,1->2->3 和 2->3->5 并为 1->2->3->5另外只能输出结 ...

  9. yii压缩

    application\components\controller.php protected function afterRender($view, &$output) { if(Yii:: ...

  10. July 8th, Week 28th Friday, 2016

    Care and diligence bring luck. 谨慎和勤奋带来好运气. Just as we have said before, diligence is the mother of g ...