Advertisement
Whiplash141

Whip's Image to String Converter

Dec 16th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.63 KB | None | 0 0
  1. clear; clc; close all;
  2. tic
  3.  
  4. %desired image size is 170x170
  5. image = imread('C:\Users\Joshua\Desktop\GIMP\Logos\cass.jpg');
  6.  
  7. %colorMap = [.2 .2 .2; .8 .8 .8; .8 .2 .2; .2 .8 .2; .2 .2 .8; .8 .8 .2; .2 .8 .8; .8 .2 .8; .4 .4 .4; .6 .6 .6];
  8.  
  9. %not really sure
  10. % colorMap = [0 0 0; 1 1 1; 1 0 0; 0 1 0; 0 0 1; 1 1 0; 0 1 1; 1 0 1;.25 .25 .25; .5 .5 .5; .75 .75 .75; 1 .5 0; .65 .15 .15];
  11. % colorStr = ['e';'w';'r';'g';'b';'y';'c';'p';'d';'m';'l';'o';'a'];
  12.  
  13. %grayscale
  14. % colorMap = [0 0 0; 1 1 1; .25 .25 .25; .5 .5 .5; .75 .75 .75];
  15. % colorStr = ['e';'w';'d';'m';'l'];
  16.  
  17. %color wheel
  18. colorMap = [0 0 0; 1 1 1; .25 .25 .25; .5 .5 .5; .75 .75 .75; 1 0 0; 0 1 0; 0 0 1; 1 .5 0; 1 1 0; 1 0 1];
  19. colorStr = ['e';'w';'d';'m';'l';'r';'g';'b';'o';'y';'p'];
  20.  
  21.  
  22.  
  23. imageConv = rgb2ind(image, colorMap, 'dither');
  24. imageRGB = ind2rgb(imageConv, colorMap);
  25.  
  26. [rows, columns] = size(imageConv);
  27.  
  28. charCount = 1;
  29. lastChar = -1;
  30.  
  31. charString = '';
  32. for row = 1:rows
  33.     for col = 1:columns
  34.        
  35.         thisChar = imageConv(row, col);
  36.         if col == 1
  37.             charCount = 1;
  38.         elseif thisChar == lastChar
  39.             charCount = charCount + 1;
  40.         elseif col~= 1
  41.             charString = strcat(charString, sprintf('%s%i;', colorStr(lastChar+1), charCount));
  42.             charCount = 1;
  43.         end
  44.  
  45.         lastChar = thisChar;
  46.     end
  47.    
  48.     charString = strcat(charString, sprintf('%s%i;n;', colorStr(lastChar+1), charCount));
  49. %     charCount = 1;
  50. end
  51.  
  52. figure
  53. hold on
  54. subplot(1,2,1)
  55. imagesc(image)
  56.  
  57. subplot(1,2,2)
  58. imagesc(imageRGB)
  59. hold off
  60. toc
  61.  
  62. fprintf('Encoded Length: %i\n', length(charString));
  63. disp(charString);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement