implemented feature matching test

This commit is contained in:
2020-03-26 12:07:02 +01:00
parent a0a3e2e36b
commit 7aeff660cf
4 changed files with 61 additions and 24 deletions
@@ -5,6 +5,7 @@ import static de.unidue.nes.gpu.events.TaskType.CPU_DILATE;
import static de.unidue.nes.gpu.events.TaskType.CPU_ERODE;
import static de.unidue.nes.gpu.events.TaskType.CPU_FEATURE_DETECTION1;
import static de.unidue.nes.gpu.events.TaskType.CPU_FEATURE_DETECTION2;
import static de.unidue.nes.gpu.events.TaskType.CPU_FEATURE_MATCHING;
import static de.unidue.nes.gpu.events.TaskType.CPU_GAUSSIAN_BLUR;
import static de.unidue.nes.gpu.events.TaskType.CPU_GRAYSCALE;
import static de.unidue.nes.gpu.events.TaskType.CPU_HOUGH_CIRCLE;
@@ -19,6 +20,7 @@ import static de.unidue.nes.gpu.events.TaskType.GPU_DOWNLOAD;
import static de.unidue.nes.gpu.events.TaskType.GPU_ERODE;
import static de.unidue.nes.gpu.events.TaskType.GPU_FEATURE_DETECTION1;
import static de.unidue.nes.gpu.events.TaskType.GPU_FEATURE_DETECTION2;
import static de.unidue.nes.gpu.events.TaskType.GPU_FEATURE_MATCHING;
import static de.unidue.nes.gpu.events.TaskType.GPU_GAUSSIAN_BLUR;
import static de.unidue.nes.gpu.events.TaskType.GPU_GRAYSCALE;
import static de.unidue.nes.gpu.events.TaskType.GPU_HOUGH_CIRCLE;
@@ -40,6 +42,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -85,6 +88,7 @@ public class EventHandler {
printImageProcTasks();
printPipelineTasks();
printFeatureTasks();
printMatchingTable();
printStereoTable();
List<TaskResultEvent> notTested = this.events.stream().filter(evt -> !this.testedTasks.contains(evt.getType())).collect(Collectors.toList());
@@ -94,6 +98,10 @@ public class EventHandler {
}
}
private void printMatchingTable() {
printFeatureMatchingTable("feature matching table", new TaskType[] { CPU_FEATURE_MATCHING, GPU_FEATURE_MATCHING });
}
private void printStereoTable() {
printTable("stereo matching table", new TaskType[] { CPU_STEREO_MATCHING, GPU_STEREO_MATCHING });
}
@@ -124,6 +132,39 @@ public class EventHandler {
new TaskType[] { GPU_UPLOAD, GPU_DOWNLOAD, GPU_COMBINED, PAGE_LOCKED_GPU_UPLOAD, PAGE_LOCKED_GPU_DOWNLOAD, PAGE_LOCKED_GPU_COMBINED });
}
private void printFeatureMatchingTable(String tablename, TaskType[] types) {
this.testedTasks.addAll(Arrays.asList(types));
List<TaskResultEvent> eventList = this.events.stream().filter(ev -> Arrays.asList(types).contains(ev.getType())).collect(Collectors.toList());
if (eventList.isEmpty()) {
return;
}
System.out.println(tablename + ": ");
List<Integer> numberOfFeatures = IntStream.iterate(500, i -> true, i -> i + 500).limit(20).boxed().collect(Collectors.toList());
Map<TaskType, List<TaskResultEvent>> mappedEvents = new EnumMap<>(TaskType.class);
for (TaskResultEvent event : eventList) {
mappedEvents.computeIfAbsent(event.getType(), k -> new ArrayList<>()).add(event);
}
StringBuilder content = new StringBuilder();
mappedEvents.entrySet().stream().sorted(Comparator.comparingInt(entry -> entry.getKey().ordinal())).forEach(entry -> {
content.append(entry.getKey().getTitle());
content.append(" number of features = (average time, standard deviation):");
entry.getValue().sort(Comparator.comparingDouble(evt -> evt.getTimeDto().avgTime));
for (int i = 0; i < 20; i++) {
content.append("\n");
content.append(numberOfFeatures.get(i));
content.append("=");
content.append("(");
content.append(entry.getValue().get(i).getTimeDto().avgTime);
content.append(", ");
content.append(entry.getValue().get(i).getTimeDto().stdAbweichung);
content.append(")");
}
content.append("\n\n\n");
});
System.out.println(content);
}
private void printTable(String tablename, TaskType[] types) {
final boolean addHorizontalLines = true;
this.testedTasks.addAll(Arrays.asList(types));
@@ -52,14 +52,9 @@ public class CpuFeatureTask {
GpuEventBus.INSTANCE.post(new TaskResultEvent("", FeatureBasedTask.TEST_RUNS, image.size(), TaskType.CPU_FEATURE_DETECTION2, time));
}
public void match(Mat[] images) {
if (images.length != 2) {
throw new RuntimeException("This should not happen");
}
ORB orb = ORB.create();
public void match(Mat image, int nfeatures) {
ORB orb = ORB.create(nfeatures);
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
Mat mask = new Mat();
MatOfKeyPoint imageKeypoints = new MatOfKeyPoint();
@@ -69,14 +64,14 @@ public class CpuFeatureTask {
Mat testDescriptors = new Mat();
MatOfDMatch matches = new MatOfDMatch();
orb.detectAndCompute(images[0], mask, imageKeypoints, imageDescriptors);
orb.detectAndCompute(images[1], mask, testKeyPoints, testDescriptors);
orb.detectAndCompute(image, mask, imageKeypoints, imageDescriptors);
orb.detectAndCompute(image, mask, testKeyPoints, testDescriptors);
TimeDto time = TimingService.testPerformance(() -> {
matcher.match(testDescriptors, imageDescriptors, matches);
}, FeatureBasedTask.TEST_RUNS);
LOG.info(() -> "cpu feature matching time for images of size " + images[0].size() + ": " + time + "ms");
GpuEventBus.INSTANCE.post(new TaskResultEvent("", FeatureBasedTask.TEST_RUNS, images[0].size(), TaskType.CPU_FEATURE_MATCHING, time));
LOG.info(() -> "cpu feature matching time for " + imageKeypoints.toArray().length + " features in image of size " + image.size() + ": " + time + "ms");
GpuEventBus.INSTANCE.post(new TaskResultEvent("", FeatureBasedTask.TEST_RUNS, image.size(), TaskType.CPU_FEATURE_MATCHING, time));
}
}
@@ -75,37 +75,33 @@ public class GpuFeatureTask {
}
public void match(org.opencv.core.Mat[] images) {
if (images.length != 2) {
throw new RuntimeException("This should not happen");
}
public void match(org.opencv.core.Mat image, int nfeatures) {
Mat first = new Mat();
Mat second = new Mat();
this.converter.convert(images[0], first);
this.converter.convert(images[1], second);
this.converter.convert(image, first);
try (GpuMat gpuImg = new GpuMat();
GpuMat gpuImg2 = new GpuMat();
GpuMat mask = new GpuMat();
GpuMat imgDescriptors = new GpuMat();
GpuMat testDescriptors = new GpuMat();
ORB orb = ORB.create();
/* =cv::ORB::HARRIS_SCORE */
ORB orb = ORB.create(nfeatures, 1.2f, 8, 31, org.bytedeco.opencv.opencv_features2d.ORB.HARRIS_SCORE, 2, 0, 31, 20, false);
DescriptorMatcher matcher = DescriptorMatcher.createBFMatcher(NORM_HAMMING);
KeyPointVector keypoints = new KeyPointVector();
KeyPointVector testKeyPoints = new KeyPointVector();
DMatchVector matches = new DMatchVector()) {
gpuImg.upload(first);
gpuImg2.upload(second);
orb.detectAndCompute(gpuImg, mask, keypoints, imgDescriptors);
orb.detectAndCompute(gpuImg2, mask, testKeyPoints, testDescriptors);
orb.detectAndCompute(gpuImg, mask, testKeyPoints, testDescriptors);
TimeDto time = TimingService.testPerformance(() -> {
matcher.match(testDescriptors, imgDescriptors, matches);
}, FeatureBasedTask.TEST_RUNS);
LOG.info(() -> "gpu matching time for images of size " + images[0].size() + ": " + time + "ms");
GpuEventBus.INSTANCE.post(new TaskResultEvent("", FeatureBasedTask.TEST_RUNS, images[0].size(), TaskType.GPU_FEATURE_MATCHING, time));
LOG.info(() -> "gpu feature matching time for " + keypoints.size() + " features in image of size " + image.size() + ": " + time + "ms");
GpuEventBus.INSTANCE.post(new TaskResultEvent("", FeatureBasedTask.TEST_RUNS, image.size(), TaskType.GPU_FEATURE_MATCHING, time));
}
}
}
@@ -1,6 +1,7 @@
package de.unidue.nes.gpu.tasks.taskdefinitions;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.apache.logging.log4j.LogManager;
@@ -52,6 +53,10 @@ public class FeatureBasedTask implements Task {
Arrays.stream(stereoimages).forEach(arr -> cpuStereo.matchImages(arr[0], arr[1]));
Arrays.stream(stereoimages).forEach(arr -> gpuStereo.matchImages(arr[0], arr[1]));
Mat matchingImage = stereoimages[4][0];
IntStream.iterate(500, i -> true, i -> i + 500).limit(20).forEach(i -> cpuTask.match(matchingImage, i));
IntStream.iterate(500, i -> true, i -> i + 500).limit(20).forEach(i -> gpuTask.match(matchingImage, i));
}
}