use of boofcv.struct.feature.Match in project BoofCV by lessthanoptimal.
the class TestTemplateMatching method maxMatches.
/**
* Makes sure maximum number of matches is handled correctly
*/
@Test
public void maxMatches() {
expected = new ArrayList<>();
expected.add(new Match(10, 11, 15));
expected.add(new Match(16, 15, 18));
expected.add(new Match(0, 0, 19));
expected.add(new Match(22, 30, 15));
DummyIntensity intensity = new DummyIntensity(true, 4, 5);
TemplateMatching alg = new TemplateMatching(intensity);
alg.setImage(input);
alg.setTemplate(template, null, 2);
alg.process();
// remove the lowest scores
expected.remove(3);
expected.remove(0);
checkResults(alg.getResults().toList(), expected, 4, 5);
}
use of boofcv.struct.feature.Match in project BoofCV by lessthanoptimal.
the class ExampleTemplateMatching method drawRectangles.
/**
* Helper function will is finds matches and displays the results as colored rectangles
*/
private static void drawRectangles(Graphics2D g2, GrayF32 image, GrayF32 template, GrayF32 mask, int expectedMatches) {
List<Match> found = findMatches(image, template, mask, expectedMatches);
int r = 2;
int w = template.width + 2 * r;
int h = template.height + 2 * r;
for (Match m : found) {
System.out.println("Match " + m.x + " " + m.y + " score " + m.score);
// this demonstrates how to filter out false positives
// the meaning of score will depend on the template technique
// if( m.score < -1000 ) // This line is commented out for demonstration purposes
// continue;
// the return point is the template's top left corner
int x0 = m.x - r;
int y0 = m.y - r;
int x1 = x0 + w;
int y1 = y0 + h;
g2.drawLine(x0, y0, x1, y0);
g2.drawLine(x1, y0, x1, y1);
g2.drawLine(x1, y1, x0, y1);
g2.drawLine(x0, y1, x0, y0);
}
}
use of boofcv.struct.feature.Match in project BoofCV by lessthanoptimal.
the class TemplateMatching method process.
/**
* Performs template matching.
*/
public void process() {
// compute match intensities
if (mask == null)
match.process(template);
else
match.process(template, mask);
GrayF32 intensity = match.getIntensity();
int offsetX = 0;
int offsetY = 0;
// adjust intensity image size depending on if there is a border or not
if (!match.isBorderProcessed()) {
int x0 = match.getBorderX0();
int x1 = imageWidth - (template.width - x0);
int y0 = match.getBorderY0();
int y1 = imageHeight - (template.height - y0);
intensity = intensity.subimage(x0, y0, x1, y1, null);
} else {
offsetX = match.getBorderX0();
offsetY = match.getBorderY0();
}
// find local peaks in intensity image
candidates.reset();
extractor.process(intensity, null, null, null, candidates);
// select the best matches
if (scores.length < candidates.size) {
scores = new float[candidates.size];
indexes = new int[candidates.size];
}
for (int i = 0; i < candidates.size; i++) {
Point2D_I16 p = candidates.get(i);
scores[i] = -intensity.get(p.x, p.y);
}
int N = Math.min(maxMatches, candidates.size);
QuickSelect.selectIndex(scores, N, candidates.size, indexes);
// save the results
results.reset();
for (int i = 0; i < N; i++) {
Point2D_I16 p = candidates.get(indexes[i]);
Match m = results.grow();
m.score = -scores[indexes[i]];
m.set(p.x - offsetX, p.y - offsetY);
}
}
use of boofcv.struct.feature.Match in project BoofCV by lessthanoptimal.
the class TestTemplateMatching method basicTest_NOBORDER.
/**
* Basic detection task with an extraction algorithm that has no border
*/
@Test
public void basicTest_NOBORDER() {
expected = new ArrayList<>();
expected.add(new Match(10, 11, 15));
expected.add(new Match(17, 15, 18));
// shouldn't detect this guy since its inside the border
expected.add(new Match(0, 0, 18));
DummyIntensity intensity = new DummyIntensity(false, 4, 5);
TemplateMatching alg = new TemplateMatching(intensity);
alg.setImage(input);
alg.setTemplate(template, null, 10);
alg.process();
expected.remove(2);
checkResults(alg.getResults().toList(), expected, 4, 5);
}
use of boofcv.struct.feature.Match in project BoofCV by lessthanoptimal.
the class TestTemplateMatching method checkResults.
private void checkResults(List<Match> found, List<Match> expected, int offsetX, int offsetY) {
assertEquals(expected.size(), found.size());
for (Match f : found) {
boolean matched = false;
for (Match e : expected) {
if (e.x - offsetX == f.x & e.y - offsetY == f.y) {
assertFalse(matched);
assertEquals(e.score, f.score, 1e-8);
matched = true;
}
}
assertTrue(matched);
}
}
Aggregations