大家好,新年快乐 我正在为otsu方法编写我的matlab代码但是我有错误,如果有人可以查看我的代码并建议我进行一些更正,我将非常感激。 这是我的代码:
im1=imread('cameraman.tif');
%im1=rgb2gray(im1);
[n,m]=size(im1);
hst=imhist(im1);
mu=0;
N=0;
for i=1:255
N=N+hst(i);
end
% The total mean level of the original image
for i=1:255
mu(i)=mu(i)+((i.*hst(i))./N);
end
for T=1:254
qb=0;
muT=0;
qo=0;
for i=1:T
qb=qb+(hst(i)./N); % probability of class occurence (background)
m=m+((i.*hst(i))./N);% probability of class mean (background)
end
for i=T+1:255
qo=qo+(hst(i)./N);% probability of class occurence (object)
end
sigma(T)=((mu-(qb*muT))^2)/(qb*qo)
end
[Y,T] = max(sigma)
[n,m]=size(im1);
for i=1:n
for j=1:m
if im1(i,j)>T
im(i,j)=1;
else
im(i,j)=0;
end
end
end
figure(1);
subplot(1,3,1);
imshow(im1);
subplot(1,3,2);
imhist(im1);
subplot(1,3,3);
imshow(im);
提前致谢
答案 0 :(得分:1)
更改了导致错误的两行,请测试:
mu=zeros(255,1);
代替mu=0;
sigma(T)=((mu(T)-(qb*muT))^2)/(qb*qo);
代替sigma(T)=((mu-(qb*muT))^2)/(qb*qo);
答案 1 :(得分:0)
感谢lennon310的帮助,我发现了我的错误 我的代码希望它能帮助其他人:
clc
clear all
close all
im1=imread('cameraman.tif');
%im1=rgb2gray(im1);
[n,m]=size(im1);
hst=imhist(im1);
m=0;
mu=0;
N=0;
for i=1:255
end
% The total mean level of the original image
for i=1:255
N=N+hst(i);
m=m+(i.*hst(i));
end
mu=m/N;
for T=1:254
qb=0;
muT=0;
qo=0;
mT=0;
shis=0;
for i=1:T
qb=qb+hst(i) % probability of class occurence (background)
mT=mT+(i.*hst(i));% probability of class mean (background)
end
muT=mT/N;
qb=qb/N;
for i=T+1:255
qo=qo+hst(i);% probability of class occurence (object)
end
qo=qo/N;
sigma(T)=(((mu*qb)-muT))^2/(qb*qo)
end
[Y,T] = max(sigma)
[n,m]=size(im1);
for i=1:n
for j=1:m
if im1(i,j)>T
im(i,j)=1;
else
im(i,j)=0;
end
end
end
figure(1);
subplot(1,3,1);
imshow(im1);
subplot(1,3,2);
imhist(im1);
subplot(1,3,3);
imshow(im);