added circle logic

This commit is contained in:
2020-02-26 14:29:46 +01:00
parent c2df9fea04
commit 4885538811
5 changed files with 150 additions and 2 deletions
@@ -11,6 +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.HoughCircleTask;
/**
* Class containing the main method of this project.
@@ -50,7 +51,7 @@ public class GpuTestSystem {
private List<Task> getTasks() {
return Arrays.asList();
return Arrays.asList(new HoughCircleTask());
}
}
@@ -0,0 +1,56 @@
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.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import de.unidue.nes.gpu.utils.TimingService;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class CPUHoughCircles {
private static final Logger LOG = LogManager.getLogger(CPUHoughCircles.class);
private static final String IMG_PATH = "src/main/resources/circlesImage.png";
private Mat[] testImages;
public CPUHoughCircles() {
this.testImages = new Mat[] { Imgcodecs.imread(IMG_PATH), Imgcodecs.imread(IMG_PATH), Imgcodecs.imread(IMG_PATH), Imgcodecs.imread(IMG_PATH) };
Imgproc.resize(this.testImages[1], this.testImages[1], new Size(), 2.5, 2.5);
Imgproc.resize(this.testImages[2], this.testImages[2], new Size(), 5, 5);
Imgproc.resize(this.testImages[3], this.testImages[3], new Size(), 10, 10);
Imgproc.cvtColor(this.testImages[0], this.testImages[0], Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(this.testImages[1], this.testImages[1], Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(this.testImages[2], this.testImages[2], Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(this.testImages[3], this.testImages[3], Imgproc.COLOR_BGR2GRAY);
}
public void detectCircles() {
Mat circles = new Mat();
long time1 = TimingService.measureTime(
() -> Imgproc.HoughCircles(this.testImages[0], circles, Imgproc.HOUGH_GRADIENT, 1, (double) this.testImages[0].rows() / 10, 64, 40), 50);
long time2 = TimingService.measureTime(
() -> Imgproc.HoughCircles(this.testImages[1], circles, Imgproc.HOUGH_GRADIENT, 1, (double) this.testImages[1].rows() / 10, 64, 50), 25);
long time3 = TimingService.measureTime(
() -> Imgproc.HoughCircles(this.testImages[2], circles, Imgproc.HOUGH_GRADIENT, 1, (double) this.testImages[2].rows() / 10, 64, 45), 10);
// not comparable to gpu
// long time4 = TimingService.measureTime(
// () -> Imgproc.HoughCircles(this.testImages[3], circles, Imgproc.HOUGH_GRADIENT, 1, (double) this.testImages[3].rows() / 10, 64, 50), 1);
LOG.info(() -> "original image: " + time1 + "ms");
LOG.info(() -> "2.5x original image: " + time2 + "ms");
LOG.info(() -> "5x original image: " + time3 + "ms");
// LOG.info(() -> "10x original image: " + time4 + "ms");
}
}
@@ -0,0 +1,72 @@
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.HoughCirclesDetector;
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.utils.MatConverter;
import de.unidue.nes.gpu.utils.TimingService;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class GPUHoughCircles {
private static final Logger LOG = LogManager.getLogger(GPUHoughCircles.class);
private static final String IMG_PATH = "src/main/resources/circlesImage.png";
private Mat[] testImages;
private MatConverter converter;
public GPUHoughCircles() {
this.testImages = new Mat[] { Imgcodecs.imread(IMG_PATH), Imgcodecs.imread(IMG_PATH), Imgcodecs.imread(IMG_PATH), Imgcodecs.imread(IMG_PATH) };
Imgproc.resize(this.testImages[1], this.testImages[1], new Size(), 2.5, 2.5);
Imgproc.resize(this.testImages[2], this.testImages[2], new Size(), 5, 5);
Imgproc.resize(this.testImages[3], this.testImages[3], new Size(), 10, 10);
Imgproc.cvtColor(this.testImages[0], this.testImages[0], Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(this.testImages[1], this.testImages[1], Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(this.testImages[2], this.testImages[2], Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(this.testImages[3], this.testImages[3], Imgproc.COLOR_BGR2GRAY);
this.converter = new MatConverter();
}
public void detectCircles() {
GpuMat circles = new GpuMat();
GpuMat gpuImg = new GpuMat();
HoughCirclesDetector detector1 = opencv_cudaimgproc.createHoughCirclesDetector(1, this.testImages[0].rows() / 10, 64, 40, 32, 256);
org.bytedeco.opencv.opencv_core.Mat tmp = this.converter.convert2Javacv(this.testImages[0]);
gpuImg.upload(tmp);
long time1 = TimingService.measureTime(() -> detector1.detect(gpuImg, circles), 50);
HoughCirclesDetector detector2 = opencv_cudaimgproc.createHoughCirclesDetector(1, this.testImages[1].rows() / 10, 64, 40, 32, 256);
tmp = this.converter.convert2Javacv(this.testImages[1]);
gpuImg.upload(tmp);
long time2 = TimingService.measureTime(() -> detector2.detect(gpuImg, circles), 25);
HoughCirclesDetector detector3 = opencv_cudaimgproc.createHoughCirclesDetector(1, this.testImages[2].rows() / 10, 64, 40, 32, 256);
tmp = this.converter.convert2Javacv(this.testImages[2]);
gpuImg.upload(tmp);
long time3 = TimingService.measureTime(() -> detector3.detect(gpuImg, circles), 10);
// throws out of memory error :(
// HoughCirclesDetector detector4 = opencv_cudaimgproc.createHoughCirclesDetector(1, this.testImages[3].rows() / 10, 64, 40, 32, 256);
// org.bytedeco.opencv.opencv_core.Mat tmp = this.converter.convert2Javacv(this.testImages[3]);
// gpuImg.upload(tmp);
// long time4 = TimingService.measureTime(() -> detector4.detect(gpuImg, circles), 1);
LOG.info(() -> "original image: " + time1 + "ms");
LOG.info(() -> "2.5x original image: " + time2 + "ms");
LOG.info(() -> "5x original image: " + time3 + "ms");
// LOG.info(() -> "10x original image: " + time4 + "ms");
}
}
@@ -1,8 +1,27 @@
package de.unidue.nes.gpu.tasks.taskdefinitions;
import de.unidue.nes.gpu.tasks.Task;
import de.unidue.nes.gpu.tasks.cputasks.CPUHoughCircles;
import de.unidue.nes.gpu.tasks.gputasks.GPUHoughCircles;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class HoughCircleTask {
public class HoughCircleTask implements Task {
private CPUHoughCircles cpuTask;
private GPUHoughCircles gpuTask;
public HoughCircleTask() {
this.cpuTask = new CPUHoughCircles();
this.gpuTask = new GPUHoughCircles();
}
@Override
public void runTask() {
this.cpuTask.detectCircles();
this.gpuTask.detectCircles();
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB