下面是一段otsu算法的matlab程序,各位大虾帮我看看错在哪里?我调试的时候,最后分割的结果是全白色的。
functionth=thresh_md(a);x=imread('e:\a02.jpg');a=rgb2gray(x);subplot(2,2,1);imshow(x)...
function th=thresh_md(a);
x=imread('e:\a02.jpg');
a=rgb2gray(x);
subplot(2,2,1);imshow(x)
subplot(2,2,2);imshow(a)
subplot(2,2,3);imhist(a)
count=imhist(a);
[m,n]=size(a);
N=m*n;
L=256;
f=count/N;
t=1:L;
w0=0;
u0=0;
w1=0;
u1=0;
for i=1:t
w0=w0+f(i);
end
w1=1-w0;
for i=1:t
if w0~=0
u0=u0+((i-1)*f(i)/w0);
end
end
for i=t+1:L
if w1~=0
u1=u1+((i-1)*f(i)/w1);
end
end
ut=w0*u0+w1*u1;
d=w0*(u0-ut)^2+w1*(u1-ut)^2;
[y,ty]=max(d); %可以取出数组的最大值及取最大值的点
th=ty-1;
for i=1:m
for j=1:n
if a(i,j)>th
a(i,j)=0;
else
a(i,j)=255;
end
end
end
subplot(2,2,4);imshow(a); 展开
x=imread('e:\a02.jpg');
a=rgb2gray(x);
subplot(2,2,1);imshow(x)
subplot(2,2,2);imshow(a)
subplot(2,2,3);imhist(a)
count=imhist(a);
[m,n]=size(a);
N=m*n;
L=256;
f=count/N;
t=1:L;
w0=0;
u0=0;
w1=0;
u1=0;
for i=1:t
w0=w0+f(i);
end
w1=1-w0;
for i=1:t
if w0~=0
u0=u0+((i-1)*f(i)/w0);
end
end
for i=t+1:L
if w1~=0
u1=u1+((i-1)*f(i)/w1);
end
end
ut=w0*u0+w1*u1;
d=w0*(u0-ut)^2+w1*(u1-ut)^2;
[y,ty]=max(d); %可以取出数组的最大值及取最大值的点
th=ty-1;
for i=1:m
for j=1:n
if a(i,j)>th
a(i,j)=0;
else
a(i,j)=255;
end
end
end
subplot(2,2,4);imshow(a); 展开
1个回答
展开全部
程序思路有点乱.。写成下面这样的,容易看!
image_1=imread('E:\ebook\lena.bmp'); %读入图片
image_1=rgb2gray(image_1);%灰度化
[m,n]=size(image_1);%计算图片的像素点个数,行列,n是列数,Gray
num=zeros(1,256);%存放各灰度级出现的次数
p=zeros(1,256);%存放各灰度级的比率
image_1=double(image_1);%双精度化
for i=1:m
for j=1:n
num(image_1(i,j)+1)=num(image_1(i,j)+1)+1;%统计各灰度级的像素点个数
end
end
for i=1:256
p(i)=num(i)/(m*n);%计算各灰度级出现的比率
end
for i=2:256
if p(i)~=0
st=i+1;%实现寻找出现比率不为0的最小灰度值
break
end
end
for i=256:-1:1
if p(i)~=0;
nd=i-1;%实现找出出现比率不为0的最大灰度值
break
end
end
%以下程序实现利用最小方差和法找出门阈值
w=inf; th=0;
for t=st:nd%最小非零比率灰度值到最大非零比率灰度值
qt1=0; qt2=0;%前景后景像素点比率
u1=0; u2=0;%前景后景均值
v1=0; v2=0;%
for i=1:t
qt1=qt1+p(i);
end
for i=1:t
u1=u1+i*p(i)/qt1;
end
for i=1:t
v1=v1+((i-u1)^2)*p(i)/qt1;
end
for i=t+1:256
qt2=qt2+p(i);
end
for i=t+1:256
u2=u2+i*p(i)/qt2;
end
for i=t+1:256
v2=v2+((i-u2)^2)*p(i)/qt2;
end
if qt1*v1+qt2*v2<w
th=t; w=qt1*v1+qt2*v2 ;
end
end
for i=1:m
for j=1:n
if (image_1(i,j)+1>th)
image_2(i,j)=255;
else
image_2(i,j)=0;
end
end
end
image_2=uint8(image_2);%读入读出变换
figure,imshow(image_2);%显示二值化后的图片
image_1=imread('E:\ebook\lena.bmp'); %读入图片
image_1=rgb2gray(image_1);%灰度化
[m,n]=size(image_1);%计算图片的像素点个数,行列,n是列数,Gray
num=zeros(1,256);%存放各灰度级出现的次数
p=zeros(1,256);%存放各灰度级的比率
image_1=double(image_1);%双精度化
for i=1:m
for j=1:n
num(image_1(i,j)+1)=num(image_1(i,j)+1)+1;%统计各灰度级的像素点个数
end
end
for i=1:256
p(i)=num(i)/(m*n);%计算各灰度级出现的比率
end
for i=2:256
if p(i)~=0
st=i+1;%实现寻找出现比率不为0的最小灰度值
break
end
end
for i=256:-1:1
if p(i)~=0;
nd=i-1;%实现找出出现比率不为0的最大灰度值
break
end
end
%以下程序实现利用最小方差和法找出门阈值
w=inf; th=0;
for t=st:nd%最小非零比率灰度值到最大非零比率灰度值
qt1=0; qt2=0;%前景后景像素点比率
u1=0; u2=0;%前景后景均值
v1=0; v2=0;%
for i=1:t
qt1=qt1+p(i);
end
for i=1:t
u1=u1+i*p(i)/qt1;
end
for i=1:t
v1=v1+((i-u1)^2)*p(i)/qt1;
end
for i=t+1:256
qt2=qt2+p(i);
end
for i=t+1:256
u2=u2+i*p(i)/qt2;
end
for i=t+1:256
v2=v2+((i-u2)^2)*p(i)/qt2;
end
if qt1*v1+qt2*v2<w
th=t; w=qt1*v1+qt2*v2 ;
end
end
for i=1:m
for j=1:n
if (image_1(i,j)+1>th)
image_2(i,j)=255;
else
image_2(i,j)=0;
end
end
end
image_2=uint8(image_2);%读入读出变换
figure,imshow(image_2);%显示二值化后的图片
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询