Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- img = imread('test7.png');
- bw = im2bw(img, 0.9);
- bw = ~bw;
- % matlabpool open
- rots = 1:16; % todo: zwiększyć do 1:360. Niech się komp męczy, a co!
- rot2area = zeros(size(rots));
- rotang=360/size(rots,2);
- parfor i=rots
- imm = imrotate(bw, i*rotang);
- % szumy won
- L = bwlabel(imm);
- s = regionprops(imm, 'Area');
- area_values = [s.Area];
- idx = find(5 < area_values);
- imm = ismember(L, idx);
- % pole otoczki
- [r,c] = find(imm);
- height = max(r)-min(r);
- width = max(c)-min(c);
- rot2area(i) = height * width;
- end
- % dobieramy najlepsze obroty
- [~,rots] = sort(rot2area);
- rots = rots(1:4);
- imm = imrotate(bw, rots(4)*rotang);
- % odszumiamy raz jeszcze
- L = bwlabel(imm);
- s = regionprops(imm, 'Area');
- area_values = [s.Area];
- idx = find(5 < area_values);
- imm = ismember(L, idx);
- % to nic nie daje, przynajmniej dla testowego tekstu
- %imm = imerode(imm, strel('rectangle',[1,1]));
- s = regionprops(imm, 'Centroid', 'BoundingBox');
- cents = cat(1,s.Centroid);
- bgbox = cat(1,s.BoundingBox);
- [width,~] = size(imm);
- SE = strel('line', width*width,0);
- imm2 = imdilate(imm,SE);
- linesp = regionprops(imm2, 'BoundingBox');
- linesbb = cat(1,linesp.BoundingBox);
- lines_cnt = size(linesbb,1);
- idx = find(bgbox(:,3) < 1.5 * bgbox(:,4)); % indeksy połączonych
- chars = cell(lines_cnt, 1);
- for i=1:lines_cnt
- p = {};
- j = 1;
- ly = linesbb(i,2);
- lh = linesbb(i,4);
- for ci=1:size(bgbox,1)
- cy = cents(ci,2);
- if ((ly <= cy) && (cy <= ly+lh) && ismember(ci,idx))
- x = ceil(bgbox(ci,1));
- y = ceil(bgbox(ci,2));
- w = ceil(bgbox(ci,3));
- h = ceil(bgbox(ci,4));
- p{j} = imresize(imm(y:y+h-1, x:x+w-1),[24,24]);
- j = j+1;
- end
- end
- chars{i} = p;
- end
- %%% RYSOWANIE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- imshow(imm)
- hold on
- plot(cents(:,1), cents(:,2), 'b+')
- unrec = {};
- unrec_i = 1;
- rec = {};
- rec_i = 1;
- for i=1:size(linesbb,1)
- rectangle('Position', linesbb(i,:), 'Curvature', [0,0], 'EdgeColor', 'y');
- end
- for i=1:size(bgbox,1)
- x = ceil(bgbox(i,1));
- y = ceil(bgbox(i,2));
- w = ceil(bgbox(i,3));
- h = ceil(bgbox(i,4));
- if not(ismember(i, idx))
- rectangle('Position', bgbox(i,:), 'Curvature', [0,0], 'EdgeColor', 'r');
- unrec{unrec_i} = imm(y:y+h-1, x:x+w-1);
- unrec_i = unrec_i + 1;
- else
- rectangle('Position', bgbox(i,:), 'Curvature', [0,0], 'EdgeColor', 'g');
- rec{rec_i} = imm(y:y+h-1, x:x+w-1);
- rec_i = rec_i + 1;
- end
- end
- hold off
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement