(textmap, linkmap, text_threshold, link_threshold, low_text)
| 23 | |
| 24 | |
| 25 | def getDetBoxes_core(textmap, linkmap, text_threshold, link_threshold, low_text): |
| 26 | # prepare data |
| 27 | linkmap = linkmap.copy() |
| 28 | textmap = textmap.copy() |
| 29 | img_h, img_w = textmap.shape |
| 30 | |
| 31 | """ labeling method """ |
| 32 | ret, text_score = cv2.threshold(textmap, low_text, 1, 0) |
| 33 | ret, link_score = cv2.threshold(linkmap, link_threshold, 1, 0) |
| 34 | |
| 35 | text_score_comb = np.clip(text_score + link_score, 0, 1) |
| 36 | nLabels, labels, stats, centroids = \ |
| 37 | cv2.connectedComponentsWithStats(text_score_comb.astype(np.uint8), connectivity=4) |
| 38 | |
| 39 | det = [] |
| 40 | mapper = [] |
| 41 | for k in range(1,nLabels): |
| 42 | # size filtering |
| 43 | size = stats[k, cv2.CC_STAT_AREA] |
| 44 | if size < 10: continue |
| 45 | |
| 46 | # thresholding |
| 47 | if np.max(textmap[labels==k]) < text_threshold: continue |
| 48 | |
| 49 | # make segmentation map |
| 50 | segmap = np.zeros(textmap.shape, dtype=np.uint8) |
| 51 | segmap[labels==k] = 255 |
| 52 | segmap[np.logical_and(link_score==1, text_score==0)] = 0 # remove link area |
| 53 | x, y = stats[k, cv2.CC_STAT_LEFT], stats[k, cv2.CC_STAT_TOP] |
| 54 | w, h = stats[k, cv2.CC_STAT_WIDTH], stats[k, cv2.CC_STAT_HEIGHT] |
| 55 | niter = int(math.sqrt(size * min(w, h) / (w * h)) * 2) |
| 56 | sx, ex, sy, ey = x - niter, x + w + niter + 1, y - niter, y + h + niter + 1 |
| 57 | # boundary check |
| 58 | if sx < 0 : sx = 0 |
| 59 | if sy < 0 : sy = 0 |
| 60 | if ex >= img_w: ex = img_w |
| 61 | if ey >= img_h: ey = img_h |
| 62 | kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(1 + niter, 1 + niter)) |
| 63 | segmap[sy:ey, sx:ex] = cv2.dilate(segmap[sy:ey, sx:ex], kernel, iterations=1) |
| 64 | #kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 5)) |
| 65 | #segmap[sy:ey, sx:ex] = cv2.dilate(segmap[sy:ey, sx:ex], kernel1, iterations=1) |
| 66 | |
| 67 | |
| 68 | # make box |
| 69 | np_contours = np.roll(np.array(np.where(segmap!=0)),1,axis=0).transpose().reshape(-1,2) |
| 70 | rectangle = cv2.minAreaRect(np_contours) |
| 71 | box = cv2.boxPoints(rectangle) |
| 72 | |
| 73 | # align diamond-shape |
| 74 | w, h = np.linalg.norm(box[0] - box[1]), np.linalg.norm(box[1] - box[2]) |
| 75 | box_ratio = max(w, h) / (min(w, h) + 1e-5) |
| 76 | if abs(1 - box_ratio) <= 0.1: |
| 77 | l, r = min(np_contours[:,0]), max(np_contours[:,0]) |
| 78 | t, b = min(np_contours[:,1]), max(np_contours[:,1]) |
| 79 | box = np.array([[l, t], [r, t], [r, b], [l, b]], dtype=np.float32) |
| 80 | |
| 81 | # make clock-wise order |
| 82 | startidx = box.sum(axis=1).argmin() |
no test coverage detected
searching dependent graphs…