added task for feature matching
This commit is contained in:
@@ -11,7 +11,7 @@ import org.apache.logging.log4j.core.config.Configuration;
|
||||
|
||||
import de.unidue.nes.gpu.initialization.Initializer;
|
||||
import de.unidue.nes.gpu.tasks.Task;
|
||||
import de.unidue.nes.gpu.tasks.taskdefinitions.ColoringTask;
|
||||
import de.unidue.nes.gpu.tasks.taskdefinitions.FeatureMatchingTask;
|
||||
|
||||
/**
|
||||
* Class containing the main method of this project.
|
||||
@@ -51,7 +51,7 @@ public class GpuTestSystem {
|
||||
|
||||
private List<Task> getTasks() {
|
||||
|
||||
return Arrays.asList(new ColoringTask());
|
||||
return Arrays.asList(new FeatureMatchingTask());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package de.unidue.nes.gpu.tasks.cputasks;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.opencv.core.DMatch;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfDMatch;
|
||||
import org.opencv.core.MatOfKeyPoint;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.features2d.DescriptorMatcher;
|
||||
import org.opencv.features2d.ORB;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class CPUFeatureMatching {
|
||||
|
||||
private static final Logger LOG = LogManager.getLogger(CPUFeatureMatching.class);
|
||||
|
||||
public void matchFeatures() {
|
||||
|
||||
ORB orb = ORB.create();
|
||||
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
|
||||
Mat originalImage = Imgcodecs.imread("src/main/resources/theSimpsons.jpg");
|
||||
Mat wrongImage = Imgcodecs.imread("src/main/resources/car.jpg");
|
||||
Mat correctImage = Imgcodecs.imread("src/main/resources/theSimpsons_mirrored.jpg");
|
||||
|
||||
Imgproc.cvtColor(originalImage, originalImage, Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.cvtColor(wrongImage, wrongImage, Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.cvtColor(correctImage, correctImage, Imgproc.COLOR_BGR2GRAY);
|
||||
|
||||
Imgproc.resize(originalImage, originalImage, new Size(), 15, 15);
|
||||
Imgproc.resize(wrongImage, wrongImage, new Size(), 15, 15);
|
||||
Imgproc.resize(correctImage, correctImage, new Size(), 15, 15);
|
||||
|
||||
Mat mask = new Mat();
|
||||
|
||||
MatOfKeyPoint imageKeypoints = new MatOfKeyPoint();
|
||||
Mat imageDescriptors = new Mat();
|
||||
|
||||
MatOfKeyPoint testKeyPoints = new MatOfKeyPoint();
|
||||
Mat testDescriptors = new Mat();
|
||||
MatOfDMatch matches = new MatOfDMatch();
|
||||
|
||||
long time = System.currentTimeMillis();
|
||||
for (int i = 0; i < 50; i++) {
|
||||
orb.detectAndCompute(originalImage, mask, imageKeypoints, imageDescriptors);
|
||||
orb.detectAndCompute(wrongImage, mask, testKeyPoints, testDescriptors);
|
||||
matcher.match(testDescriptors, imageDescriptors, matches);
|
||||
List<DMatch> matchList = matches.toList();
|
||||
double sum1 = matchList.stream().sorted((match1, match2) -> Float.compare(match1.distance, match2.distance)).limit(5)
|
||||
.mapToDouble(match -> match.distance).sum();
|
||||
|
||||
orb.detectAndCompute(correctImage, mask, testKeyPoints, testDescriptors);
|
||||
matcher.match(testDescriptors, imageDescriptors, matches);
|
||||
matchList = matches.toList();
|
||||
|
||||
double sum2 = matchList.stream().sorted((match1, match2) -> Float.compare(match1.distance, match2.distance)).limit(5)
|
||||
.mapToDouble(match -> match.distance).sum();
|
||||
|
||||
if (sum1 < sum2) {
|
||||
// first image is the correct one
|
||||
} else {
|
||||
// second image is the correct one
|
||||
}
|
||||
}
|
||||
long time2 = System.currentTimeMillis();
|
||||
|
||||
LOG.info(() -> "execution time: " + (time2 - time) + "ms");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package de.unidue.nes.gpu.tasks.gputasks;
|
||||
|
||||
import static org.bytedeco.opencv.global.opencv_core.NORM_HAMMING;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bytedeco.opencv.opencv_core.DMatch;
|
||||
import org.bytedeco.opencv.opencv_core.DMatchVector;
|
||||
import org.bytedeco.opencv.opencv_core.GpuMat;
|
||||
import org.bytedeco.opencv.opencv_core.KeyPointVector;
|
||||
import org.bytedeco.opencv.opencv_core.Mat;
|
||||
import org.bytedeco.opencv.opencv_cudafeatures2d.DescriptorMatcher;
|
||||
import org.bytedeco.opencv.opencv_cudafeatures2d.ORB;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import de.unidue.nes.gpu.utils.MatConverter;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class GPUFeatureMatching {
|
||||
|
||||
private static final Logger LOG = LogManager.getLogger(GPUFeatureMatching.class);
|
||||
|
||||
private MatConverter converter;
|
||||
|
||||
public GPUFeatureMatching() {
|
||||
this.converter = new MatConverter();
|
||||
}
|
||||
|
||||
public void matchFeatures() {
|
||||
org.opencv.core.Mat _originalImage = Imgcodecs.imread("src/main/resources/theSimpsons.jpg");
|
||||
org.opencv.core.Mat _wrongImage = Imgcodecs.imread("src/main/resources/car.jpg");
|
||||
org.opencv.core.Mat _correctImage = Imgcodecs.imread("src/main/resources/theSimpsons_mirrored.jpg");
|
||||
Imgproc.cvtColor(_originalImage, _originalImage, Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.cvtColor(_wrongImage, _wrongImage, Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.cvtColor(_correctImage, _correctImage, Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.resize(_originalImage, _originalImage, new Size(), 15, 15);
|
||||
Imgproc.resize(_wrongImage, _wrongImage, new Size(), 15, 15);
|
||||
Imgproc.resize(_correctImage, _correctImage, new Size(), 15, 15);
|
||||
|
||||
Mat originalImage = new Mat();
|
||||
Mat wrongImage = new Mat();
|
||||
Mat correctImage = new Mat();
|
||||
this.converter.convert(_originalImage, originalImage);
|
||||
this.converter.convert(_wrongImage, wrongImage);
|
||||
this.converter.convert(_correctImage, correctImage);
|
||||
|
||||
try (GpuMat gpuImg = new GpuMat();
|
||||
GpuMat gpuWrongImg = new GpuMat();
|
||||
GpuMat gpuCorrectImg = new GpuMat();
|
||||
GpuMat mask = new GpuMat();
|
||||
GpuMat imgDescriptors = new GpuMat();
|
||||
GpuMat testDescriptors = new GpuMat()) {
|
||||
|
||||
gpuImg.upload(originalImage);
|
||||
gpuWrongImg.upload(wrongImage);
|
||||
gpuCorrectImg.upload(correctImage);
|
||||
|
||||
// Mat maskMat = new Mat(noArray());
|
||||
// mask.upload(noArray());
|
||||
ORB orb = ORB.create();
|
||||
DescriptorMatcher matcher = DescriptorMatcher.createBFMatcher(NORM_HAMMING);
|
||||
KeyPointVector keypoints = new KeyPointVector();
|
||||
KeyPointVector testKeyPoints = new KeyPointVector();
|
||||
DMatchVector matches = new DMatchVector();
|
||||
|
||||
long time = System.currentTimeMillis();
|
||||
for (int i = 0; i < 50; i++) {
|
||||
orb.detectAndCompute(gpuImg, mask, keypoints, imgDescriptors);
|
||||
orb.detectAndCompute(gpuWrongImg, mask, testKeyPoints, testDescriptors);
|
||||
matcher.match(testDescriptors, imgDescriptors, matches);
|
||||
List<DMatch> matchList = Arrays.asList(matches.get());
|
||||
double sum1 = matchList.stream().sorted((match1, match2) -> Float.compare(match1.distance(), match2.distance())).limit(5)
|
||||
.mapToDouble(DMatch::distance).sum();
|
||||
orb.detectAndCompute(gpuCorrectImg, mask, testKeyPoints, testDescriptors);
|
||||
matcher.match(testDescriptors, imgDescriptors, matches);
|
||||
matchList = Arrays.asList(matches.get());
|
||||
double sum2 = matchList.stream().sorted((match1, match2) -> Float.compare(match1.distance(), match2.distance())).limit(5)
|
||||
.mapToDouble(DMatch::distance).sum();
|
||||
if (sum1 < sum2) {
|
||||
// first image is the correct one
|
||||
} else {
|
||||
// second image is the correct one
|
||||
}
|
||||
}
|
||||
|
||||
long time2 = System.currentTimeMillis();
|
||||
LOG.info(() -> "execution time: " + (time2 - time) + "ms");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+12
-1
@@ -1,15 +1,26 @@
|
||||
package de.unidue.nes.gpu.tasks.taskdefinitions;
|
||||
|
||||
import de.unidue.nes.gpu.tasks.Task;
|
||||
import de.unidue.nes.gpu.tasks.cputasks.CPUFeatureMatching;
|
||||
import de.unidue.nes.gpu.tasks.gputasks.GPUFeatureMatching;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class FeatureMatchingTask implements Task {
|
||||
|
||||
private CPUFeatureMatching cpuTask;
|
||||
private GPUFeatureMatching gpuTask;
|
||||
|
||||
public FeatureMatchingTask() {
|
||||
this.cpuTask = new CPUFeatureMatching();
|
||||
this.gpuTask = new GPUFeatureMatching();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runTask() {
|
||||
|
||||
this.cpuTask.matchFeatures();
|
||||
this.gpuTask.matchFeatures();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 26 KiB |
Reference in New Issue
Block a user