Skip to content

Lotemn102/Text-Segmentation-From-Images

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 

Repository files navigation

TIS - Text-based Images Segmentation

A script for cropping words or characters from text-based images.

Can be used for both printed texts and handwritten texts.

For example:

1

Quick Start

# Creates a new image object.
img = tis('example.png')

# Crops words from text and saves them to 'cropped_words' directory.
img.crop_text(output_dir='cropped_words', iterations=5)

# Draws rectangles around all words in the text, and saves the result to
# 'result_rec.png'.
img.draw_rectangles(output_path='result_rec.png', iterations=5)

How This Works

This script uses cv2 package.

The algorithm for finding the words in a text-based image is demonstrated in the following chart:

2

Available Functions

draw_rectangles(output_path, iterations=5, dilate=True)

This function draws rectangles around the words in the text. With this function, you can see how ‘iterations’ variables affect the scripts’ segmentation performance. Can be convenient for long texts.

Parameters:

  • output_path (string): A path to save the image to. If None is given as a parameter, image will be saved in the original image parent directory.

  • iterations (int): Number of dilation iterations that will be done on the image. Default value is set to 5.

  • dilate (bool): Whether to dilate the text in the image or not. Default is set to 'True'. It is recommended to dilate the image for better segmentation.

Returns: None. Saves the image in the output path.

For example:

# Creates a new image object.
img = tis(‘example.png')

# Draws rectangles around all words in the text, and saves the result to
# 'result_rec.png'.
img.draw_rectangles(output_path='result_rec.png', iterations=5)

Result:

3

Higher number of iteration is used for text segmentation, while lower number is used for characters segmentation.

For example:

# Creates a new image object.
img = tis('example.png')

# Draws rectangles around all words in the text, and saves the result to
# 'result_rec.png'.
img.draw_rectangles(output_path='result_rec.png', iterations=1)

Result:

4

crop_text(output_dir, iterations=5, dilate=True):

This function crops the words from the text and saves them in separate png images.

Parameters:

  • output_dir (string): A path to save the image to. If None is given as a parameter, images will be saved in a directory called "text_segmentation" inside the original image's parent directory.

  • iterations (int): Number of dilation iterations that will be done on the image. Default value is set to 5.

  • dilate (bool): Whether to dilate the text in the image or not. Default is set to 'True'. It is recommended to dilate the image for better segmentation.

Returns: None. Saves images of all words from the text in the output path.

For example:

# Creates a new image object.
img = tis('example.png')

# Crops words from text and saves them to 'cropped_words' directory.
img.crop_text(output_dir='cropped_words', iterations=5)

Result:

5

Requirements

  • NumPy
  • cv2

About

Text-based images segmentation.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages