Skip to content

Instantly share code, notes, and snippets.

@JakubMifek
Created January 10, 2018 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakubMifek/12146168a3ca4871fa6a35a3ec08e87e to your computer and use it in GitHub Desktop.
Save JakubMifek/12146168a3ca4871fa6a35a3ec08e87e to your computer and use it in GitHub Desktop.
Matlab code for creating ascii art
[input net] = CreateNetwork('alphabet2.png', 17, 6);
characterLength = size(net.LW{1,1}, 1);
output = [' ', '1', 'B', 'S', 'd', 'u', '!', '2', 'C', 'T', 'e', 'v', '"', '3', 'D', 'U', 'f', 'w', '#', '4', 'E', 'V', 'g', 'x', '$', '5', 'F', 'W', 'h', 'y', '%', '6', 'G', 'X', 'i', 'z', '&', '7', 'H', 'Y', 'j', '{', "'", '8', 'I', 'Z', 'k', '|', '(', '9', 'J', '[', 'l', '}', ')', ':', 'K', '\', 'm', '~', '*', ';', 'L', ']', 'n', '+', '<', 'M', '^', 'o', ',', '=', 'N', '_', 'p', '-', '>', 'O', '`', 'q', '.', '?', 'P', 'a', 'r', '/', '@', 'Q', 'b', 's', '0', 'A', 'R', 'c', 't'];
%output = [' ', '1', 'u', '!', '2', 'v', '"', '3', 'w', '#', '4', 'x', '$', '5', '{', '%', '6', '|', '&', '7', '}', "'", '8', '~', '(', '9', '[', ')', ':', '\', '*', ';', ']', '+', '<', '^', ',', '=', '_', '-', '>', '`', '.', '?', 'a', '/', '@', 'b', '0', 'A', 'c'];
%output = [' ', '|', '!', '}', '"', '~', '#', '[', '$', '\', '%', ']', '&', '^', "'", '_', '(', '`', ')', ':', '*', ';', '+', '<', ',', '=', '-', '>', '.', '?','/', '@', '0', '{'];
ratio = 11/21; % width / height of a character
fontSize = 15
tileW = round(fontSize * ratio)
tileH = round(fontSize)
img = imread('PortraitBig.png');
img = 1-im2double(img(:,:,1));
img = img == 1;
img = 1-img;
oTw = tileW;
% change the tileW and tileH so that it corresponds to the size of a
% character in the network
tileH = round(sqrt(characterLength/ratio))
tileW = round(tileH * ratio)
% Resize the image so that we have each tile of the size of input
% vector in the network
%img = imresize(img, tileW/oTw);
%img = img > 0.5;
% We add multiple rows and columns on the bottom and right side of the
% image so that the image is devidable by our tile height and width
m = mod(size(img,1), tileH);
if m > 0
img = [img; ones(tileH - m, size(img, 2))];
end
m = mod(size(img,2), tileW);
if m > 0
img = [img ones(size(img,1), tileW - m)];
end
% Now we split the image into rectangles of our tiles width and height and
% transform the rectangles into column vectors
cols = (im2col(img, [tileH tileW],'distinct'));
cols = cols * 2 - 1;
% Next we use the created network and transform each tile into a letter of
% our alphabet
characters = char(zeros(size(cols, 2), 1));
perc = 0;
set = ceil(size(characters, 1) / 100);
h = waitbar(0,'Initializing waitbar...');
for i = 1:size(cols, 2)
if mod((i - 1), set) == 0
waitbar(((i - 1) / set) / 100, h, sprintf('%d%% done...',(i - 1) / set))
end
X = cols(:,i);
Ai = {X};
[Y,Pf,Af] = net(cell(1, characterLength), {}, Ai);
Y = round((Y{end} + 1) / 2);
lastError = size(input, 1);
lastPos = 1;
for j = 1:size(output, 2)
error = GetOutputError(Y, input(:, j));
if error < lastError
lastError = error;
lastPos = j;
end
end
characters(i) = output(lastPos);
end
close(h);
% Now we transform the output into a matrix
characters = reshape(characters, [size(img, 1) / tileH, size(img, 2) / tileW]);
dlmwrite('test.txt', characters, '', '');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment