close all
I = imread('C:\Users\Administrator\Desktop\001.bmp');
A=size(I);
if length(A)==3
J=rgb2gray(I);
else
J=I;
end
figure(1)
imshow(J);
title('灰度化图像 ');
[e1,e2]=size(J);
Im=imcrop(J,[820,980,800,400]); 缺点:人工确定条形码所在范围区间
figure(2)
subplot(1,2,1),imshow(Im)
title('中心区域 ');
subplot(1,2,2),imhist(Im)
title('中心区域直方图');
[xa,ya]=size(Im);
b=double(Im);
zd=double(max(Im)) ;
zx=double(min(Im)) ;
T=double((zd+zx))/2;
count=double(0);
while 1
count=count+1;
S0=0.0; n0=0.0;
S1=0.0; n1=0.0;
for i=1:xa
for j=1:ya
if double(Im(i,j))>=T
S1=S1+double(Im(i,j));
n1=n1+1;
else
S0=S0+double(Im(i,j));
n0=n0+1;
end
end
end
T0=S0/n0;
T1=S1/n1;
if abs(T-((T0+T1)/2))<0.1
break;
else
T=(T0+T1)/2;
end
end
count
T
K=find(Im<T);
Im(K)=0;
K=find(Im>=T); <