根据OSTU大津法使用Matlab实现数字图像处理segmentation的graythresh函数
来源:互联网 发布:空调推荐 知乎 编辑:程序博客网 时间:2024/05/17 01:05
OSTU大津法算法思想:
一幅有depth个灰度级,根据每个灰度级t,可以将一幅图分为前景和背景。
前景指所有灰度级低于等于t的像素点,背景指大于t的像素点。
w0指前景像素个数;
w1指背景像素个数;
u0指前景加权平均,即
temp=0;
for i=1:t
temp=temp+i*hist(i)
end
u0=temp/w0;
其中w0=hist(0)+hist(1)+……+hist(t)
hist指各个灰度级上的像素个数
u1对应是背景加权平均
u对应整幅图的加权平均
u=u0*w0+u1*w1.(*)
大津法的结果指使得g最大的t值:
g=w0*(u0-u)^2+w1*(u1-u)^2=w0*w1*(u1-u0)^2 (代入*式可得)
实现代码:
function level = Mygraythresh(I)
%GRAYTHRESH Global image threshold using Otsu's method.
% LEVEL = Mygraythresh(I) computes a global threshold (LEVEL) that can be
% used to convert an intensity image to a binary image with IM2BW. LEVEL
% is a normalized intensity value that lies in the range [0, 1].
% GRAYTHRESH uses Otsu's method, which chooses the threshold to minimize
% the intraclass variance of the thresholded black and white pixels.
%
% Example
% -------
% I = imread('coins.png');
% level = Mygraythresh(I);
% BW = im2bw(I,level);
% figure, imshow(BW)
%
% See also IM2BW.
%I=rgb2gray(I);
I=im2uint8(I(:));
depth=256;
counts=imhist(I,depth);
w=cumsum(counts);
ut=counts .* (1:depth)';
u=cumsum(ut);
MAX=0;
level=0;
for t=1:depth
u0=u(t,1)/w(t,1);
u1=(u(depth,1)-u(t,1))/(w(depth,1)-w(t,1));
w0=w(t,1);
w1=w(depth,1)-w0;
g=w0*w1*(u1-u0)*(u1-u0);
if g > MAX
MAX=g;
level = t;
end
end
level=level/256;
实验结果与matlab自带的graythresh略有差别。
- 根据OSTU大津法使用Matlab实现数字图像处理segmentation的graythresh函数
- matlab graythresh()函数使用的注意点
- Matlab ones()[ zeros() ] & rgb2gray() & graythresh() & im2bw() 函数的使用
- matlab中的graythresh函数的实例
- Matlab中的graythresh函数
- 数字图像处理之OSTU二值化
- Matlab之graythresh()函数详解
- 数字图像处理的MATLAB实现[1]
- 数字图像处理的MATLAB实现[2]
- 《数字图像处理的MATLAB实现》读书报告
- 数字图像处理--几个MATLAB函数
- [Matlab]数字图像处理实验和Matlab的使用
- 使用 matlab 数字图像处理(三)—— 实现图像的旋转(不使用 imrotate)
- Matlab数字图像处理的基础
- 基于MATLAB数字图像处理的快速傅里叶变换编程实现
- [数字图像处理]常见噪声的分类与Matlab实现
- 数字图像处理,常见噪声的分类与Matlab实现
- [数字图像处理]常见噪声的分类与Matlab实现
- 推荐引擎基本知识
- mysql之删除重复数据
- 新手学习ad hoc网络
- linux下利用valgrind工具进行内存泄露检测和性能分析
- android如何浏览并选择图片 音频 视频 转
- 根据OSTU大津法使用Matlab实现数字图像处理segmentation的graythresh函数
- vm下linux的常见问题
- Android编译问题总结
- 在CSDN上DOWN的一篇文章,放到这里以供参考:一个程序员对学弟学妹建议
- LINUX常用命令
- JQuery 实现图片切换
- 探究gc
- Objective-C block
- Invalidate()函数