博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Win8 Metro(C#)数字图像处理--2.55OSTU法图像二值化
阅读量:7021 次
发布时间:2019-06-28

本文共 2989 字,大约阅读时间需要 9 分钟。

原文:



[函数名称]

  Ostu法图像二值化      WriteableBitmap OstuThSegment(WriteableBitmap src)

[函数代码]

///         /// Ostu method of image segmention.        ///         /// The source image.        /// 
public static WriteableBitmap OstuThSegment(WriteableBitmap src) Ostu法阈值分割 { if (src != null) { int w = src.PixelWidth; int h = src.PixelHeight; WriteableBitmap dstImage = new WriteableBitmap(w, h); byte[] temp = src.PixelBuffer.ToArray(); byte[] tempMask = (byte[])temp.Clone(); //定义灰度图像信息存储变量 int[] srcData = new int[w * h]; //定义阈值变量 int Th = 0; ; //定义背景和目标像素数目变量N1,N2,灰度变量U1,U2,灰度和变量Sum1,Sum2,临时缓存变量Temp int N1 = 0, N2 = 0, Sum1 = 0, Sum2 = 0; //定义背景和目标像素比例变量W1,W2,图像整体平均灰度变量U,方差变量g,对比阈值变量TT double W1 = 0, W2 = 0, U1 = 0, U2 = 0, g = 0, TT = 0; for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { srcData[i + j * w] = (int)((double)tempMask[i * 4 + j * w * 4] * 0.114 + (double)tempMask[i * 4 + 1 + j * w * 4] * 0.587 + (double)tempMask[i * 4 + 2 + j * w * 4] * 0.299); } } //寻找最大类间方差 for (int T = 0; T <= 255; T++) { for (int i = 0; i < srcData.Length; i++) { if (srcData[i] > T) { N2++; Sum2 += srcData[i]; } else { N1++; Sum1 += srcData[i]; } } W1 = (double)(N1 / (N1 + N2)); W2 = (double)(1.0 - W1); U1 = (N1 == 0 ? 0.0 : (Sum1 / N1)); U2 = (N2 == 0 ? 0.0 : (Sum2 / N2)); g = N1 * N2 * (U1 - U2) * (U1 - U2); if (g > TT) { TT = g; Th = T; } N1 = 0; N2 = 0; Sum1 = 0; Sum2 = 0; W1 = 0.0; W2 = 0.0; U1 = 0.0; U2 = 0.0; g = 0.0; } for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { temp[i * 4 + j * w * 4] = temp[i * 4 + 1 + j * w * 4] = temp[i * 4 + 2 + j * w * 4] = (byte)(srcData[i + j * w] < Th ? 0 : 255); } } Stream sTemp = dstImage.PixelBuffer.AsStream(); sTemp.Seek(0, SeekOrigin.Begin); sTemp.Write(temp, 0, w * 4 * h); return dstImage; } else { return null; } }
你可能感兴趣的文章
MySQL 数据库 表 字段编码
查看>>
新的博客新技术ReactNative
查看>>
ElasticSearch 安装 ik 分词插件
查看>>
警告:MySQL-server-5.5.46-1.linux2.6.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY...
查看>>
Spring 3整合Quartz 2实现定时任务三:动态暂停 恢复 修改和删除任务
查看>>
pipework let's assign static IP to docker container simple.
查看>>
gentoo prefix重生(llvm/clang)
查看>>
线程池的一点理解
查看>>
MacOS下shh,sftp,scp简单使用
查看>>
Android(支持kotlin) 新版Bintray-极简上传Library到JCenter,可上传自定义maven仓库
查看>>
css3毛玻璃
查看>>
vue生命周期
查看>>
Vue响应式原理源码浅析
查看>>
RxSwift (二) Working with Subjects
查看>>
2018年终总结与展望 | 掘金年度征文
查看>>
HTML常用标签
查看>>
UITesting常见问题收集
查看>>
AQS同步组件--Semaphore
查看>>
webpack系列之五module生成1
查看>>
关于Spring Cloud—环境变化
查看>>