added convertTo, which is useful when dealing with StereoBM

This commit is contained in:
2020-05-08 17:08:09 +02:00
parent f0e5f51fcb
commit 681ff9879f
3 changed files with 183 additions and 40 deletions
@@ -2,18 +2,14 @@ package de.unidue.nes.gpu.testframework;
import java.time.Instant;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import de.unidue.nes.gpu.testframework.input.MatArrayInput;
import de.unidue.nes.gpu.testframework.opencv.DescriptorMatcher;
import de.unidue.nes.gpu.testframework.opencv.Features2d;
import de.unidue.nes.gpu.testframework.opencv.Imgproc;
import de.unidue.nes.gpu.testframework.opencv.MatFunctions;
import de.unidue.nes.gpu.testframework.opencv.ORB;
import de.unidue.nes.gpu.testframework.opencv.StereoBM;
import de.unidue.nes.gpu.testframework.tasks.CustomTask;
import de.unidue.nes.gpu.testframework.tasks.DummyTask;
@@ -33,47 +29,19 @@ public class Main {
utils.setCommandPipeline(arr -> {
Mat first = arr.get()[0];
Mat second = arr.get()[1];
Mat third = new Mat();
MatFunctions.copyTo(first, third);
Imgproc.resize(third, third, new Size(), 1.5, 1.5);
Imgproc.cvtColor(first, first, Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(second, second, Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(third, third, Imgproc.COLOR_BGR2GRAY);
ORB orb = ORB.create(2500);
StereoBM sbm = StereoBM.create();
Mat disparity = new Mat();
sbm.compute(first, second, disparity);
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
MatOfKeyPoint keypoints3 = new MatOfKeyPoint();
MatFunctions.convertTo(disparity, disparity, CvType.CV_8U, 0.5);
orb.detect(first, keypoints1);
orb.detect(second, keypoints2);
orb.detect(third, keypoints3);
Mat descriptors1 = new Mat();
Mat descriptors2 = new Mat();
Mat descriptors3 = new Mat();
orb.compute(first, keypoints1, descriptors1);
orb.compute(second, keypoints2, descriptors2);
orb.compute(third, keypoints3, descriptors3);
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
MatOfDMatch matches1 = new MatOfDMatch();
matcher.match(descriptors2, descriptors1, matches1);
MatOfDMatch matches2 = new MatOfDMatch();
matcher.match(descriptors3, descriptors1, matches2);
Mat out1 = new Mat();
Mat out2 = new Mat();
Features2d.drawMatches(first, keypoints1, second, keypoints2, matches1, out1);
Features2d.drawMatches(first, keypoints1, third, keypoints3, matches2, out2);
CustomTask.addTask(new DummyTask(out1, out2));
CustomTask.addTask(new DummyTask(disparity));
});
utils.setInput(new MatArrayInput(Imgcodecs.imread("src/main/resources/left.png"), Imgcodecs.imread("src/main/resources/right.png")));
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")));
// utils.setInput(new MatInput(Imgcodecs.imread("src/main/resources/car.jpg")));
// utils.setInput(new MatInput(Imgcodecs.imread("src/main/resources/skyscraper_small.jpg")));
@@ -0,0 +1,162 @@
package de.unidue.nes.gpu.testframework.internal.tasks.mat;
import org.bytedeco.opencv.opencv_core.GpuMat;
import org.opencv.core.Mat;
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;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class ConvertTo implements Task {
private final int numParams;
private final Mat _this;
private final Mat m;
private final int rtype;
private final double alpha;
private final double beta;
private final Mat original_this;
private final Mat originalM;
private String descr = "";
public ConvertTo(Mat _this, Mat m, int rtype) {
this.numParams = 3;
this._this = _this;
this.m = m;
this.rtype = rtype;
this.alpha = -1;
this.beta = -1;
this.original_this = new Mat();
this._this.copyTo(this.original_this);
this.originalM = new Mat();
this.m.copyTo(this.originalM);
}
public ConvertTo(Mat _this, Mat m, int rtype, double alpha) {
this.numParams = 4;
this._this = _this;
this.m = m;
this.rtype = rtype;
this.alpha = alpha;
this.beta = -1;
this.original_this = new Mat();
this._this.copyTo(this.original_this);
this.originalM = new Mat();
this.m.copyTo(this.originalM);
}
public ConvertTo(Mat _this, Mat m, int rtype, double alpha, double beta) {
this.numParams = 5;
this._this = _this;
this.m = m;
this.rtype = rtype;
this.alpha = alpha;
this.beta = beta;
this.original_this = new Mat();
this._this.copyTo(this.original_this);
this.originalM = new Mat();
this.m.copyTo(this.originalM);
}
@Override
public CpuTask toCpuTask() {
return () -> {
switch (this.numParams) {
case 3:
this._this.convertTo(this.m, this.rtype);
break;
case 4:
this._this.convertTo(this.m, this.rtype, this.alpha);
break;
case 5:
this._this.convertTo(this.m, this.rtype, this.alpha, this.beta);
break;
default:
throw new RuntimeException();
}
};
}
@Override
public GpuTask toGpuTask() {
return new GpuTask() {
private GpuMat gpu_this;
private GpuMat gpuM;
@Override
public void run() {
switch (ConvertTo.this.numParams) {
case 3:
this.gpu_this.convertTo(this.gpuM, ConvertTo.this.rtype);
break;
case 4:
this.gpu_this.convertTo(this.gpuM, ConvertTo.this.rtype, ConvertTo.this.alpha);
break;
case 5:
this.gpu_this.convertTo(this.gpuM, ConvertTo.this.rtype, ConvertTo.this.alpha, ConvertTo.this.beta);
break;
default:
throw new RuntimeException();
}
}
@Override
public void init() {
this.gpu_this = MatPool.getUploadedGpuMat(ConvertTo.this._this, ConvertTo.this);
this.gpuM = MatPool.getUploadedGpuMat(ConvertTo.this.m, ConvertTo.this);
}
@Override
public void cleanUp() {
// Nothing to clean up
}
};
}
@Override
public String getDescription() {
return this.descr;
}
@Override
public void reset() {
this.original_this.copyTo(this._this);
this.originalM.copyTo(this.m);
}
@Override
public void initDescription() {
switch (this.numParams) {
case 3:
this.descr = this._this + ".convertTo(" + this.m + ", " + this.rtype + ")";
break;
case 4:
this.descr = this._this + ".convertTo(" + this.m + ", " + this.rtype + ", " + this.alpha + ")";
break;
case 5:
this.descr = this._this + ".convertTo(" + this.m + ", " + this.rtype + ", " + this.alpha + ", " + this.beta + ")";
break;
default:
throw new RuntimeException();
}
}
}
@@ -4,6 +4,7 @@ import org.opencv.core.Mat;
import de.unidue.nes.gpu.testframework.internal.pipeline.TestPipeline;
import de.unidue.nes.gpu.testframework.internal.tasks.mat.Clone;
import de.unidue.nes.gpu.testframework.internal.tasks.mat.ConvertTo;
import de.unidue.nes.gpu.testframework.internal.tasks.mat.CopyTo;
/**
@@ -26,4 +27,16 @@ public class MatFunctions {
TestPipeline.INSTANCE.addTask(clone);
return clone.getClone();
}
public static void convertTo(Mat _this, Mat m, int rtype) {
TestPipeline.INSTANCE.addTask(new ConvertTo(_this, m, rtype));
}
public static void convertTo(Mat _this, Mat m, int rtype, double alpha) {
TestPipeline.INSTANCE.addTask(new ConvertTo(_this, m, rtype, alpha));
}
public static void convertTo(Mat _this, Mat m, int rtype, double alpha, double beta) {
TestPipeline.INSTANCE.addTask(new ConvertTo(_this, m, rtype, alpha, beta));
}
}