added findContours (only CPU) and another convenience class

This commit is contained in:
2020-05-11 12:35:25 +02:00
parent 681ff9879f
commit 1c81151e67
4 changed files with 144 additions and 11 deletions
@@ -1,15 +1,17 @@
package de.unidue.nes.gpu.testframework;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import de.unidue.nes.gpu.testframework.input.MatArrayInput;
import de.unidue.nes.gpu.testframework.opencv.Imgproc;
import de.unidue.nes.gpu.testframework.opencv.MatFunctions;
import de.unidue.nes.gpu.testframework.opencv.StereoBM;
import de.unidue.nes.gpu.testframework.tasks.CpuTaskImpl;
import de.unidue.nes.gpu.testframework.tasks.CustomTask;
import de.unidue.nes.gpu.testframework.tasks.DummyTask;
@@ -28,18 +30,21 @@ public class Main {
TestUtils<Mat[]> utils = new TestUtils<>();
utils.setCommandPipeline(arr -> {
Mat first = arr.get()[0];
Mat second = arr.get()[1];
Imgproc.cvtColor(first, first, Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(second, second, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(first, first, 128, 255, Imgproc.THRESH_BINARY);
StereoBM sbm = StereoBM.create();
Mat disparity = new Mat();
sbm.compute(first, second, disparity);
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(first, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
MatFunctions.convertTo(disparity, disparity, CvType.CV_8U, 0.5);
Imgproc.cvtColor(first, first, Imgproc.COLOR_GRAY2BGR);
CustomTask.addTask(new CpuTaskImpl(() -> {
for (int i = 0; i < contours.size(); i++) {
org.opencv.imgproc.Imgproc.drawContours(first, contours, i, new Scalar(0, 0, 255), 2);
}
}, "drawContours"));
CustomTask.addTask(new DummyTask(disparity));
CustomTask.addTask(new DummyTask(first));
});
utils.setInput(new MatArrayInput(Imgcodecs.imread("src/main/resources/left_small.png"), Imgcodecs.imread("src/main/resources/right_small.png")));
// utils.setInput(new MatArrayInput(Imgcodecs.imread("src/main/resources/left_small.png"), Imgcodecs.imread("src/main/resources/right_small.png")));
@@ -0,0 +1,72 @@
package de.unidue.nes.gpu.testframework.internal.tasks.imgproc;
import java.util.List;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.imgproc.Imgproc;
import de.unidue.nes.gpu.testframework.tasks.CpuTask;
import de.unidue.nes.gpu.testframework.tasks.GpuTask;
import de.unidue.nes.gpu.testframework.tasks.Task;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class FindContours implements Task {
private final Mat image;
private final List<MatOfPoint> contours;
private final Mat hierarchy;
private final int mode;
private final int method;
private final Mat originalImage;
private final Mat originalHierarchy;
private String descr;
public FindContours(Mat image, List<MatOfPoint> contours, Mat hierarchy, int mode, int method) {
this.image = image;
this.contours = contours;
this.hierarchy = hierarchy;
this.mode = mode;
this.method = method;
this.originalImage = new Mat();
this.image.copyTo(this.originalImage);
this.originalHierarchy = new Mat();
this.hierarchy.copyTo(this.originalHierarchy);
}
@Override
public CpuTask toCpuTask() {
return () -> {
Imgproc.findContours(this.image, this.contours, this.hierarchy, this.mode, this.method);
};
}
@Override
public GpuTask toGpuTask() {
return null;// Not possible on GPU
}
@Override
public String getDescription() {
return this.descr;
}
@Override
public void reset() {
this.originalImage.copyTo(this.image);
this.originalHierarchy.copyTo(this.hierarchy);
this.contours.clear();
}
@Override
public void initDescription() {
this.descr = "Imgproc.findContours(" + this.image + ", " + this.contours + ", " + this.hierarchy + ", " + this.mode + ", " + this.method + ")";
}
}
@@ -1,11 +1,15 @@
package de.unidue.nes.gpu.testframework.opencv;
import java.util.List;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Size;
import de.unidue.nes.gpu.testframework.internal.pipeline.TestPipeline;
import de.unidue.nes.gpu.testframework.internal.tasks.imgproc.Canny;
import de.unidue.nes.gpu.testframework.internal.tasks.imgproc.CvtColor;
import de.unidue.nes.gpu.testframework.internal.tasks.imgproc.FindContours;
import de.unidue.nes.gpu.testframework.internal.tasks.imgproc.GaussianBlur;
import de.unidue.nes.gpu.testframework.internal.tasks.imgproc.MedianBlur;
import de.unidue.nes.gpu.testframework.internal.tasks.imgproc.Resize;
@@ -67,6 +71,10 @@ public class Imgproc {
public static final int THRESH_BINARY = 0, THRESH_BINARY_INV = 1, THRESH_TRUNC = 2, THRESH_TOZERO = 3, THRESH_TOZERO_INV = 4, THRESH_MASK = 7,
THRESH_OTSU = 8, THRESH_TRIANGLE = 16;
public static final int RETR_EXTERNAL = 0, RETR_LIST = 1, RETR_CCOMP = 2, RETR_TREE = 3, RETR_FLOODFILL = 4;
public static final int CHAIN_APPROX_NONE = 1, CHAIN_APPROX_SIMPLE = 2, CHAIN_APPROX_TC89_L1 = 3, CHAIN_APPROX_TC89_KCOS = 4;
private Imgproc() {
}
@@ -118,4 +126,9 @@ public class Imgproc {
public static void resize(Mat src, Mat dst, Size dsize, double fx, double fy, int interpolation) {
TestPipeline.INSTANCE.addTask(new Resize(src, dst, dsize, fx, fy, interpolation));
}
public static void findContours(Mat image, List<MatOfPoint> contours, Mat hierarchy, int mode, int method) {
TestPipeline.INSTANCE.addTask(new FindContours(image, contours, hierarchy, mode, method));
}
}
@@ -0,0 +1,43 @@
package de.unidue.nes.gpu.testframework.tasks;
/**
* A class to reduce boilerplate code
*
* @author Roch P., Shahbaz Nejad B.
*/
public class CpuTaskImpl implements Task {
private final CpuTask delegate;
private final String description;
public CpuTaskImpl(CpuTask delegate, String description) {
this.delegate = delegate;
this.description = description;
}
@Override
public CpuTask toCpuTask() {
return this.delegate;
}
@Override
public GpuTask toGpuTask() {
return null;
}
@Override
public String getDescription() {
return this.description;
}
@Override
public void reset() {
// Nothing to reset
}
@Override
public void initDescription() {
// Description already initialized
}
}