def get_candidate_symbol_regions(image, text_regions, updated_width, updated_height):
img = skimage.io.imread(image.name)[:, :, :3]
if not (updated_height == len(img) and updated_width == len(img[0])):
img = skimage.transform.resize(img, (updated_height, updated_width))
symbol_regions = dict()
for x, y, w, h in text_regions:
text_region_image = img[y: y + h, x: x + w]
text_region_image_width = len(text_region_image[0])
text_region_image_height = len(text_region_image)
text_region_gray_image = skimage.color.rgb2gray(text_region_image)
text_region_binary_image = image <= threshold_otsu(text_region_gray_image)
temp = TemporaryFile(".png")
skimage.io.imsave(temp.name, text_region_binary_image)
text_region_binary_image = skimage.io.imread(temp.name)
text_region_blurred_image = gaussian_filter(text_region_binary_image, sigma=3.5)
text_region_blobs = text_region_blurred_image > text_region_blurred_image.mean()
text_region_labels = skimage.morphology.label(text_region_blobs, neighbors=4)
symbol_blobs = ndimage.find_objects(text_region_labels)
candidate_symbol_regions = set()
for c1, c2 in symbol_blobs:
if (c2.stop - c2.start) * c1.stop - c1.start > (text_region_image.shape[0] * text_region_image.shape[1]) * (0.026):
if (c2.stop - c2.start) * c1.stop - c1.start < (text_region_image.shape[0] * text_region_image.shape[1]) * (0.90):
candidate_symbol_regions.add(
(c2.start, c1.start, c2.stop - c2.start, c1.stop - c1.start))
symbol_regions[str((x, y, w, h))] = dict()
symbol_regions[str((x, y, w, h))]["image"] = text_region_image
symbol_regions[str((x, y, w, h))]["regions"] = candidate_symbol_regions
symbol_regions[str((x, y, w, h))]["width"] = text_region_image_width
symbol_regions[str((x, y, w, h))]["height"] = text_region_image_height
return symbol_regions
symbol_segmentation.py 文件源码
python
阅读 24
收藏 0
点赞 0
评论 0
评论列表
文章目录