www.pudn.com > codebar.rar > imagefilter.m


%%% 
%%function [Iout] = imagefilter(Iin,width,height) 
function [left_t,up_t,right_t,down_t] = imagefilter(Iin,width,height) 
%flag = false(height,width); 
T=30;%15; 
for i=1:height 
    b_count = 0; 
    wdot = 1;%白点坐标记录 
    for j=1:width 
     	if(Iin(i,j) == 0)               
            b_count = b_count+1; 
        end 
        if(Iin(i,j) == 1)%一旦出现白点,判断之前黑点个数,如果大于某个值,则标志清除 
            if(b_count > T) 
                for k=wdot:(j-1) 
                    %flag(i,k) = true;%对前面的点标志为白点 
                    Iin(i,k) = 1; 
                end 
            end 
            b_count = 0;%清0,重新计数 
            wdot = j; 
        elseif(width == j) 
            if(b_count > T) 
                for k=wdot:j 
                    %flag(i,k) = true;%对前面的点标志为白点 
                    Iin(i,k) = 1; 
                end 
            end 
        end 
    end 
end 
%figure,imshow(Iin),title('去掉大片黑点后图像'); 
%%%----------------------------剪切上下区域-------------------------%%%% 
%figure, imshow(Iout); 
%[height,width]=size(Iout); 
pixelH = zeros(height,1); 
 
for i=1:height 
    for j=1:width 
        if(Iin(i,j) == 0)%是黑点 
            %pixelV(j,1)=pixelV(j,1)+1;%垂直投影 
            pixelH(i,1)=pixelH(i,1)+1;%水平投影 
            %pixelPos(i,1)=i; 
        end 
    end 
end 
pixel_ave = max(pixelH); 
pixelPos=zeros(height,1); 
cout1 = height; 
R3 = 0.9;%0.8->0.9 
index = 1; 
for j=1:height 
   	tmp = pixelH(j,1)/pixel_ave; 
  	if(tmp>R3) 
      	%pixelH(index,1)=pixelH(j,1); 
       	pixelPos(index,1)=j;%pixelPos(j,1); 
       	index = index+1; 
    end 
end 
up_t = min(pixelPos(1:(index-1))); 
down_t =max(pixelPos(1:(index-1))); 
I2 = imcrop(Iin,[1 up_t width down_t-up_t]);%I 
figure,imshow(I2),title('上下区域被剪切后'); 
 
%%%----------------------------剪切左右区域-------------------------%%%% 
[height,width]=size(I2); 
leftdot=ones(height,1);   %左边要剪切的位置 
rightdot=ones(height,1);  %右边要剪切的位置 
T2 = 40;%一般条形码的最左边和最右边有一定宽度的白条,假定为30个像素 
%whitenum=0; 
for i=1:height 
    wnum = 0; 
    blackdot = 1; 
    left = true;%每一行开始时令白宽为假; 
    whitenum=0; 
    for j=1:width 
        if(I2(i,j)==1) 
            wnum = wnum+1;  %统计白点 
        end 
        if((j == width)&&(whitenum<2)) 
            rightdot(i,1)=blackdot; 
        end 
        if(I2(i,j)==0)     %遇到了黑点 
            if(wnum > T2)   %白点连续40个说明这里就是白条 
                if(left==true) 
                    leftdot(i,1) = j; 
                    left = false;%左边点不要重新叠加了 
                end 
                whitenum=whitenum+1;%%左边的条框被发现 
                rightdot(i,1)=blackdot;%记录大白宽条前一个黑点 
            end 
            blackdot = j;   %记录该黑点位置 
            wnum = 0;       %重新置零 
        end 
    end 
end 
%%%----------------------------干扰点过滤-------------------------%%%% 
R1 = [0.05 0.1 0.2 0.4 0.7 0.8 0.9];%0.30.5如果很多个1,很少个百,1或百有可能被排除掉 
R2 = [1.95 1.9 1.8 1.6 1.3 1.2 1.1];%1.7 1.5  
%leftdot重点要考虑小值,照顾到1这一边 
left_ave = mean(leftdot); 
cout1 = height; 
for i=1:length(R1) 
    index1 = 1; 
    for j=1:cout1 
        lratio = leftdot(j,1)/left_ave; 
        if((lratio>R1(i))&&(lratioK1(i))&&(rratio