implemented edge detection

This commit is contained in:
2020-02-21 15:25:26 +01:00
parent 1a62c12bd9
commit 310e63a975
6 changed files with 114 additions and 3 deletions
@@ -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.ContourRecognitionTask;
import de.unidue.nes.gpu.tasks.taskdefinitions.EdgeDetectionTask;
/**
* Class containing the main method of this project.
@@ -51,7 +51,7 @@ public class GpuTestSystem {
private List<Task> getTasks() {
return Arrays.asList(new ContourRecognitionTask());
return Arrays.asList(new EdgeDetectionTask());
}
}
@@ -0,0 +1,27 @@
package de.unidue.nes.gpu.tasks.cputasks;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class CPUEdgeDetection {
private static final Logger LOG = LogManager.getLogger(CPUEdgeDetection.class);
public void findEdges(Mat img) {
Mat edges = new Mat();
long time = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
Imgproc.Canny(img, edges, 20, 60);
}
long time2 = System.currentTimeMillis();
LOG.info(() -> "execution time: " + (time2 - time) + "ms");
}
}
@@ -0,0 +1,42 @@
package de.unidue.nes.gpu.tasks.gputasks;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bytedeco.opencv.global.opencv_cudaimgproc;
import org.bytedeco.opencv.opencv_core.GpuMat;
import org.bytedeco.opencv.opencv_cudaimgproc.CannyEdgeDetector;
import org.opencv.core.Mat;
import de.unidue.nes.gpu.utils.MatConverter;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class GPUEdgeDetection {
private static final Logger LOG = LogManager.getLogger(GPUEdgeDetection.class);
private MatConverter converter;
public GPUEdgeDetection() {
this.converter = new MatConverter();
}
public void findEdges(Mat img) {
org.bytedeco.opencv.opencv_core.Mat javacvMat = new org.bytedeco.opencv.opencv_core.Mat();
this.converter.convert(img, javacvMat);
long time;
long time2;
try (GpuMat gpuImg = new GpuMat(); GpuMat gpuEdges = new GpuMat(); CannyEdgeDetector detector = opencv_cudaimgproc.createCannyEdgeDetector(20, 60)) {
gpuImg.upload(javacvMat);
time = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
detector.detect(gpuImg, gpuEdges);
}
time2 = System.currentTimeMillis();
gpuEdges.download(javacvMat);
}
LOG.info(() -> "gpu execution time: " + (time2 - time) + "ms");
}
}
@@ -0,0 +1,19 @@
/**
*
*/
package de.unidue.nes.gpu.tasks.taskdefinitions;
import de.unidue.nes.gpu.tasks.Task;
/**
* @author Roch P., Shahbaz Nejad B.
*
*/
public class ColoringTask implements Task {
@Override
public void runTask() {
}
}
@@ -1,15 +1,38 @@
package de.unidue.nes.gpu.tasks.taskdefinitions;
import org.opencv.core.Mat;
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;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class EdgeDetectionTask implements Task {
private CPUEdgeDetection cpuTask;
private GPUEdgeDetection gpuTask;
public EdgeDetectionTask() {
this.cpuTask = new CPUEdgeDetection();
this.gpuTask = new GPUEdgeDetection();
}
@Override
public void runTask() {
Mat image = Imgcodecs.imread("src/main/resources/theSimpsons.jpg");
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);
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB