Guest User

Untitled

a guest
Jan 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. [input net] = CreateNetwork('alphabet2.png', 17, 6);
  2.  
  3. characterLength = size(net.LW{1,1}, 1);
  4.  
  5. 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'];
  6. %output = [' ', '1', 'u', '!', '2', 'v', '"', '3', 'w', '#', '4', 'x', '$', '5', '{', '%', '6', '|', '&', '7', '}', "'", '8', '~', '(', '9', '[', ')', ':', '\', '*', ';', ']', '+', '<', '^', ',', '=', '_', '-', '>', '`', '.', '?', 'a', '/', '@', 'b', '0', 'A', 'c'];
  7. %output = [' ', '|', '!', '}', '"', '~', '#', '[', '$', '\', '%', ']', '&', '^', "'", '_', '(', '`', ')', ':', '*', ';', '+', '<', ',', '=', '-', '>', '.', '?','/', '@', '0', '{'];
  8.  
  9. ratio = 11/21; % width / height of a character
  10. fontSize = 15
  11. tileW = round(fontSize * ratio)
  12. tileH = round(fontSize)
  13.  
  14. img = imread('PortraitBig.png');
  15. img = 1-im2double(img(:,:,1));
  16. img = img == 1;
  17. img = 1-img;
  18.  
  19. oTw = tileW;
  20.  
  21. % change the tileW and tileH so that it corresponds to the size of a
  22. % character in the network
  23. tileH = round(sqrt(characterLength/ratio))
  24. tileW = round(tileH * ratio)
  25.  
  26. % Resize the image so that we have each tile of the size of input
  27. % vector in the network
  28. %img = imresize(img, tileW/oTw);
  29. %img = img > 0.5;
  30.  
  31. % We add multiple rows and columns on the bottom and right side of the
  32. % image so that the image is devidable by our tile height and width
  33.  
  34. m = mod(size(img,1), tileH);
  35. if m > 0
  36. img = [img; ones(tileH - m, size(img, 2))];
  37. end
  38.  
  39. m = mod(size(img,2), tileW);
  40. if m > 0
  41. img = [img ones(size(img,1), tileW - m)];
  42. end
  43.  
  44. % Now we split the image into rectangles of our tiles width and height and
  45. % transform the rectangles into column vectors
  46.  
  47. cols = (im2col(img, [tileH tileW],'distinct'));
  48. cols = cols * 2 - 1;
  49.  
  50. % Next we use the created network and transform each tile into a letter of
  51. % our alphabet
  52.  
  53. characters = char(zeros(size(cols, 2), 1));
  54. perc = 0;
  55. set = ceil(size(characters, 1) / 100);
  56.  
  57. h = waitbar(0,'Initializing waitbar...');
  58.  
  59. for i = 1:size(cols, 2)
  60.  
  61. if mod((i - 1), set) == 0
  62. waitbar(((i - 1) / set) / 100, h, sprintf('%d%% done...',(i - 1) / set))
  63. end
  64.  
  65. X = cols(:,i);
  66. Ai = {X};
  67. [Y,Pf,Af] = net(cell(1, characterLength), {}, Ai);
  68. Y = round((Y{end} + 1) / 2);
  69.  
  70. lastError = size(input, 1);
  71. lastPos = 1;
  72.  
  73. for j = 1:size(output, 2)
  74. error = GetOutputError(Y, input(:, j));
  75. if error < lastError
  76. lastError = error;
  77. lastPos = j;
  78. end
  79. end
  80.  
  81. characters(i) = output(lastPos);
  82. end
  83.  
  84. close(h);
  85.  
  86. % Now we transform the output into a matrix
  87. characters = reshape(characters, [size(img, 1) / tileH, size(img, 2) / tileW]);
  88.  
  89. dlmwrite('test.txt', characters, '', '');
Add Comment
Please, Sign In to add comment