clear all
clc
load('MnistConv.mat');
I = imread('testnumber3.jpg');
% flipImage=flip(I,2);
% rotate=imrotate(flipImage,180);
image(I)
% Convert to grayscale image
Igray = double(rgb2gray(I));
% Convert to binary image
Ibw1 = im2bw(Igray,graythresh(Igray));
Ibw=(256-Igray)/256;
% Ibw = im2bw(Igray,graythresh(Igray));
% Edge detection
Iedge = edge(Ibw,'Roberts');
% Morphology
% * *Image Dilation*
se = strel('square',2);
Iedge2 = imdilate(Iedge, se);
% * *Image Filling*
Ifill= imfill(Iedge2,'holes');
[Ilabel num] = bwlabel(Ifill);
disp(num);
Iprops = regionprops(Ilabel);
Ibox = [Iprops.BoundingBox];
Ibox = reshape(Ibox,[4 num]);
% Plot the Object Location
hold on;
for cnt = 1:num
kotak=rectangle('position',Ibox(:,cnt),'edgecolor','r');
crop=imcrop(Ibw,Ibox(:,cnt));
% Resizing
imgLGE = imresize(crop, 5, 'bicubic');
X = abs(imresize(imgLGE, [28 28]));
for i=1:28
for j=1:28
if X(i,j)<0.
X(i,j)=0;
elseif X(i,j)>0.5
X(i,j)=X(i,j)+0;
end
end
end
k=1;
x = X(:, :, k); % Input, 28x28
y1 = Conv(x, W1); % Convolution, 20x20x20
y2 = ReLU(y1); %
y3 = Pool(y2); % Pool, 10x10x20
y4 = reshape(y3, [], 1); % 2000
v5 = W5*y4; % ReLU, 360
y5 = ReLU(v5); %
v = Wo*y5; % Softmax, 10
y(:,cnt) = Softmax(v); %
end
[max,result]=max(y);