This example demonstrates how to find regions containing texts in images. It can be used for:

  • Previous step of character segmentation and recognition in a OCR solution.
  • Improve accuracy using third-party OCR modules. Tests have demonstrated that solutions using OCR modules like Tesseract can improve accuracy submitting just the image regions with text.
  • Improve performance of OCR solutions by reducing the amount of pixels to be processed.


The algorithm provided by Marvin just search for regions with an specific frequency of constrast variation in the horizontal axis. Horizontal lines segments with such pattern are grouped and the text region is determined. In some cases, false positive regions are detected, but a further step can be used to remove it based on its characteristics.

import marvin.MarvinPluginCollection.*;

public static List<MarvinSegment> findTextRegions
(
   MarvinImage imageIn, //input image
   int maxWhiteSpace, //max white pattern width
   int maxFontLineWidth, //max black pattern width
   int minTextWidth, //min text width
   int grayScaleThreshold //gray scale image threshold for fiding pattern
)


Output Images:

Original Images:
passport.png road_sign2.jpg

Source:
package textRegions;

import java.awt.Color;
import java.util.List;

import marvin.image.MarvinImage;
import marvin.image.MarvinSegment;
import marvin.io.MarvinImageIO;

import static marvin.MarvinPluginCollection.*;

public class TextRegions{

	public TextRegions(){
		MarvinImage image = MarvinImageIO.loadImage("./res/passport.png");
		image = findText(image, 15, 8, 30, 150);
		MarvinImageIO.saveImage(image, "./res/passport_out.png");
		
		image = MarvinImageIO.loadImage("./res/road_sign2.jpg");
		image = findText(image, 10, 20, 70, 200);
		MarvinImageIO.saveImage(image, "./res/road_sign2_out.png");
	}
	
	public MarvinImage findText(MarvinImage image, int maxWhiteSpace, int maxFontLineWidth, int minTextWidth, int grayScaleThreshold){
		List<MarvinSegment> segments = findTextRegions(image, maxWhiteSpace, maxFontLineWidth, minTextWidth, grayScaleThreshold);
		
		for(MarvinSegment s:segments){
			if(s.height >= 5){
				s.y1-=5;
				s.y2+=5;
				image.drawRect(s.x1, s.y1, s.x2-s.x1, s.y2-s.y1, Color.red);
				image.drawRect(s.x1+1, s.y1+1, (s.x2-s.x1)-2, (s.y2-s.y1)-2, Color.red);
			}
		}
		return image;
	}
	
	public static void main(String[] args) {
		new TextRegions();
	}
}