-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageTo20x20Gray.m
70 lines (66 loc) · 2.88 KB
/
imageTo20x20Gray.m
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 vectorImage = imageTo20x20Gray(fileName, cropPercentage)
%IMAGETO20X20GRAY display reduced image and converts for digit classification
%
% Sample usage:
% imageTo20x20Gray('myDigit.jpg', 100);
%
% First parameter: Image file name
% Could be bigger than 20 x 20 px, it will
% be resized to 20 x 20. Better if used with
% square images but not required.
%
% Second parameter: cropPercentage (any number between 0 and 100)
% 0 0% will be cropped (optional, no needed for square images)
% 50 50% of available croping will be cropped
% 100 crop all the way to square image (for rectangular images)
%
%
% (Thanks to Edwin Frühwirth for parts of this code)
% Read as RGB image
Image3DmatrixRGB = imread(fileName);
% Convert to NTSC image (YIQ)
Image3DmatrixYIQ = rgb2ntsc(Image3DmatrixRGB );
% Convert to grays keeping only luminance (Y) and discard chrominance (IQ)
Image2DmatrixBW = Image3DmatrixYIQ(:,:,1);
% Get the size of your image
oldSize = size(Image2DmatrixBW);
% Obtain crop size toward centered square (cropDelta)
% ...will be zero for the already minimum dimension
% ...and if the cropPercentage is zero,
% ...both dimensions are zero
% ...meaning that the original image will go intact to croppedImage
cropDelta = floor((oldSize - min(oldSize)) .* (cropPercentage/100));
% Compute the desired final pixel size for the original image
finalSize = oldSize - cropDelta;
% Compute each dimension origin for croping
cropOrigin = floor(cropDelta / 2) + 1;
% Compute each dimension copying size
copySize = cropOrigin + finalSize - 1;
% Copy just the desired cropped image from the original B&W image
croppedImage = Image2DmatrixBW( ...
cropOrigin(1):copySize(1), cropOrigin(2):copySize(2));
%% Resolution scale factors: [rows cols]
scale = [28 28] ./ finalSize; % Brojeve menjati u zavisnosti od pixela
%% Compute back the new image size (extra step to keep code general)
newSize = max(floor(scale .* finalSize),1);
% Compute a re-sampled set of indices:
rowIndex = min(round(((1:newSize(1))-0.5)./scale(1)+0.5), finalSize(1));
colIndex = min(round(((1:newSize(2))-0.5)./scale(2)+0.5), finalSize(2));
% Copy just the indexed values from old image to get new image
newImage = croppedImage(rowIndex,colIndex,:);
% Invert black and white
invertedImage = - newImage;
% Find min and max grays values in the image
maxValue = max(invertedImage(:));
minValue = min(invertedImage(:));
% Compute the value range of actual grays
delta = maxValue - minValue;
% Normalize grays between 0 and 1
normImage = (invertedImage - minValue) / delta;
% Add contrast. Multiplication factor is contrast control.
contrastedImage = sigmoid((normImage -0.5) * 5);
% Show image as seen by the classifier
imshow(contrastedImage, [-1, 1] );
% Output the matrix as a unrolled vector
vectorImage = reshape(contrastedImage, 1, newSize(1)*newSize(2));
end