added logic for contour recognition

This commit is contained in:
2020-02-21 14:09:09 +01:00
parent d9c4e5e2ed
commit 1a62c12bd9
5 changed files with 102 additions and 3 deletions
@@ -11,8 +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.ConvertingTask;
import de.unidue.nes.gpu.tasks.taskdefinitions.UploadDownloadTask;
import de.unidue.nes.gpu.tasks.taskdefinitions.ContourRecognitionTask;
/**
* Class containing the main method of this project.
@@ -52,7 +51,7 @@ public class GpuTestSystem {
private List<Task> getTasks() {
return Arrays.asList(new ConvertingTask(), new UploadDownloadTask());
return Arrays.asList(new ContourRecognitionTask());
}
}
@@ -0,0 +1,58 @@
package de.unidue.nes.gpu.tasks.cputasks;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Scalar;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class CPUContourRecognition {
private static final Logger LOG = LogManager.getLogger(CPUContourRecognition.class);
public void findContours(Mat image) {
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
long time1 = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
Imgproc.findContours(image, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
contours.clear();
// hierarchy.create(0, 0, CvType.CV_8U);
}
long time2 = System.currentTimeMillis();
// drawContours(image, contours, hierarchy);
for (int i = 0; i < 1000; i++) {
Imgproc.findContours(image, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
contours.clear();
// hierarchy.create(0, 0, CvType.CV_8U);
}
// drawContours(image, contours, hierarchy);
long time3 = System.currentTimeMillis();
LOG.info(() -> "External Contours: " + (time2 - time1) + "ms");
LOG.info(() -> "All Contours: " + (time3 - time2) + "ms");
}
private void drawContours(Mat image, List<MatOfPoint> contours, Mat hierarchy) {
HighGui.imshow("original image", image);
Mat contImg = new Mat(image.width(), image.height(), CvType.CV_8UC3);
contImg.setTo(new Scalar(0, 0, 0));
for (int i = 0; i < contours.size(); i++) {
Imgproc.drawContours(contImg, contours, i, new Scalar(0, 0, 255));
}
HighGui.imshow("contour image", contImg);
HighGui.waitKey();
}
}
@@ -0,0 +1,13 @@
package de.unidue.nes.gpu.tasks.gputasks;
import org.opencv.core.Mat;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class GPUContourRecognition {
public void findContours(Mat image) {
throw new UnsupportedOperationException("No GPU contour algorithm");
}
}
@@ -1,15 +1,44 @@
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.CPUContourRecognition;
import de.unidue.nes.gpu.tasks.gputasks.GPUContourRecognition;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class ContourRecognitionTask implements Task {
private CPUContourRecognition cpuTask;
private GPUContourRecognition gpuTask;
public ContourRecognitionTask() {
this.cpuTask = new CPUContourRecognition();
this.gpuTask = new GPUContourRecognition();
}
@Override
public void runTask() {
Mat image = loadTestImage();
Mat gray = new Mat();
// change the image so it can be used for finding contours
Imgproc.cvtColor(image, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(gray, gray, 64, 255, Imgproc.THRESH_BINARY_INV);
Mat contImg = new Mat();
gray.copyTo(contImg);
this.cpuTask.findContours(contImg);
// gray.copyTo(contImg);
// this.gpuTask.findContours(contImg);
}
private Mat loadTestImage() {
return Imgcodecs.imread("src/main/resources/contourImage.png");
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB