com.venky.ocr.Histogram Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Commonly used programming tasks in java
package com.venky.ocr;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import com.venky.ocr.TextRecognizer.CharacterRegion;
public class Histogram {
private SortedMap> heightClassification = new TreeMap>();
private SortedMap> widthClassification = new TreeMap>();
public Histogram(Map trainingMap){
for (Character c: trainingMap.keySet()){
CharacterRegion reg = trainingMap.get(c);
List sameHeightCharacters = heightClassification.get(reg.height());
List sameWidthCharacters = widthClassification.get(reg.width());
if (sameHeightCharacters == null){
sameHeightCharacters = new ArrayList();
heightClassification.put(reg.height(), sameHeightCharacters);
}
if (sameWidthCharacters == null){
sameWidthCharacters = new ArrayList();
widthClassification.put(reg.width(), sameWidthCharacters);
}
sameHeightCharacters.add(c);
sameWidthCharacters.add(c);
}
}
public void printHeightDistribution(){
for (Integer height:heightClassification.keySet()){
System.out.println(height +":" + heightClassification.get(height));
}
}
public void printWidthDistribution(){
for (Integer width:widthClassification.keySet()){
System.out.println(width +":" + widthClassification.get(width));
}
}
public SortedMap> getTrainingCharactersByHeight(){
return heightClassification;
}
public SortedMap> getTrainingCharactersByWidth(){
return widthClassification;
}
public Collection getCharactersTallerThan(int height, int numBands){
int numBandsRet = 0 ;
List taller = new ArrayList();
for (int h : heightClassification.tailMap(height+1).keySet() ){
taller.addAll(heightClassification.get(h));
numBandsRet ++ ;
if (numBandsRet >= numBands){
break;
}
}
return taller;
}
public Collection getCharactersThickerThan(int width, int numBands){
List thicker = new ArrayList();
int numBandsRet = 0 ;
for (int w : widthClassification.tailMap(width+1).keySet() ){
thicker.addAll(widthClassification.get(w));
numBandsRet ++ ;
if (numBandsRet >= numBands){
break;
}
}
return thicker;
}
}