@Override
protected void addContent(PLayer layer) {
for (MatchHypothesis h : hypotheses){
ModelPartMatch m1 = h.getTopLeft();
ModelPartMatch m2 = h.getBottomRight();
RegionMatch p1 = m1.getScoreMatch();
RegionMatch p2 = m2.getScoreMatch();
PPath line = PPath.createLine(p1.getX(),p1.getY(),p2.getX(),p2.getY());
line.setStroke(new BasicStroke(2f));
line.setStrokePaint(Color.red);
layer.addChild(line);
Point p = h.getExpectedBottomLeftPartModelLocation();
PPath c = PPath.createRectangle(p.x,p.y,10,10);
c.setStroke(new BasicStroke(2f));
c.setStrokePaint(Color.green);
layer.addChild(c);
p = h.getExpectedTopRightPartModelLocation();
c = PPath.createRectangle(p.x,p.y,10,10);
c.setStroke(new BasicStroke(2f));
c.setStrokePaint(Color.green);
layer.addChild(c);
}
}
};
logger.step(hypothesesRenderer, "hypotheses");
for (MatchHypothesis h1 : hypotheses){
Point expectedLocation = h1.getExpectedTopRightPartModelLocation();
// find if there is a matched part in the expected location
// according to this hypothesis
for (RegionMatch s2 : tms2){
Point seenLocation = s2.getLocation();
boolean isMatchedPartSeenNearbyExpectedLocation = seenLocation.distance(expectedLocation.x,expectedLocation.y) < 5.0f;
if (isMatchedPartSeenNearbyExpectedLocation){
h1.setTopRight(new ModelPartMatch(model.getTopRight(),s2));
break;
}
}
expectedLocation = h1.getExpectedBottomLeftPartModelLocation();
for (RegionMatch s4 : tms4){
Point seenLocation = s4.getLocation();
boolean isMatchedPartSeenNearbyExpectedLocation = seenLocation.distance(expectedLocation.x, expectedLocation.y) < 5.0f;
if (isMatchedPartSeenNearbyExpectedLocation){
h1.setBottomLeft(new ModelPartMatch(model.getBottomLeft(),s4));
break;
}
}
}
logger.step(new MatchHypotheseRenderer(testImage, hypotheses), "hypotheses + other matched parts");
//hypotheses.add(newHypothesis);
// keep good hypotheses
List<MatchHypothesis> goodHypotheses = Lists.newArrayList();
for (MatchHypothesis h1 : hypotheses){
if (h1.getScore() == 4){
goodHypotheses.add(h1);
}
}
// sort hypotheses by increasing sizes
Collections.sort(goodHypotheses, new Comparator<MatchHypothesis>(){
public int compare(MatchHypothesis arg0, MatchHypothesis arg1) {
return arg0.getBounds().width * arg0.getBounds().height -
arg1.getBounds().width * arg1.getBounds().height;
}
});
Map<RegionMatch,Integer> alreadyUsedMatch = Maps.newHashMap();
// remove overlapping hypotheses
List<MatchHypothesis> nonOverlappingGoodHypotheses = Lists.newArrayList();
for (MatchHypothesis h1 : goodHypotheses){
if (alreadyUsedMatch.containsKey(h1.getTopLeft().getScoreMatch()) ||
alreadyUsedMatch.containsKey(h1.getBottomLeft().getScoreMatch()) ||
alreadyUsedMatch.containsKey(h1.getTopRight().getScoreMatch()) ||
alreadyUsedMatch.containsKey(h1.getBottomRight().getScoreMatch())){
// ignore
}else{
nonOverlappingGoodHypotheses.add(h1);
alreadyUsedMatch.put(h1.getTopLeft().getScoreMatch(), 1);
alreadyUsedMatch.put(h1.getBottomLeft().getScoreMatch(), 1);
alreadyUsedMatch.put(h1.getTopRight().getScoreMatch(), 1);
alreadyUsedMatch.put(h1.getBottomRight().getScoreMatch(), 1);
}
}
logger.step(new MatchHypotheseRenderer(testImage, nonOverlappingGoodHypotheses), "non-overlapping good hypotheses");
// collect the list of ScoreRegionMatch objects to return
List<RegionMatch> matches = Lists.newArrayList();
for (MatchHypothesis h1 : nonOverlappingGoodHypotheses){
RegionMatch regionMatch = new RegionMatch(h1.getBounds());
matches.add(regionMatch);
}
return matches;
}