|
| 1 | +function polys = getPolygonBoxes(boxes, labels, mapper) |
| 2 | +% This function generates polygon shape bounding boxes from quadrilateral |
| 3 | +% boxes. |
| 4 | + |
| 5 | +% Copyright 2021 The Mathworks, Inc. |
| 6 | + |
| 7 | +% configurations |
| 8 | +numCp = 5; |
| 9 | +maxLenRatio = 0.7; |
| 10 | +expandRatio = 1.45; |
| 11 | +rMax = 2.0; |
| 12 | +rStep = 0.2; |
| 13 | + |
| 14 | +polys = []; |
| 15 | +for i = 1:size(boxes,1) |
| 16 | + box = reshape(boxes(i,:,:),[2 4]); |
| 17 | + box = box'; |
| 18 | + % size filter for small instance |
| 19 | + w = int32(norm(box(1,:)-box(2,:))+1); |
| 20 | + h = int32(norm(box(2,:)-box(3,:))+1); |
| 21 | + if w < 10 || h < 10 |
| 22 | + polys = [polys; NaN([1 28])]; |
| 23 | + continue; |
| 24 | + end |
| 25 | + |
| 26 | + % warp image |
| 27 | + tar = double([[1,1];[w,1];[w,h];[1,h]]); |
| 28 | + M = fitgeotrans(box,tar,'projective'); |
| 29 | + wordLabel = imwarp(labels,M,'nearest','OutputView',imref2d([h w])); |
| 30 | + try |
| 31 | + Minv = inv(transpose(M.T)); |
| 32 | + catch |
| 33 | + polys = [polys; NaN([1 28])]; |
| 34 | + continue; |
| 35 | + end |
| 36 | + |
| 37 | + % binarization for selected label |
| 38 | + cur_label = mapper(i); |
| 39 | + wordLabel(wordLabel ~= cur_label) = 0; |
| 40 | + wordLabel(wordLabel > 0) = 1; |
| 41 | + |
| 42 | + % Polygon generation |
| 43 | + |
| 44 | + % find top/bottom contours |
| 45 | + cp = []; % stores the top and bottom location of the column in word label containing word |
| 46 | + maxLen = -1; |
| 47 | + for j = 1:w |
| 48 | + region = find(wordLabel(:,j)~=0); |
| 49 | + if size(region,1) < 2 |
| 50 | + continue |
| 51 | + end |
| 52 | + cp = [cp; [j,region(1),region(end)]]; |
| 53 | + lengths = region(end) - region(1) + 1; |
| 54 | + if lengths > maxLen |
| 55 | + maxLen = lengths; |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + % pass if maxLen is similar to h |
| 60 | + if h * maxLenRatio < maxLen |
| 61 | + polys = [polys; NaN([1 28])]; |
| 62 | + continue; |
| 63 | + end |
| 64 | + |
| 65 | + % get pivot points with fixed length |
| 66 | + totSeg = numCp * 2 + 1; |
| 67 | + segW = double(w)/totSeg; % segment_width |
| 68 | + pp = repmat([nan,nan],numCp,1); % init pivot points |
| 69 | + cpSection = repmat([0,0],totSeg,1); % stores center point of each section |
| 70 | + segHeight = zeros([1 numCp]); |
| 71 | + segNum = 1; |
| 72 | + numSec = 0; |
| 73 | + prevH = -1; |
| 74 | + for k = 1:length(cp) |
| 75 | + x = cp(k,1); |
| 76 | + sy = cp(k,2); |
| 77 | + ey = cp(k,3); |
| 78 | + if (segNum) * segW <= (x-1) && segNum <= totSeg |
| 79 | + % average previous segment |
| 80 | + if numSec == 0 |
| 81 | + break; |
| 82 | + end |
| 83 | + cpSection(segNum,:) = [cpSection(segNum,1)/numSec cpSection(segNum,2)/numSec]; |
| 84 | + numSec = 0; |
| 85 | + % reset variables |
| 86 | + segNum = segNum + 1; |
| 87 | + prevH = -1; |
| 88 | + |
| 89 | + end |
| 90 | + % accumulate center points |
| 91 | + cy = (sy + ey) * 0.5; |
| 92 | + curH = ey - sy + 1; |
| 93 | + |
| 94 | + cpSection(segNum,:) = [cpSection(segNum,1)+x cpSection(segNum,2)+cy]; |
| 95 | + numSec = numSec + 1; |
| 96 | + if mod(segNum,2) ~= 0 |
| 97 | + continue; % No polygon area |
| 98 | + end |
| 99 | + if prevH < curH |
| 100 | + |
| 101 | + pp(int32((segNum)/2),:) = [x cy]; |
| 102 | + segHeight(int32((segNum)/2)) = curH; |
| 103 | + prevH = curH; |
| 104 | + end |
| 105 | + |
| 106 | + |
| 107 | + end |
| 108 | + % processing last segment |
| 109 | + if numSec ~=0 |
| 110 | + cpSection(end,:) = [cpSection(end,1)/numSec cpSection(end,2)/numSec]; |
| 111 | + end |
| 112 | + |
| 113 | + % pass if num of pivots is not sufficient or segment width i |
| 114 | + % smaller than character height |
| 115 | + if any(any(isnan(pp))) || (segW < max(segHeight)*0.25) |
| 116 | + polys = [polys;NaN([1 28])]; |
| 117 | + continue; |
| 118 | + end |
| 119 | + |
| 120 | + %calc median maximum of pivot points |
| 121 | + halfCharH = median(segHeight)*(expandRatio/2); |
| 122 | + |
| 123 | + %calculate gradient and apply to make horizontal pivots |
| 124 | + newPivotPoint = []; |
| 125 | + for k = 1:size(pp,1) |
| 126 | + x = pp(k,1); |
| 127 | + cy = pp(k,2); |
| 128 | + dx = cpSection(k*2+1,1) - cpSection(k*2-1,1); |
| 129 | + dy = cpSection(k*2+1,2) - cpSection(k*2-1,2); |
| 130 | + if dx == 0 |
| 131 | + newPivotPoint = [newPivotPoint; [x cy-halfCharH x cy+halfCharH]]; |
| 132 | + continue; |
| 133 | + end |
| 134 | + rad = -atan2(dy,dx); |
| 135 | + c = halfCharH*cos(rad); |
| 136 | + s = halfCharH*sin(rad); |
| 137 | + newPivotPoint = [newPivotPoint; [x-s cy-c x+s cy+c]]; |
| 138 | + end |
| 139 | + % get edge points to cover character heatmaps |
| 140 | + isSppFound = false; |
| 141 | + isEppFound = false; |
| 142 | + gradS = (pp(2,2)-pp(1,2))/(pp(2,1)-pp(1,1)) + (pp(3,2)-pp(2,2))/(pp(3,1)-pp(2,1)); |
| 143 | + gradE = (pp(end-1,2)-pp(end,2))/(pp(end-1,1)-pp(end,1)) + (pp(end-2,2)-pp(end-1,2))/(pp(end-2,1)-pp(end-1,1)); |
| 144 | + for r = 0.5:rStep:rMax |
| 145 | + dx = 2 * halfCharH * r; |
| 146 | + if ~isSppFound |
| 147 | + lineImg = uint8(zeros(size(wordLabel))); |
| 148 | + dy = gradS * dx; |
| 149 | + p = newPivotPoint(1,:) - [dx dy dx dy]; |
| 150 | + lineImg = insertShape(lineImg,'Line',[p(1) p(2) p(3) p(4)]); |
| 151 | + if (sum(wordLabel & lineImg,'all') == 0) || r+2 * rStep >= rMax |
| 152 | + spp = p; |
| 153 | + isSppFound = true; |
| 154 | + end |
| 155 | + end |
| 156 | + if ~isEppFound |
| 157 | + lineImg = uint8(zeros(size(wordLabel))); |
| 158 | + dy = gradE * dx; |
| 159 | + p = newPivotPoint(end,:) + [dx dy dx dy]; |
| 160 | + lineImg = insertShape(lineImg,'Line',[p(1) p(2) p(3) p(4)]); |
| 161 | + if (sum(wordLabel & lineImg,'all') == 0) || r+2 * rStep >= rMax |
| 162 | + epp = p; |
| 163 | + isEppFound = true; |
| 164 | + end |
| 165 | + end |
| 166 | + if isSppFound && isEppFound |
| 167 | + break; |
| 168 | + end |
| 169 | + end |
| 170 | + % pass if boundary of polygon is not found |
| 171 | + if ~(isSppFound&&isEppFound) |
| 172 | + polys = [polys;NaN([1 28])]; |
| 173 | + continue; |
| 174 | + end |
| 175 | + |
| 176 | + % make final polygon |
| 177 | + poly = []; |
| 178 | + poly = [poly warpCoord(Minv,[spp(1),spp(2)])]; |
| 179 | + for l = 1:size(newPivotPoint,1) |
| 180 | + p = newPivotPoint(l,:); |
| 181 | + poly = [poly warpCoord(Minv,[p(1),p(2)])]; |
| 182 | + end |
| 183 | + poly = [poly warpCoord(Minv,[epp(1),epp(2)])]; |
| 184 | + poly = [poly warpCoord(Minv,[epp(3),epp(4)])]; |
| 185 | + for l = length(newPivotPoint):-1:1 |
| 186 | + p = newPivotPoint(l,:); |
| 187 | + poly = [poly warpCoord(Minv,[p(3) p(4)])]; |
| 188 | + end |
| 189 | + poly = [poly warpCoord(Minv,[spp(3) spp(4)])]; |
| 190 | + |
| 191 | + % add to final result |
| 192 | + polys = [polys;poly]; |
| 193 | +end |
| 194 | +end |
| 195 | + |
| 196 | +function res = warpCoord(Minv,pt) |
| 197 | + out = Minv * [pt(1) pt(2) 1]'; |
| 198 | + res = [out(1)/out(3) out(2)/out(3)]; |
| 199 | +end |
| 200 | + |
0 commit comments