-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreImg.m
More file actions
70 lines (63 loc) · 2.23 KB
/
preImg.m
File metadata and controls
70 lines (63 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function result = preImg(file)
%% read the image file
if strcmp(file(end-2:end), 'png')
[img, map] = imread(file);
img = ind2gray(img,map);
else
img = imread(file);
img = rgb2gray(img);
end
%% preprocess the image
% turn to binary image
img = im2bw(img);
% make the background black and numbers white
if sum(img(:)) > numel(img)*0.5
img = ~img;
end
% eliminate small objects on the image
ratio = size(img,1)/1000;
img = imresize(img, size(img)/ratio);
img = bwareaopen(img,300);
%% use connected component of a graph to find the segmentation
s = regionprops(img, 'BoundingBox');
result = zeros(50, 50, length(s));
for i = 1:length(s)
component = int32(s(i).BoundingBox);
separateImg = img(component(2):component(2)+component(4), component(1):component(1)+component(3));
Imgback = 255 * uint8(separateImg);
separateImgback = makeRec(Imgback);
result(:, :, i) = imresize(separateImgback, [50 50], 'nearest');
end
% add edge to every component of the image and reszie it to 28*28 to
% fit into neural network prediction
result = addedge(result);
%% show the figures of an image
% for i = 1: length(s)
% subplot(5, 5, i);
% tempImg = result(:, :, i);
% imshow(tempImg);
% end
end
function img = makeRec(img)
% turn the image into a rectangular image by adding zeros
% the length of the sides is decided by the longer one
[rowNum, columnNum] = size(img);
zeroNum = round(abs(rowNum-columnNum)/2);
if rowNum > columnNum
img = [zeros(rowNum,zeroNum) img zeros(rowNum,zeroNum)];
else
img = [zeros(zeroNum,columnNum); img; zeros(zeroNum,columnNum)];
end
end
function newImgs = addedge(imgs)
% add an 50% margin to each side of the image
zeroline = zeros(15, 50);
zerocolumn = zeros(80, 15);
newImgs = zeros(28, 28, size(imgs,3));
for i = 1:size(imgs, 3)
temp = [zeroline;imgs(:,:,i);zeroline];
temp = abs(imresize([zerocolumn temp zerocolumn], [28 28]));
%normalize to match the training data
newImgs(:,:,i) = sqrt(temp/(max(temp(:)) - min(temp(:))));
end
end