Advertisement
kpfp_linux

Untitled

Jan 11th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.69 KB | None | 0 0
  1. img = imread('test7.png');
  2. bw = im2bw(img, 0.9);
  3. bw = ~bw;
  4.  
  5. % matlabpool open
  6. rots = 1:16; % todo: zwiększyć do 1:360. Niech się komp męczy, a co!
  7. rot2area = zeros(size(rots));
  8. rotang=360/size(rots,2);
  9. parfor i=rots
  10.     imm = imrotate(bw, i*rotang);
  11.    
  12.     % szumy won
  13.     L = bwlabel(imm);
  14.     s = regionprops(imm, 'Area');
  15.     area_values = [s.Area];
  16.     idx = find(5 < area_values);
  17.     imm = ismember(L, idx);
  18.    
  19.     % pole otoczki
  20.     [r,c] = find(imm);
  21.     height = max(r)-min(r);
  22.     width = max(c)-min(c);
  23.     rot2area(i) = height * width;
  24. end
  25.  
  26. % dobieramy najlepsze obroty
  27. [~,rots] = sort(rot2area);
  28. rots = rots(1:4);
  29.  
  30. imm = imrotate(bw, rots(4)*rotang);
  31.  
  32. % odszumiamy raz jeszcze
  33. L = bwlabel(imm);
  34. s = regionprops(imm, 'Area');
  35. area_values = [s.Area];
  36. idx = find(5 < area_values);
  37. imm = ismember(L, idx);
  38.  
  39. % to nic nie daje, przynajmniej dla testowego tekstu
  40. %imm = imerode(imm, strel('rectangle',[1,1]));
  41.  
  42. s = regionprops(imm, 'Centroid', 'BoundingBox');
  43. cents = cat(1,s.Centroid);
  44. bgbox = cat(1,s.BoundingBox);
  45.  
  46. [width,~] = size(imm);
  47. SE = strel('line', width*width,0);
  48. imm2 = imdilate(imm,SE);
  49. linesp = regionprops(imm2, 'BoundingBox');
  50. linesbb = cat(1,linesp.BoundingBox);
  51. lines_cnt = size(linesbb,1);
  52.  
  53. idx = find(bgbox(:,3) < 1.5 * bgbox(:,4)); % indeksy połączonych
  54. chars = cell(lines_cnt, 1);
  55.  
  56. for i=1:lines_cnt
  57.     p = {};
  58.     j = 1;
  59.     ly = linesbb(i,2);
  60.     lh = linesbb(i,4);
  61.     for ci=1:size(bgbox,1)
  62.         cy = cents(ci,2);
  63.         if ((ly <= cy) && (cy <= ly+lh) && ismember(ci,idx))
  64.             x = ceil(bgbox(ci,1));
  65.             y = ceil(bgbox(ci,2));
  66.             w = ceil(bgbox(ci,3));
  67.             h = ceil(bgbox(ci,4));
  68.             p{j} = imresize(imm(y:y+h-1, x:x+w-1),[24,24]);
  69.             j = j+1;
  70.         end
  71.     end
  72.     chars{i} = p;
  73. end
  74.  
  75. %%% RYSOWANIE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  76. imshow(imm)
  77. hold on
  78. plot(cents(:,1), cents(:,2), 'b+')
  79. unrec = {};
  80. unrec_i = 1;
  81. rec = {};
  82. rec_i = 1;
  83.  
  84. for i=1:size(linesbb,1)
  85.     rectangle('Position', linesbb(i,:), 'Curvature', [0,0], 'EdgeColor', 'y');
  86. end
  87.  
  88. for i=1:size(bgbox,1)
  89.     x = ceil(bgbox(i,1));
  90.     y = ceil(bgbox(i,2));
  91.     w = ceil(bgbox(i,3));
  92.     h = ceil(bgbox(i,4));
  93.     if not(ismember(i, idx))
  94.         rectangle('Position', bgbox(i,:), 'Curvature', [0,0], 'EdgeColor', 'r');
  95.         unrec{unrec_i} = imm(y:y+h-1, x:x+w-1);
  96.         unrec_i = unrec_i + 1;
  97.     else
  98.         rectangle('Position', bgbox(i,:), 'Curvature', [0,0], 'EdgeColor', 'g');
  99.         rec{rec_i} = imm(y:y+h-1, x:x+w-1);
  100.         rec_i = rec_i + 1;
  101.     end
  102. end
  103. hold off
  104. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement