tic
clc;
clear all;
%read picture
%height and width
img_rgb=imread('./pic/number0.bmp');
h=size(img_rgb,1);
w=size(img_rgb,2);
%get rgb picture
figure(1);
subplot(121);
imshow(img_rgb);
title('rgb picture');
%get gray picture
% Relized method:myself Algorithm realized
% Y = ( R*77 + G*150 + B*29) >>8
% Cb = (-R*44 - G*84 + B*128) >>8
% Cr = ( R*128 - G*108 - B*20) >>8
img_rgb=double(img_rgb);
img_y=zeros(h,w);
img_u=zeros(h,w);
img_v=zeros(h,w);
for i = 1 : h
for j = 1 : w
img_y(i,j) = bitshift(( img_rgb(i,j,1)*77 + img_rgb(i,j,2)*150 + img_rgb(i,j,3)*29),-8);
img_u(i,j) = bitshift((-img_rgb(i,j,1)*44 - img_rgb(i,j,2)*84 + img_rgb(i,j,3)*128 + 32678),-8);
img_v(i,j) = bitshift(( img_rgb(i,j,1)*128 - img_rgb(i,j,2)*108 - img_rgb(i,j,3)*20 + 32678),-8);
end
end
img_y = uint8(img_y);
img_u = uint8(img_u);
img_v = uint8(img_v);
img_rgb = uint8(img_rgb);
subplot(122);
imshow(img_y);
title('gray picture');
%get bin picture
img_y=double(img_y);
img_thresh=zeros(h,w);
img_thresh_inverse=zeros(h,w);
THRESH_HOLD=150;
for i=1:h
for j=1:w
if(i>=384-56 && i<=384+55 && j>=512-56 && j<=512+55)
if(img_y(i,j)>THRESH_HOLD)
img_thresh(i,j)=255;
img_thresh_inverse(i,j)=0;
else
img_thresh(i,j)=0;
img_thresh_inverse(i,j)=255;
end
else
img_thresh(i,j)=img_y(i,j);
img_thresh_inverse(i,j)=img_y(i,j);
end
end
end
img_y=uint8(img_y);
img_thresh=uint8(img_thresh);
img_thresh_inverse=uint8(img_thresh_inverse);
figure(2);
subplot(121);
imshow(img_thresh);
title('bin picture');
subplot(122);
imshow(img_thresh_inverse);
title('bin inverse picture');
%open operation:corrosion and expansion
img_corrosion=zeros(h,w);
for i=1:h
for j=1:w
if(i>=384-56 && i<=384+55 && j>=512-56 && j<=512+55)
a=img_thresh_inverse(i-1,j-1)&img_thresh_inverse(i-1,j)&img_thresh_inverse(i-1,j+1);
b=img_thresh_inverse(i,j-1)&img_thresh_inverse(i,j)&img_thresh_inverse(i,j+1);
c=img_thresh_inverse(i+1,j-1)&img_thresh_inverse(i+1,j)&img_thresh_inverse(i+1,j+1);
if((a&b&c)==1)
img_corrosion(i,j)=255;
else
img_corrosion(i,j)=0;
end
else
img_corrosion(i,j)=img_thresh_inverse(i,j);
end
end
end
img_corrosion=uint8(img_corrosion);
figure(3);
subplot(121);
imshow(img_corrosion);
title('corrosion picture');
img_expansion=zeros(h,w);
for i=1:h
for j=1:w
if(i>=384-55 && i<=384+54 && j>=512-55 && j<=512+54)
a=img_corrosion(i-1,j-1)|img_corrosion(i-1,j)|img_corrosion(i-1,j+1);
b=img_corrosion(i,j-1)|img_corrosion(i,j)|img_corrosion(i,j+1);
c=img_corrosion(i+1,j-1)|img_corrosion(i+1,j)|img_corrosion(i+1,j+1);
if((a|b|c)==1)
img_expansion(i,j)=255;
else
img_expansion(i,j)=0;
end
else
img_expansion(i,j)=img_corrosion(i,j);
end
end
end
img_expansion=uint8(img_expansion);
subplot(122);
imshow(img_expansion);
title('expansion picture');
%get number matrix
test=imread('./pic/output_file1.bmp');
number=zeros(28,28);
for i=1:28
for j=1:28
number(i,j)=sum(sum(test(384-56+4*(i-1):384-56+4*i-1 , 512-56+4*(j-1):512-56+4*j-1)))/16;
end
end
number=floor(number);
number=uint8(number);
pic=load('./pic/pic_28x28.txt');
pic=reshape(pic,28,28);
pic=pic';
pic=uint8(pic);
figure(4);
subplot(121);
imshow(number)
title('matlab number');
subplot(122);
imshow(pic)
title('vivado number');
%use softmax to recog the number
load('weight');
number=double(number);
number=number';
number=reshape(number,784,1);
v=W*number;
v=v/max(v)*10;
y=softmax(v);
n=0;
for i=1:10
if(y(i)>=n)
n=y(i);
number_recog=i;
end
if(number_recog==10)
number_recog=0;
end
end
fprintf('the probability is %f\n',n*100);
fprintf('the number is %d\n',number_recog);
toc