From 99b3bfae9c2b64912adb62db646b5d0b366466ce Mon Sep 17 00:00:00 2001 From: Peter Roch Date: Thu, 7 May 2020 11:50:19 +0200 Subject: [PATCH] added implementation for ORB related functions --- .../internal/tasks/orb/ORB_create.java | 89 ++++++++++++++- .../tasks/orb/ORB_detectAndCompute.java | 108 +++++++++++++++++- .../internal/tasks/orb/ORBs.java | 35 ++++++ 3 files changed, 221 insertions(+), 11 deletions(-) diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/orb/ORB_create.java b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/orb/ORB_create.java index 349e79d..a00a6f0 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/orb/ORB_create.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/orb/ORB_create.java @@ -9,31 +9,108 @@ import de.unidue.nes.gpu.testframework.tasks.Task; */ public class ORB_create implements Task { - public ORB_create(int id) { + private final int numParams; + private final int nfeatures; + + private final float scaleFactor; + + public ORB_create(int id) { + this.numParams = 0; + this.nfeatures = -1; + this.scaleFactor = -1; + ORBs.addORB(org.opencv.features2d.ORB.create(500, 1.2f), id); + ORBs.addCudaORB(org.bytedeco.opencv.opencv_cudafeatures2d.ORB.create(500, 1.2f, 8, 31, 0, 2, org.opencv.features2d.ORB.HARRIS_SCORE, 31, 20, true), id); } public ORB_create(int id, int nfeatures) { - + this.numParams = 1; + this.nfeatures = nfeatures; + this.scaleFactor = -1; + ORBs.addORB(org.opencv.features2d.ORB.create(nfeatures, 1.2f), id); + ORBs.addCudaORB( + org.bytedeco.opencv.opencv_cudafeatures2d.ORB.create(nfeatures, 1.2f, 8, 31, 0, 2, org.opencv.features2d.ORB.HARRIS_SCORE, 31, 20, true), id); } public ORB_create(int id, int nfeatures, float scaleFactor) { - + this.numParams = 2; + this.nfeatures = nfeatures; + this.scaleFactor = scaleFactor; + ORBs.addORB(org.opencv.features2d.ORB.create(nfeatures, scaleFactor), id); + ORBs.addCudaORB( + org.bytedeco.opencv.opencv_cudafeatures2d.ORB.create(nfeatures, scaleFactor, 8, 31, 0, 2, org.opencv.features2d.ORB.HARRIS_SCORE, 31, 20, true), + id); } @Override public CpuTask toCpuTask() { - return null; + return () -> { + switch (this.numParams) { + case 0: + org.opencv.features2d.ORB.create(); + break; + case 1: + org.opencv.features2d.ORB.create(this.nfeatures); + break; + case 2: + org.opencv.features2d.ORB.create(this.nfeatures, this.scaleFactor); + break; + default: + throw new RuntimeException(); + } + }; } @Override public GpuTask toGpuTask() { - return null; + return new GpuTask() { + + private org.bytedeco.opencv.opencv_cudafeatures2d.ORB theOrb; + + @Override + public void run() { + switch (ORB_create.this.numParams) { + case 0: + this.theOrb = org.bytedeco.opencv.opencv_cudafeatures2d.ORB.create(500, 1.2f, 8, 31, 0, 2, org.opencv.features2d.ORB.HARRIS_SCORE, 31, 20, + true); + break; + case 1: + this.theOrb = org.bytedeco.opencv.opencv_cudafeatures2d.ORB.create(ORB_create.this.nfeatures, 1.2f, 8, 31, 0, 2, + org.opencv.features2d.ORB.HARRIS_SCORE, 31, 20, true); + break; + case 2: + this.theOrb = org.bytedeco.opencv.opencv_cudafeatures2d.ORB.create(ORB_create.this.nfeatures, ORB_create.this.scaleFactor, 8, 31, 0, 2, + org.opencv.features2d.ORB.HARRIS_SCORE, 31, 20, true); + break; + default: + throw new RuntimeException(); + } + } + + @Override + public void init() { + // Nothing to init + } + + @Override + public void cleanUp() { + this.theOrb.close(); + } + }; } @Override public String getDescription() { - return null; + switch (this.numParams) { + case 0: + return "ORB.create()"; + case 1: + return "ORB.create(" + this.nfeatures + ")"; + case 2: + return "ORB.create(" + this.nfeatures + ", " + this.scaleFactor + ")"; + default: + throw new RuntimeException(); + } } @Override diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/orb/ORB_detectAndCompute.java b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/orb/ORB_detectAndCompute.java index 30b25d9..038fe56 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/orb/ORB_detectAndCompute.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/orb/ORB_detectAndCompute.java @@ -1,8 +1,12 @@ package de.unidue.nes.gpu.testframework.internal.tasks.orb; +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.tasks.CpuTask; import de.unidue.nes.gpu.testframework.tasks.GpuTask; import de.unidue.nes.gpu.testframework.tasks.Task; @@ -12,28 +16,122 @@ import de.unidue.nes.gpu.testframework.tasks.Task; */ public class ORB_detectAndCompute implements Task { - public ORB_detectAndCompute(int id, Mat image, Mat mask, MatOfKeyPoint keypoints, Mat descriptors) { + private final int id; + private final ORB theOrb; + + private final Mat image; + private final Mat mask; + private final MatOfKeyPoint keypoints; + private final Mat descriptors; + + private final Mat originalImage; + private final Mat originalMask; + private final MatOfKeyPoint originalKeypoints; + private final Mat originalDescriptors; + + public ORB_detectAndCompute(int id, Mat image, Mat mask, MatOfKeyPoint keypoints, Mat descriptors) { + this.id = id; + + this.theOrb = ORBs.getById(this.id); + + this.image = image; + this.mask = mask; + this.keypoints = keypoints; + this.descriptors = descriptors; + + this.originalImage = new Mat(); + this.image.copyTo(this.originalImage); + + this.originalMask = new Mat(); + this.mask.copyTo(this.originalMask); + + this.originalKeypoints = new MatOfKeyPoint(); + this.keypoints.copyTo(this.originalKeypoints); + + this.originalDescriptors = new Mat(); + this.descriptors.copyTo(this.originalDescriptors); } @Override public CpuTask toCpuTask() { - return null; + return () -> this.theOrb.detectAndCompute(this.image, this.mask, this.keypoints, this.descriptors); } @Override public GpuTask toGpuTask() { - return null; + 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 GpuMat gpuMask; + + private KeyPointVector keypointsAsJavaCVKeyPointVector; + + private GpuMat gpuDescriptors; + + @Override + public void run() { + cudaOrb.detectAndCompute(this.gpuImage, this.gpuMask, this.keypointsAsJavaCVKeyPointVector, this.gpuDescriptors); + } + + @Override + public void init() { + this.gpuImage = MatPool.getUploadedGpuMat(ORB_detectAndCompute.this.image, ORB_detectAndCompute.this); + this.gpuMask = MatPool.getUploadedGpuMat(ORB_detectAndCompute.this.mask, ORB_detectAndCompute.this); + this.gpuDescriptors = MatPool.getUploadedGpuMat(ORB_detectAndCompute.this.descriptors, ORB_detectAndCompute.this); + + this.keypointsAsJavaCVKeyPointVector = createKeyPointVector(ORB_detectAndCompute.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_detectAndCompute.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 null; + return "ORB.detectAndCompute(" + this.image + ", " + this.mask + ", " + this.keypoints + ", " + this.descriptors + ")"; } @Override public void reset() { - + this.originalImage.copyTo(this.image); + this.originalMask.copyTo(this.mask); + this.originalKeypoints.copyTo(this.keypoints); + this.originalDescriptors.copyTo(this.descriptors); } } diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/orb/ORBs.java b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/orb/ORBs.java index 0866b96..5aa619c 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/orb/ORBs.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/internal/tasks/orb/ORBs.java @@ -1,8 +1,43 @@ package de.unidue.nes.gpu.testframework.internal.tasks.orb; +import java.util.HashMap; +import java.util.Map; + /** * @author Roch P., Shahbaz Nejad B. */ public class ORBs { + private static final Map ORB_MAP = new HashMap<>(); + + private static final Map CUDA_ORB_MAP = new HashMap<>(); + + public static void addORB(org.opencv.features2d.ORB orb, int id) { + if (ORB_MAP.containsKey(id)) { + throw new RuntimeException(); + } + ORB_MAP.put(id, orb); + } + + public static void addCudaORB(org.bytedeco.opencv.opencv_cudafeatures2d.ORB orb, int id) { + if (CUDA_ORB_MAP.containsKey(id)) { + throw new RuntimeException(); + } + CUDA_ORB_MAP.put(id, orb); + } + + public static org.opencv.features2d.ORB getById(int id) { + if (!ORB_MAP.containsKey(id)) { + throw new RuntimeException(); + } + return ORB_MAP.get(id); + } + + public static org.bytedeco.opencv.opencv_cudafeatures2d.ORB getCudaOrbById(int id) { + if (!CUDA_ORB_MAP.containsKey(id)) { + return null; + } + return CUDA_ORB_MAP.get(id); + } + }