diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/Main.java b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/Main.java index bf294ec..2b616a1 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/Main.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/Main.java @@ -1,11 +1,17 @@ package de.unidue.nes.gpu.testframework; import org.opencv.core.Mat; +import org.opencv.core.MatOfDMatch; +import org.opencv.core.MatOfKeyPoint; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import de.unidue.nes.gpu.testframework.input.MatArrayInput; +import de.unidue.nes.gpu.testframework.opencv.DescriptorMatcher; +import de.unidue.nes.gpu.testframework.opencv.Features2d; import de.unidue.nes.gpu.testframework.opencv.Imgproc; +import de.unidue.nes.gpu.testframework.opencv.MatFunctions; +import de.unidue.nes.gpu.testframework.opencv.ORB; import de.unidue.nes.gpu.testframework.tasks.CustomTask; import de.unidue.nes.gpu.testframework.tasks.DummyTask; @@ -24,25 +30,55 @@ public class Main { TestUtils utils = new TestUtils<>(); utils.setCommandPipeline(arr -> { Mat first = arr.get()[0]; - Mat dst1 = new Mat(); - Mat dst2 = new Mat(); - Mat dst3 = new Mat(); + Mat second = arr.get()[1]; + Mat third = new Mat(); + MatFunctions.copyTo(first, third); + Imgproc.resize(third, third, new Size(), 1.5, 1.5); - Imgproc.resize(first, dst1, new Size(0, 0), 2, 2); - Imgproc.resize(dst1, dst2, new Size(), 2, 2); - Imgproc.resize(dst2, dst3, first.size()); + Imgproc.cvtColor(first, first, org.opencv.imgproc.Imgproc.COLOR_BGR2GRAY); + Imgproc.cvtColor(second, second, org.opencv.imgproc.Imgproc.COLOR_BGR2GRAY); + Imgproc.cvtColor(third, third, org.opencv.imgproc.Imgproc.COLOR_BGR2GRAY); - CustomTask.addTask(new DummyTask(dst3)); + ORB orb = ORB.create(2500); + + MatOfKeyPoint keypoints1 = new MatOfKeyPoint(); + MatOfKeyPoint keypoints2 = new MatOfKeyPoint(); + MatOfKeyPoint keypoints3 = new MatOfKeyPoint(); + + orb.detect(first, keypoints1); + orb.detect(second, keypoints2); + orb.detect(third, keypoints3); + + Mat descriptors1 = new Mat(); + Mat descriptors2 = new Mat(); + Mat descriptors3 = new Mat(); + + orb.compute(first, keypoints1, descriptors1); + orb.compute(second, keypoints2, descriptors2); + orb.compute(third, keypoints3, descriptors3); + + DescriptorMatcher matcher = DescriptorMatcher.create(org.opencv.features2d.DescriptorMatcher.BRUTEFORCE_HAMMING); + + MatOfDMatch matches1 = new MatOfDMatch(); + matcher.match(descriptors2, descriptors1, matches1); + MatOfDMatch matches2 = new MatOfDMatch(); + matcher.match(descriptors3, descriptors1, matches2); + + Mat out1 = new Mat(); + Mat out2 = new Mat(); + Features2d.drawMatches(first, keypoints1, second, keypoints2, matches1, out1); + Features2d.drawMatches(first, keypoints1, third, keypoints3, matches2, out2); + + CustomTask.addTask(new DummyTask(out1, out2)); }); - utils.setInput(new MatArrayInput(Imgcodecs.imread("src/main/resources/left_small.png"), Imgcodecs.imread("src/main/resources/skyscraper_small.jpg"), - Imgcodecs.imread("src/main/resources/car.jpg"))); + utils.setInput(new MatArrayInput(Imgcodecs.imread("src/main/resources/left.png"), Imgcodecs.imread("src/main/resources/right.png"))); // utils.setInput(new MatArrayInput(Imgcodecs.imread("src/main/resources/left_small.png"), Imgcodecs.imread("src/main/resources/right_small.png"))); // utils.setInput(new MatInput(Imgcodecs.imread("src/main/resources/car.jpg"))); // utils.setInput(new MatInput(Imgcodecs.imread("src/main/resources/skyscraper_small.jpg"))); utils.initializeCommands(); utils.warmup(10); - utils.runTests(50); + utils.runTests(20); utils.evaluate(); } } \ No newline at end of file diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/imgproc/CvtColor.java b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/imgproc/CvtColor.java index ec7e4b5..b060d92 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/imgproc/CvtColor.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/imgproc/CvtColor.java @@ -121,8 +121,9 @@ public class CvtColor implements Task { this.descr = "Imgproc.cvtColor(" + Objects.toString(this.src) + ", " + Objects.toString(this.dst) + ", " + this.code + ")"; } else if (this.numParams == 4) { this.descr = "Imgproc.cvtColor(" + Objects.toString(this.src) + ", " + Objects.toString(this.dst) + ", " + this.code + ", " + this.dstCn + ")"; + } else { + throw new RuntimeException(); } - throw new RuntimeException(); } } diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/DescriptorMatcher.java b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/DescriptorMatcher.java index dd933ac..750986e 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/DescriptorMatcher.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/DescriptorMatcher.java @@ -10,6 +10,8 @@ import de.unidue.nes.gpu.testframework.internal.tasks.descriptormatcher.Descript import de.unidue.nes.gpu.testframework.internal.tasks.descriptormatcher.DescriptorMatcher_match; /** + * A replacement for the DescriptorMatcher + * * @author Roch P., Shahbaz Nejad B. */ public class DescriptorMatcher { diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/ORB.java b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/ORB.java index 96638ba..889e56d 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/ORB.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/ORB.java @@ -46,4 +46,11 @@ public class ORB { TestPipeline.INSTANCE.addTask(new ORB_detectAndCompute(this.id, image, mask, keypoints, descriptors)); } + public void detect(Mat image, MatOfKeyPoint keypoints) { + TestPipeline.INSTANCE.addTask(new ORB_detect(this.id, image, keypoints)); + } + + public void compute(Mat image, MatOfKeyPoint keypoints, Mat descriptors) { + TestPipeline.INSTANCE.addTask(new ORB_compute(this.id, image, keypoints, descriptors)); + } } diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/ORB_compute.java b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/ORB_compute.java new file mode 100644 index 0000000..a781a7f --- /dev/null +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/ORB_compute.java @@ -0,0 +1,135 @@ +package de.unidue.nes.gpu.testframework.opencv; + +import org.bytedeco.opencv.opencv_core.GpuMat; +import org.bytedeco.opencv.opencv_core.KeyPointVector; +import org.opencv.core.Mat; +import org.opencv.core.MatOfKeyPoint; +import org.opencv.features2d.ORB; + +import de.unidue.nes.gpu.testframework.internal.mathandling.MatPool; +import de.unidue.nes.gpu.testframework.internal.tasks.orb.ORBs; +import de.unidue.nes.gpu.testframework.tasks.CpuTask; +import de.unidue.nes.gpu.testframework.tasks.GpuTask; +import de.unidue.nes.gpu.testframework.tasks.Task; + +/** + * ORB.compute + * + * @author Roch P., Shahbaz Nejad B. + */ +public class ORB_compute implements Task { + + private final int id; + + private final ORB theOrb; + + private final Mat image; + private final MatOfKeyPoint keypoints; + private final Mat descriptors; + + private final Mat originalImage; + private final MatOfKeyPoint originalKeypoints; + private final Mat originalDescriptors; + + private String descr = ""; + + public ORB_compute(int id, Mat image, MatOfKeyPoint keypoints, Mat descriptors) { + this.id = id; + this.image = image; + this.keypoints = keypoints; + this.descriptors = descriptors; + + this.originalImage = new Mat(); + image.copyTo(this.originalImage); + this.originalKeypoints = new MatOfKeyPoint(); + keypoints.copyTo(this.originalKeypoints); + this.originalDescriptors = new Mat(); + descriptors.copyTo(this.originalDescriptors); + + this.theOrb = ORBs.getById(id); + } + + @Override + public CpuTask toCpuTask() { + return () -> { + this.theOrb.compute(this.image, this.keypoints, this.descriptors); + }; + } + + @Override + public GpuTask toGpuTask() { + org.bytedeco.opencv.opencv_cudafeatures2d.ORB cudaOrb = ORBs.getCudaOrbById(this.id); + if (cudaOrb == null) { + return null;// no orb was created + } + return new GpuTask() { + + private GpuMat gpuImage; + + private KeyPointVector keypointsAsJavaCVKeyPointVector; + + private GpuMat gpuDescriptors; + + @Override + public void run() { + cudaOrb.compute(this.gpuImage, this.keypointsAsJavaCVKeyPointVector, this.gpuDescriptors); + } + + @Override + public void init() { + this.gpuImage = MatPool.getUploadedGpuMat(ORB_compute.this.image, ORB_compute.this); + this.gpuDescriptors = MatPool.getUploadedGpuMat(ORB_compute.this.descriptors, ORB_compute.this); + + this.keypointsAsJavaCVKeyPointVector = createKeyPointVector(ORB_compute.this.keypoints); + } + + private KeyPointVector createKeyPointVector(MatOfKeyPoint keypoints) { + org.opencv.core.KeyPoint[] opencvKeypoints = keypoints.toArray(); + org.bytedeco.opencv.opencv_core.KeyPoint[] javacvKeyPoints = new org.bytedeco.opencv.opencv_core.KeyPoint[opencvKeypoints.length]; + for (int i = 0; i < opencvKeypoints.length; i++) { + org.opencv.core.KeyPoint kp = opencvKeypoints[i]; + org.bytedeco.opencv.opencv_core.KeyPoint javacvKeyPoint = new org.bytedeco.opencv.opencv_core.KeyPoint((float) kp.pt.x, (float) kp.pt.y, + kp.size, kp.angle, kp.response, kp.octave, kp.class_id); + javacvKeyPoints[i] = javacvKeyPoint; + } + return new KeyPointVector(javacvKeyPoints); + } + + @Override + public void cleanUp() { + copyKeyPointVectorToKeypoints(this.keypointsAsJavaCVKeyPointVector, ORB_compute.this.keypoints); + this.keypointsAsJavaCVKeyPointVector.close(); + } + + private void copyKeyPointVectorToKeypoints(KeyPointVector vec, MatOfKeyPoint keypoints) { + org.bytedeco.opencv.opencv_core.KeyPoint[] javacvKeyPoints = vec.get(); + org.opencv.core.KeyPoint[] opencvKeypoints = new org.opencv.core.KeyPoint[javacvKeyPoints.length]; + for (int i = 0; i < javacvKeyPoints.length; i++) { + org.bytedeco.opencv.opencv_core.KeyPoint kp = javacvKeyPoints[i]; + org.opencv.core.KeyPoint opencvKeyPoint = new org.opencv.core.KeyPoint(kp.pt().x(), kp.pt().y(), kp.size(), kp.angle(), kp.response(), + kp.octave(), kp.class_id()); + opencvKeypoints[i] = opencvKeyPoint; + } + keypoints.fromArray(opencvKeypoints); + } + }; + } + + @Override + public String getDescription() { + return this.descr; + } + + @Override + public void reset() { + this.originalImage.copyTo(this.image); + this.originalKeypoints.copyTo(this.keypoints); + this.originalDescriptors.copyTo(this.descriptors); + } + + @Override + public void initDescription() { + this.descr = "ORB.compute(" + this.image + ", " + this.keypoints + ", " + this.descriptors + ")"; + } + +} diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/ORB_detect.java b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/ORB_detect.java new file mode 100644 index 0000000..1856796 --- /dev/null +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/opencv/ORB_detect.java @@ -0,0 +1,126 @@ +package de.unidue.nes.gpu.testframework.opencv; + +import org.bytedeco.opencv.opencv_core.GpuMat; +import org.bytedeco.opencv.opencv_core.KeyPointVector; +import org.opencv.core.Mat; +import org.opencv.core.MatOfKeyPoint; +import org.opencv.features2d.ORB; + +import de.unidue.nes.gpu.testframework.internal.mathandling.MatPool; +import de.unidue.nes.gpu.testframework.internal.tasks.orb.ORBs; +import de.unidue.nes.gpu.testframework.tasks.CpuTask; +import de.unidue.nes.gpu.testframework.tasks.GpuTask; +import de.unidue.nes.gpu.testframework.tasks.Task; + +/** + * ORB.detect + * + * @author Roch P., Shahbaz Nejad B. + */ +public class ORB_detect implements Task { + + private final int id; + + private final Mat image; + private final MatOfKeyPoint keypoints; + + private final Mat originalImage; + private final MatOfKeyPoint originalKeypoints; + + private final ORB theOrb; + + private String descr = ""; + + public ORB_detect(int id, Mat image, MatOfKeyPoint keypoints) { + this.id = id; + this.image = image; + this.keypoints = keypoints; + + this.originalImage = new Mat(); + this.originalKeypoints = new MatOfKeyPoint(); + this.image.copyTo(this.originalImage); + this.keypoints.copyTo(this.originalKeypoints); + + this.theOrb = ORBs.getById(id); + } + + @Override + public CpuTask toCpuTask() { + return () -> { + this.theOrb.detect(this.image, this.keypoints); + }; + } + + @Override + public GpuTask toGpuTask() { + org.bytedeco.opencv.opencv_cudafeatures2d.ORB cudaOrb = ORBs.getCudaOrbById(this.id); + if (cudaOrb == null) { + return null;// no orb was created + } + return new GpuTask() { + + private GpuMat gpuImage; + + private KeyPointVector keypointsAsJavaCVKeyPointVector; + + @Override + public void run() { + cudaOrb.detect(this.gpuImage, this.keypointsAsJavaCVKeyPointVector); + } + + @Override + public void init() { + this.gpuImage = MatPool.getUploadedGpuMat(ORB_detect.this.image, ORB_detect.this); + + this.keypointsAsJavaCVKeyPointVector = createKeyPointVector(ORB_detect.this.keypoints); + } + + private KeyPointVector createKeyPointVector(MatOfKeyPoint keypoints) { + org.opencv.core.KeyPoint[] opencvKeypoints = keypoints.toArray(); + org.bytedeco.opencv.opencv_core.KeyPoint[] javacvKeyPoints = new org.bytedeco.opencv.opencv_core.KeyPoint[opencvKeypoints.length]; + for (int i = 0; i < opencvKeypoints.length; i++) { + org.opencv.core.KeyPoint kp = opencvKeypoints[i]; + org.bytedeco.opencv.opencv_core.KeyPoint javacvKeyPoint = new org.bytedeco.opencv.opencv_core.KeyPoint((float) kp.pt.x, (float) kp.pt.y, + kp.size, kp.angle, kp.response, kp.octave, kp.class_id); + javacvKeyPoints[i] = javacvKeyPoint; + } + return new KeyPointVector(javacvKeyPoints); + } + + @Override + public void cleanUp() { + copyKeyPointVectorToKeypoints(this.keypointsAsJavaCVKeyPointVector, ORB_detect.this.keypoints); + this.keypointsAsJavaCVKeyPointVector.close(); + } + + private void copyKeyPointVectorToKeypoints(KeyPointVector vec, MatOfKeyPoint keypoints) { + org.bytedeco.opencv.opencv_core.KeyPoint[] javacvKeyPoints = vec.get(); + org.opencv.core.KeyPoint[] opencvKeypoints = new org.opencv.core.KeyPoint[javacvKeyPoints.length]; + for (int i = 0; i < javacvKeyPoints.length; i++) { + org.bytedeco.opencv.opencv_core.KeyPoint kp = javacvKeyPoints[i]; + org.opencv.core.KeyPoint opencvKeyPoint = new org.opencv.core.KeyPoint(kp.pt().x(), kp.pt().y(), kp.size(), kp.angle(), kp.response(), + kp.octave(), kp.class_id()); + opencvKeypoints[i] = opencvKeyPoint; + } + keypoints.fromArray(opencvKeypoints); + } + }; + } + + @Override + public String getDescription() { + return this.descr; + } + + @Override + public void reset() { + this.originalImage.copyTo(this.image); + this.originalKeypoints.copyTo(this.keypoints); + } + + @Override + public void initDescription() { + this.descr = "ORB.detect(" + this.image + ", " + this.keypoints + ")"; + } + +}