www.pudn.com > dcut.rar > dcut.m


%功能——分割图像字符并边缘提取(二值化) 
clear,close all; 
clc; 
text=input('请输入要处理的数字图像名:','s'); 
s=input('请输入要处理的数字图像的字符个数:'); 
I=imread(test,'jpg'); 
Ibw=im2bw(I,graythresh(I)); 
Ibw=medfilt2(Ibw);  
bw=~Ibw; 
for n=0:s-1 
    p1=ones(16,16); 
    [c,l]=size(bw);                      
    [i,j] = find(bw==0);                       % 
    jmin=min(j); 
    Ibwt=bw(:,jmin:l);                         %Cut the image on the left of the image column which value is 1 
    [c,l]=size(Ibwt);                          %剪切后图像大小 
    [i,j]=find(all(Ibwt(1:c,:)==1));           %Find out the rows and columns which value is 1 
    jmin=min(j);                               %Find out the smallest column in the new image 
    Ibwn{n+1}=Ibwt(:,1:jmin-1);                %Cut number n digit image 
    bw=Ibwt(:,jmin:l);                         %保存剪掉第n个字符的图像 
    [i,j] = find(Ibwn{n+1}==0);                %找到第n个字符图像的行和列 
    imin=min(i);                               %行数值为0的最小行 
    imax=max(i);                               %列数值为0的最小列 
    Ibwn1{n+1}=Ibwn{n+1}(imin:imax,:);         %K得到剪掉0值行的第n个字符图像eep the number n image after cutting the 0 rows 
    rate=16/max(size(Ibwn1{n+1}));             %Calculate the rate between number n image and 16*16 image 
    Ibwn1{n+1}=imresize(Ibwn1{n+1},rate);      %Convert the image to 16*16 size 
    [i,j]=size(Ibwn1{n+1});                    %The size of number n digit 
    i1=round((16-i)/2);                        %Calculate the row size different between image and 16 
    j1=round((16-j)/2);                        %Calculate the column size different between image and 16 
    p1(i1+1:i1+i,j1+1:j1+j)=Ibwn1{n+1};        %归一化Convert the image to standard 16*16 image 
    Ip(1:16,n*16+1:(n+1)*16)=p1;               %Save the number n digit image 
    p1=-1.*p1+ones(16,16);                     %Reverse the value 0 and 1 with each other in the image 
end 
for i=0:s-1                                    %sobel边缘提取 
    Ibwne{i}=edge(Ibwn{i},'sobel'); 
end