diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/GpuTestSystem.java b/gpu-app/src/main/java/de/unidue/nes/gpu/GpuTestSystem.java index b9a12ee..f14c788 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/GpuTestSystem.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/GpuTestSystem.java @@ -13,7 +13,7 @@ import de.unidue.nes.gpu.events.ApplicationFinishedEvent; import de.unidue.nes.gpu.events.GpuEventBus; import de.unidue.nes.gpu.initialization.Initializer; import de.unidue.nes.gpu.tasks.Task; -import de.unidue.nes.gpu.tasks.taskdefinitions.UploadDownloadTask; +import de.unidue.nes.gpu.tasks.taskdefinitions.FeatureMatchingTask; /** * Class containing the main method of this project. @@ -75,7 +75,7 @@ public class GpuTestSystem { private List getTasks() { - return Arrays.asList(new UploadDownloadTask()); + return Arrays.asList(new FeatureMatchingTask()); } } diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/cputasks/CPUEdgeDetection.java b/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/cputasks/CPUEdgeDetection.java index 8a1b677..5f8cbe2 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/cputasks/CPUEdgeDetection.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/cputasks/CPUEdgeDetection.java @@ -17,9 +17,11 @@ public class CPUEdgeDetection { public void findEdges(Mat img) { Mat edges = new Mat(); - long time = TimingService.measureTime(() -> Imgproc.Canny(img, edges, 20, 60), 1000); + long time = TimingService.measureTime(() -> Imgproc.Canny(img, edges, 35, 100), 1000); - LOG.info(() -> "execution time: " + time + "ms"); +// HighGui.imshow("CPU edges", edges); +// HighGui.waitKey(); + LOG.info(() -> "cpu execution time for image of size " + img.size() + ": " + time + "ms"); } } diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/cputasks/CPUFeatureMatching.java b/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/cputasks/CPUFeatureMatching.java index edf88d6..4130c4d 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/cputasks/CPUFeatureMatching.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/cputasks/CPUFeatureMatching.java @@ -1,18 +1,12 @@ 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; import de.unidue.nes.gpu.utils.TimingService; @@ -23,21 +17,12 @@ public class CPUFeatureMatching { private static final Logger LOG = LogManager.getLogger(CPUFeatureMatching.class); - public void matchFeatures() { - + public void matchFeatures(Mat[] images) { + if (images.length != 2) { + throw new RuntimeException("This should not happen"); + } 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(); @@ -49,28 +34,20 @@ public class CPUFeatureMatching { MatOfDMatch matches = new MatOfDMatch(); long time = TimingService.measureTime(() -> { - orb.detectAndCompute(originalImage, mask, imageKeypoints, imageDescriptors); - orb.detectAndCompute(wrongImage, mask, testKeyPoints, testDescriptors); + orb.detectAndCompute(images[0], mask, imageKeypoints, imageDescriptors); + orb.detectAndCompute(images[1], mask, testKeyPoints, testDescriptors); matcher.match(testDescriptors, imageDescriptors, matches); - List 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 - } - }, 50); - - LOG.info(() -> "execution time: " + time + "ms"); + }, 100); + // Just for debugging +// List matchList = matches.toList(); +// matchList = matchList.stream().sorted((match1, match2) -> Float.compare(match1.distance, match2.distance)).limit(25).collect(Collectors.toList()); +// matches.fromList(matchList); +// Mat outImg = new Mat(); +// Features2d.drawMatches(images[0], imageKeypoints, images[1], testKeyPoints, matches, outImg); +// Imgproc.resize(outImg, outImg, new Size(), 0.25, 0.25); +// HighGui.imshow("matches", outImg); +// HighGui.waitKey(); + LOG.info(() -> "cpu execution time for images of size " + images[0].size() + ": " + time + "ms"); } } diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/gputasks/GPUEdgeDetection.java b/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/gputasks/GPUEdgeDetection.java index 816f7fa..e8a547b 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/gputasks/GPUEdgeDetection.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/gputasks/GPUEdgeDetection.java @@ -26,13 +26,16 @@ public class GPUEdgeDetection { org.bytedeco.opencv.opencv_core.Mat javacvMat = new org.bytedeco.opencv.opencv_core.Mat(); this.converter.convert(img, javacvMat); long time; - try (GpuMat gpuImg = new GpuMat(); GpuMat gpuEdges = new GpuMat(); CannyEdgeDetector detector = opencv_cudaimgproc.createCannyEdgeDetector(20, 60)) { + try (GpuMat gpuImg = new GpuMat(); GpuMat gpuEdges = new GpuMat(); CannyEdgeDetector detector = opencv_cudaimgproc.createCannyEdgeDetector(35, 100)) { gpuImg.upload(javacvMat); time = TimingService.measureTime(() -> detector.detect(gpuImg, gpuEdges), 1000); - gpuEdges.download(javacvMat); + +// gpuEdges.download(javacvMat); +// opencv_highgui.imshow("GPU edges", javacvMat); +// opencv_highgui.waitKey(); } - LOG.info(() -> "gpu execution time: " + time + "ms"); + LOG.info(() -> "gpu execution timefor image of size " + img.size() + ": " + time + "ms"); } } diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/gputasks/GPUFeatureMatching.java b/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/gputasks/GPUFeatureMatching.java index 907d107..5b60170 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/gputasks/GPUFeatureMatching.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/gputasks/GPUFeatureMatching.java @@ -2,21 +2,14 @@ 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; import de.unidue.nes.gpu.utils.TimingService; @@ -34,64 +27,48 @@ public class 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); - // Watch out:https://github.com/opencv/opencv/issues/10573 - Imgproc.resize(_originalImage, _originalImage, new Size(), 15, 15); - Imgproc.resize(_wrongImage, _wrongImage, new Size(), 15, 15); - Imgproc.resize(_correctImage, _correctImage, new Size(), 15, 15); + public void matchFeatures(org.opencv.core.Mat[] images) { + if (images.length != 2) { + throw new RuntimeException("This should not happen"); + } + // Watch out:https://github.com/opencv/opencv/issues/10573 --> if picture too + // small, crash - 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); + Mat first = new Mat(); + Mat second = new Mat(); + this.converter.convert(images[0], first); + this.converter.convert(images[1], second); try (GpuMat gpuImg = new GpuMat(); - GpuMat gpuWrongImg = new GpuMat(); - GpuMat gpuCorrectImg = new GpuMat(); + GpuMat gpuImg2 = new GpuMat(); GpuMat mask = new GpuMat(); GpuMat imgDescriptors = new GpuMat(); - GpuMat testDescriptors = new GpuMat()) { + GpuMat testDescriptors = new GpuMat(); + ORB orb = ORB.create(); + DescriptorMatcher matcher = DescriptorMatcher.createBFMatcher(NORM_HAMMING); + KeyPointVector keypoints = new KeyPointVector(); + KeyPointVector testKeyPoints = new KeyPointVector(); + DMatchVector matches = new DMatchVector()) { - 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(); + gpuImg.upload(first); + gpuImg2.upload(second); long time = TimingService.measureTime(() -> { orb.detectAndCompute(gpuImg, mask, keypoints, imgDescriptors); - orb.detectAndCompute(gpuWrongImg, mask, testKeyPoints, testDescriptors); + orb.detectAndCompute(gpuImg2, mask, testKeyPoints, testDescriptors); matcher.match(testDescriptors, imgDescriptors, matches); - List 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 - } - }, 50); + }, 100); + + // Just for debugging + +// Mat outImg = new Mat(); +// opencv_features2d.drawMatches(first, keypoints, second, testKeyPoints, matches, outImg); +// opencv_imgproc.resize(outImg, outImg, new Size((int) (outImg.cols() * 0.25), (int) (outImg.rows() * 0.25))); +// opencv_highgui.imshow("matches", outImg); +// opencv_highgui.waitKey(); + + LOG.info(() -> "gpu execution time for images of size " + images[0].size() + ": " + time + "ms"); - LOG.info(() -> "execution time: " + time + "ms"); } } diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/taskdefinitions/EdgeDetectionTask.java b/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/taskdefinitions/EdgeDetectionTask.java index bb480d0..37593bf 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/taskdefinitions/EdgeDetectionTask.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/taskdefinitions/EdgeDetectionTask.java @@ -1,13 +1,14 @@ package de.unidue.nes.gpu.tasks.taskdefinitions; +import java.util.Arrays; + import org.opencv.core.Mat; -import org.opencv.core.Size; -import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import de.unidue.nes.gpu.tasks.Task; import de.unidue.nes.gpu.tasks.cputasks.CPUEdgeDetection; import de.unidue.nes.gpu.tasks.gputasks.GPUEdgeDetection; +import de.unidue.nes.gpu.utils.ImageLoader; /** * @author Roch P., Shahbaz Nejad B. @@ -25,16 +26,11 @@ public class EdgeDetectionTask implements Task { @Override public void runTask() { - Mat image = Imgcodecs.imread("src/main/resources/theSimpsons.jpg"); - Imgproc.resize(image, image, new Size(), 10, 10); - Imgproc.medianBlur(image, image, 5); - Mat gray = new Mat(); - Imgproc.cvtColor(image, gray, Imgproc.COLOR_BGR2GRAY); - Mat img = new Mat(); - gray.copyTo(img); - this.cpuTask.findEdges(img); - gray.copyTo(img); - this.gpuTask.findEdges(img); + Mat[] images = ImageLoader.getTestImages(); + Arrays.stream(images).forEach(img -> Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2GRAY)); + + Arrays.stream(images).forEach(this.cpuTask::findEdges); + Arrays.stream(images).forEach(this.gpuTask::findEdges); } } diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/taskdefinitions/FeatureMatchingTask.java b/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/taskdefinitions/FeatureMatchingTask.java index ed0e67b..ce6a83e 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/taskdefinitions/FeatureMatchingTask.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/tasks/taskdefinitions/FeatureMatchingTask.java @@ -1,8 +1,14 @@ package de.unidue.nes.gpu.tasks.taskdefinitions; +import java.util.Arrays; + +import org.opencv.core.Mat; +import org.opencv.imgproc.Imgproc; + import de.unidue.nes.gpu.tasks.Task; import de.unidue.nes.gpu.tasks.cputasks.CPUFeatureMatching; import de.unidue.nes.gpu.tasks.gputasks.GPUFeatureMatching; +import de.unidue.nes.gpu.utils.ImageLoader; /** * @author Roch P., Shahbaz Nejad B. @@ -19,8 +25,15 @@ public class FeatureMatchingTask implements Task { @Override public void runTask() { - this.cpuTask.matchFeatures(); - this.gpuTask.matchFeatures(); + Mat[][] stereoImages = ImageLoader.getStereoImages(); + for (int i = 0; i < stereoImages.length; i++) { + for (int j = 0; j < stereoImages[i].length; j++) { + Imgproc.cvtColor(stereoImages[i][j], stereoImages[i][j], Imgproc.COLOR_BGR2GRAY); + } + } + + Arrays.stream(stereoImages).forEach(this.cpuTask::matchFeatures); + Arrays.stream(stereoImages).forEach(this.gpuTask::matchFeatures); } } diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/utils/ImageLoader.java b/gpu-app/src/main/java/de/unidue/nes/gpu/utils/ImageLoader.java index 66069a3..24dba77 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/utils/ImageLoader.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/utils/ImageLoader.java @@ -13,4 +13,8 @@ public class ImageLoader { Imgcodecs.imread("src/main/resources/skyscraper_medium.jpg"), Imgcodecs.imread("src/main/resources/skyscraper_medium_large.jpg"), Imgcodecs.imread("src/main/resources/skyscraper_large.jpg") }; } + + public static Mat[][] getStereoImages() {// TODO: resize and test with different sizes + return new Mat[][] { { Imgcodecs.imread("src/main/resources/left.png"), Imgcodecs.imread("src/main/resources/right.png") } }; + } } diff --git a/gpu-app/src/main/resources/left_large.png b/gpu-app/src/main/resources/left_large.png new file mode 100644 index 0000000..e8e8e87 Binary files /dev/null and b/gpu-app/src/main/resources/left_large.png differ diff --git a/gpu-app/src/main/resources/left_medium.png b/gpu-app/src/main/resources/left_medium.png new file mode 100644 index 0000000..249e78d Binary files /dev/null and b/gpu-app/src/main/resources/left_medium.png differ diff --git a/gpu-app/src/main/resources/left_medium_large.png b/gpu-app/src/main/resources/left_medium_large.png new file mode 100644 index 0000000..c95f372 Binary files /dev/null and b/gpu-app/src/main/resources/left_medium_large.png differ diff --git a/gpu-app/src/main/resources/left_medium_small.png b/gpu-app/src/main/resources/left_medium_small.png new file mode 100644 index 0000000..f6828aa Binary files /dev/null and b/gpu-app/src/main/resources/left_medium_small.png differ diff --git a/gpu-app/src/main/resources/left_small.png b/gpu-app/src/main/resources/left_small.png new file mode 100644 index 0000000..466c40f Binary files /dev/null and b/gpu-app/src/main/resources/left_small.png differ