Advertisement
kpfp_linux

Untitled

Jan 11th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.44 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.  
  28. [~,rots] = sort(rot2area);
  29. rots = rots(1:4);
  30.  
  31. imm = imrotate(bw, rots(4)*rotang);
  32.  
  33. % odszumiamy raz jeszcze
  34. L = bwlabel(imm);
  35. s = regionprops(imm, 'Area');
  36. area_values = [s.Area];
  37. idx = find(5 < area_values);
  38. imm = ismember(L, idx);
  39.  
  40. % to nic nie daje, przynajmniej dla testowego tekstu
  41. %imm = imerode(imm, strel('rectangle',[1,1]));
  42.  
  43. s = regionprops(imm, 'Centroid', 'BoundingBox');
  44.  
  45. cents = cat(1,s.Centroid);
  46. bgbox = cat(1,s.BoundingBox);
  47.  
  48. [width,~] = size(imm);
  49. SE = strel('line', width*width,0);
  50. imm2 = imdilate(imm,SE);
  51. linesp = regionprops(imm2, 'BoundingBox');
  52. lines = cat(1,linesp.BoundingBox);
  53.  
  54. imshow(imm)
  55. hold on
  56. plot(cents(:,1), cents(:,2), 'b+')
  57. idx = find(bgbox(:,3) < 1.5 * bgbox(:,4)); % indeksy połączonych
  58. unrec = {};
  59. unrec_i = 1;
  60.  
  61. for i=1:size(lines,1)
  62.     rectangle('Position', lines(i,:), 'Curvature', [0,0], 'EdgeColor', 'y');
  63. end
  64.  
  65. for i=1:size(bgbox,1)
  66.     if not(ismember(i, idx))
  67.         rectangle('Position', bgbox(i,:), 'Curvature', [0,0], 'EdgeColor', 'r');
  68.         x = ceil(bgbox(i,1));
  69.         y = ceil(bgbox(i,2));
  70.         w = ceil(bgbox(i,3))-1;
  71.         h = ceil(bgbox(i,4))-1;
  72.         unrec{unrec_i} = imm(y:y+h, x:x+w);
  73.         unrec_i = unrec_i + 1;
  74.     else
  75.         rectangle('Position', bgbox(i,:), 'Curvature', [0,0], 'EdgeColor', 'g')
  76.     end
  77. end
  78. hold off
  79.  
  80. % % szumy won
  81. % L = bwlabel(bw);
  82. % s = regionprops(L, 'Area');
  83. % area_values = [s.Area];
  84. % idx = find(5 < area_values);
  85. % bw = ismember(L, idx);
  86. % imshow(bw)
  87. %
  88. % % oblicz otoczkę wszystkiego
  89. % [r,c] = find(bw);
  90. % height = max(r)-min(r);
  91. % width = max(c)-min(c);
  92. % area = height * width;
  93.  
  94. %rots = 1:2;
  95. %t_im1rs = zeros(size(rots,2),1);
  96. %for angle = rots
  97. %    t_im1rs(angle) = 0; %imrotate(t_im1, angle);
  98. %    tmp = regionprops(imrotate(im, angle), 'BoundingBox');
  99. %    [ul_corner, width] = tmp.BoundingBox
  100. %end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement