📄 digit_recognition.m
字号:
%-----------------------------------------------------------------
%Digit_Recognition.m Developed by Rentian Huang,
%Hope University,Distribute System Email:10076507@hope.ac.uk
%-----------------------------------------------------------------
clear all;
p(1:256,1)=1;
load digit net; %Load the BP Neural Networks which been trained
test=input('Please input a digits image:','s'); %Prompt for input digit image for recognize
s=input('Please input the number of digits:'); %Prompt for input number of digits
x=imread(test,'bmp'); %read the input image
xbw=im2bw(x,0.9); %Convert the image to Black and White image
xbw=medfilt2(xbw);%Use medium filter if needed
bw=xbw;
result='';
%The loops for image segmentation
for n=0:s-1
p1=ones(16,16); %Create an 16*16 Black and whit image (all white)
[c,l]=size(bw); %Get the size of input image
[i,j] = find(bw==0); %Find out the rows and columns which value is 0
jmin=min(j); %Find out the smallest column which value is 0
xbwt=bw(:,jmin:l); %Cut the image on the left of the image column which value is 1
[c,l]=size(xbwt); %Get the image size after cuting
[i,j]=find(all(xbwt(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
xbwn{n+1}=xbwt(:,1:jmin-1); %Cut number n digit image
bw=xbwt(:,jmin:l); %Save new image after cutting number n digit image
[i,j] = find(xbwn{n+1}==0); %Find out all the rows and columns in number n image
imin=min(i); %Find out the smallest row which value is 0
imax=max(i); %Find out the largest row which value is 0
xbwn1{n+1}=xbwn{n+1}(imin:imax,:); %Keep the number n image after cutting the 0 rows
rate=16/max(size(xbwn1{n+1})); %Calculate the rate between number n image and 16*16 image
xbwn1{n+1}=imresize(xbwn1{n+1},rate); %Convert the image to 16*16 size
[i,j]=size(xbwn1{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)=xbwn1{n+1}; %Convert the image to standard 16*16 image
xp(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
for m=0:15
p(m*16+1:(m+1)*16,1)=p1(1:16,m+1); %Form the vector of input digit image to the BP NN
end
[a,Pf,Af] = sim(net,p); %BP NN simulate
b=round(a);%The output digit from the network
if b<0
b=b+1;
elseif b>9;
b=b-1;
end
result=strcat(result,num2str(b)); %Display the output of the NN
end
imshow(xp); %Display the input image
title('The input Numbers image:')
text(0,60,strcat('Recognition Result:',result),'FontSize',10);%Display the results
%--------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -