[matlab图像处理] 阈值分割
2014-03-11 15:50
274 查看
%迭代式阈值分割 otsu阈值分割 二值化
close all;%关闭所有窗口
clear;%清除变量的状态数据
clc;%清除命令行
I=imread('rice.png');
subplot(2,2,1);
imshow(I);
title('1 rice的原图');
%迭代式阈值分割
zmax=max(max(I));%取出最大灰度值
zmin=min(min(I));%取出最小灰度值
tk=(zmax+zmin)/2;
bcal=1;
[m,n]=size(I);
while(bcal)
%定义前景和背景数
iforeground=0;
ibackground=0;
%定义前景和背景灰度总和
foregroundsum=0;
backgroundsum=0;
for i=1:m
for j=1:n
tmp=I(i,j);
if(tmp>=tk)
%前景灰度值
iforeground=iforeground+1;
foregroundsum=foregroundsum+double(tmp);
else
ibackground=ibackground+1;
backgroundsum=backgroundsum+double(tmp);
end
end
end
%计算前景和背景的平均值
z1=foregroundsum/iforeground;
z2=foregroundsum/ibackground;
tktmp=uint8((z1+z2)/2);
if(tktmp==tk)
bcal=0;
else
tk=tktmp;
end
%当阈值不再变化时,说明迭代结束
end
disp(strcat('迭代的阈值:',num2str(tk)));%在command window里显示出 :迭代的阈值:阈值
newI=im2bw(I,double(tk)/255);%函数im2bw使用阈值(threshold)变换法把灰度图像(grayscale image)
%转换成二值图像。所谓二值图像, 一般意义上是指只有纯黑(0)、纯白(255)两种颜色的图像。
%语法
%BW = im2bw(I, level)
%BW = im2bw(X, map, level)
%BW = im2bw(RGB, level)
%其中level就是设置阈值的。level取值范围[0, 1]。
subplot(2,2,2);
imshow(newI);
title('2 rice的迭代法分割效果图');
%otsu阈值分割
bw=graythresh(I);
disp(strcat('otsu阈值分割的阈值:',num2str(bw*255)));%在command window里显示出 :迭代的阈值:阈值
newII=im2bw(I,bw);
subplot(2,2,3);
imshow(newII);
title('3 rice的otsu阈值分割');
%二值化 阈值为135
[width,height,bmsize]=size(I);
for i=1:width
for j=1:height
if I(i,j)>135
I(i,j)=255;
else
I(i,j)=0;
end
end
end
subplot(2,2,4);
imshow(I);
title('4 rice的二值阈值分割');
close all;%关闭所有窗口
clear;%清除变量的状态数据
clc;%清除命令行
I=imread('rice.png');
subplot(2,2,1);
imshow(I);
title('1 rice的原图');
%迭代式阈值分割
zmax=max(max(I));%取出最大灰度值
zmin=min(min(I));%取出最小灰度值
tk=(zmax+zmin)/2;
bcal=1;
[m,n]=size(I);
while(bcal)
%定义前景和背景数
iforeground=0;
ibackground=0;
%定义前景和背景灰度总和
foregroundsum=0;
backgroundsum=0;
for i=1:m
for j=1:n
tmp=I(i,j);
if(tmp>=tk)
%前景灰度值
iforeground=iforeground+1;
foregroundsum=foregroundsum+double(tmp);
else
ibackground=ibackground+1;
backgroundsum=backgroundsum+double(tmp);
end
end
end
%计算前景和背景的平均值
z1=foregroundsum/iforeground;
z2=foregroundsum/ibackground;
tktmp=uint8((z1+z2)/2);
if(tktmp==tk)
bcal=0;
else
tk=tktmp;
end
%当阈值不再变化时,说明迭代结束
end
disp(strcat('迭代的阈值:',num2str(tk)));%在command window里显示出 :迭代的阈值:阈值
newI=im2bw(I,double(tk)/255);%函数im2bw使用阈值(threshold)变换法把灰度图像(grayscale image)
%转换成二值图像。所谓二值图像, 一般意义上是指只有纯黑(0)、纯白(255)两种颜色的图像。
%语法
%BW = im2bw(I, level)
%BW = im2bw(X, map, level)
%BW = im2bw(RGB, level)
%其中level就是设置阈值的。level取值范围[0, 1]。
subplot(2,2,2);
imshow(newI);
title('2 rice的迭代法分割效果图');
%otsu阈值分割
bw=graythresh(I);
disp(strcat('otsu阈值分割的阈值:',num2str(bw*255)));%在command window里显示出 :迭代的阈值:阈值
newII=im2bw(I,bw);
subplot(2,2,3);
imshow(newII);
title('3 rice的otsu阈值分割');
%二值化 阈值为135
[width,height,bmsize]=size(I);
for i=1:width
for j=1:height
if I(i,j)>135
I(i,j)=255;
else
I(i,j)=0;
end
end
end
subplot(2,2,4);
imshow(I);
title('4 rice的二值阈值分割');
相关文章推荐
- [matlab 图像处理] 阈值分割(直方图自己算)
- 【MATLAB图像处理】在MATLAB中图像的坐标以及rectangle的坐标
- 【Matlab图像处理】学习笔记:提取图片的R,G,B分量
- 图像处理(Matlab)-再次学习如何使用Matlab
- 转帖matlab_1_图像处理imfinfo_imshow_funtion
- MATLAB图像处理之imshow和imagesc
- [DIP] 数字图像处理 (MATLAB) CH04
- Matlab 图像处理基本操作
- Matlab图像处理学习笔记(一):二值化、开操作、连通区域提取、重心、ROI
- MATLAB图像处理_HSV与RGB颜色空间互转
- matlab 图像加入高斯噪声和平滑处理练习
- Matlab中如何读出写入图像文件以及对图像的简单处理
- WPF中调用matlab制作的dll进行图像处理
- 基于MATLAB图像处理的中值滤波、均值滤波以及高斯滤波的实现与对比
- matlab 高级函数 —— colfilt/blockproc (图像)矩阵的分块处理
- MATLAB图像处理基础
- Matlab 形态学图像处理
- 肺部胸片图像掩膜和伪彩色处理matlab
- Matlab 图像处理相关函数命令
- 图像边沿平滑处理的matlab实现