added test for converter and extended converting task
This commit is contained in:
@@ -5,7 +5,8 @@ import java.util.List;
|
||||
|
||||
import de.unidue.nes.gpu.initialization.Initializer;
|
||||
import de.unidue.nes.gpu.tasks.Task;
|
||||
import de.unidue.nes.gpu.tasks.taskdefinitions.MedianFilterTask;
|
||||
import de.unidue.nes.gpu.tasks.taskdefinitions.ConvertingTask;
|
||||
import de.unidue.nes.gpu.tasks.taskdefinitions.UploadDownloadTask;
|
||||
|
||||
/**
|
||||
* Class containing the main method of this project.
|
||||
@@ -32,12 +33,11 @@ public class GpuTestSystem {
|
||||
System.out.println("Now starting tasks to measure performance");
|
||||
// now run tasks to get the runtime performance
|
||||
tasks.forEach(Task::runTask);
|
||||
|
||||
}
|
||||
|
||||
private List<Task> getTasks() {
|
||||
|
||||
return Arrays.asList(new MedianFilterTask());
|
||||
return Arrays.asList(new ConvertingTask(), new UploadDownloadTask());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package de.unidue.nes.gpu.tasks.taskdefinitions;
|
||||
|
||||
import org.opencv.core.CvType;
|
||||
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.tasks.Task;
|
||||
import de.unidue.nes.gpu.utils.MatConverter;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*
|
||||
*/
|
||||
public class ConvertingTask implements Task {
|
||||
|
||||
@Override
|
||||
public void runTask() {
|
||||
Mat smallMat = Mat.eye(3, 3, CvType.CV_8U);
|
||||
Mat smallImage = Imgcodecs.imread("src/main/resources/car.jpg");
|
||||
Mat bigImage = new Mat();
|
||||
Imgproc.resize(smallImage, bigImage, new Size(), 10, 10);
|
||||
|
||||
MatConverter converter = new MatConverter();
|
||||
|
||||
try (org.bytedeco.opencv.opencv_core.Mat smallJavacvMat = new org.bytedeco.opencv.opencv_core.Mat();
|
||||
org.bytedeco.opencv.opencv_core.Mat smallJavacvImage = new org.bytedeco.opencv.opencv_core.Mat();
|
||||
org.bytedeco.opencv.opencv_core.Mat bigJavacvImage = new org.bytedeco.opencv.opencv_core.Mat()) {
|
||||
long time1 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
converter.convert(smallMat, smallJavacvMat);
|
||||
}
|
||||
long time2 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
converter.convert(smallImage, smallJavacvImage);
|
||||
}
|
||||
long time3 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
converter.convert(bigImage, bigJavacvImage);
|
||||
}
|
||||
long time4 = System.currentTimeMillis();
|
||||
|
||||
System.out.println("converting small mat: " + (time2 - time1));
|
||||
System.out.println("converting small image: " + (time3 - time2));
|
||||
System.out.println("converting big image: " + (time4 - time3));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package de.unidue.nes.gpu.tasks.taskdefinitions;
|
||||
|
||||
import de.unidue.nes.gpu.tasks.Task;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class UploadDownloadTask implements Task {
|
||||
|
||||
@Override
|
||||
public void runTask() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,24 @@
|
||||
package de.unidue.nes.gpu.utils;
|
||||
|
||||
import org.bytedeco.javacv.OpenCVFrameConverter;
|
||||
import org.opencv.core.CvType;
|
||||
|
||||
/**
|
||||
* Class which is converting between {@link org.opencv.core.Mat} and
|
||||
* {@link org.bytedeco.opencv.opencv_core.Mat}.
|
||||
*
|
||||
* @implNote deprecated methods are using the{@link OpenCVFrameConverter}. I'm
|
||||
* not 100% sure whether the {@code OpenCVFrameConverter} is leaking
|
||||
* memory or if it is the most efficient way of converting between
|
||||
* both APIs. Both {@code convert} methods should do exactly the same
|
||||
* without allocating native memory.
|
||||
*
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class MatConverter {
|
||||
|
||||
private static final String ERROR_MSG = "Only byte types are supported";
|
||||
|
||||
private OpenCVFrameConverter.ToOrgOpenCvCoreMat converter1;
|
||||
private OpenCVFrameConverter.ToMat converter2;
|
||||
|
||||
@@ -15,11 +27,46 @@ public class MatConverter {
|
||||
this.converter2 = new OpenCVFrameConverter.ToMat();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public org.bytedeco.opencv.opencv_core.Mat toJavaCVMat(org.opencv.core.Mat mat) {
|
||||
if (mat.depth() != CvType.CV_8U && mat.depth() != CvType.CV_8S) {
|
||||
throw new IllegalArgumentException(ERROR_MSG);
|
||||
}
|
||||
return this.converter2.convert(this.converter1.convert(mat));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public org.opencv.core.Mat toOrgOpenCvMat(org.bytedeco.opencv.opencv_core.Mat mat) {
|
||||
if (mat.depth() != CvType.CV_8U && mat.depth() != CvType.CV_8S) {
|
||||
throw new IllegalArgumentException(ERROR_MSG);
|
||||
}
|
||||
return this.converter1.convert(this.converter2.convert(mat));
|
||||
}
|
||||
|
||||
public void convert(org.opencv.core.Mat src, org.bytedeco.opencv.opencv_core.Mat dst) {
|
||||
if (src.depth() != CvType.CV_8U && src.depth() != CvType.CV_8S) {
|
||||
throw new IllegalArgumentException(ERROR_MSG);
|
||||
}
|
||||
final int cols = src.cols();
|
||||
final int rows = src.rows();
|
||||
final int type = src.type();
|
||||
byte[] buffer = new byte[(int) (src.total() * src.channels())];
|
||||
src.get(0, 0, buffer);
|
||||
dst.create(rows, cols, type);
|
||||
dst.data().put(buffer);
|
||||
}
|
||||
|
||||
public void convert(org.bytedeco.opencv.opencv_core.Mat src, org.opencv.core.Mat dst) {
|
||||
if (src.depth() != CvType.CV_8U && src.depth() != CvType.CV_8S) {
|
||||
throw new IllegalArgumentException(ERROR_MSG);
|
||||
}
|
||||
final int cols = src.cols();
|
||||
final int rows = src.rows();
|
||||
final int type = src.type();
|
||||
byte[] buffer = new byte[(int) (src.total() * src.channels())];
|
||||
src.data().get(buffer);
|
||||
dst.create(rows, cols, type);
|
||||
dst.put(0, 0, buffer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
package de.unidue.nes.gpu.utils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
|
||||
import org.bytedeco.opencv.global.opencv_imgcodecs;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class MatConverterTest {
|
||||
|
||||
static {
|
||||
System.loadLibrary("opencv_java411");
|
||||
}
|
||||
|
||||
private static final String IMAGE_PATH = "src/main/resources/car.jpg";
|
||||
|
||||
private MatConverter converter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.converter = new MatConverter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalOpencv2Javacv() {
|
||||
org.opencv.core.Mat mat = Mat.eye(3, 3, CvType.CV_8U);
|
||||
org.bytedeco.opencv.opencv_core.Mat result = this.converter.toJavaCVMat(mat);
|
||||
compareMats(mat, result);
|
||||
|
||||
mat = Mat.eye(5, 5, CvType.CV_8S);
|
||||
result = this.converter.toJavaCVMat(mat);
|
||||
compareMats(mat, result);
|
||||
|
||||
mat = Imgcodecs.imread(IMAGE_PATH);
|
||||
result = this.converter.toJavaCVMat(mat);
|
||||
compareMats(mat, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalJavacv2Opencv() {
|
||||
org.bytedeco.opencv.opencv_core.Mat mat = org.bytedeco.opencv.opencv_core.Mat.eye(3, 3, CvType.CV_8U).asMat();
|
||||
org.opencv.core.Mat result = this.converter.toOrgOpenCvMat(mat);
|
||||
compareMats(result, mat);
|
||||
|
||||
mat = org.bytedeco.opencv.opencv_core.Mat.eye(5, 5, CvType.CV_8S).asMat();
|
||||
result = this.converter.toOrgOpenCvMat(mat);
|
||||
compareMats(result, mat);
|
||||
|
||||
mat = opencv_imgcodecs.imread(IMAGE_PATH);
|
||||
result = this.converter.toOrgOpenCvMat(mat);
|
||||
compareMats(result, mat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomOpencv2Javacv() {
|
||||
org.opencv.core.Mat mat = Mat.eye(3, 3, CvType.CV_8U);
|
||||
org.bytedeco.opencv.opencv_core.Mat result = new org.bytedeco.opencv.opencv_core.Mat();
|
||||
this.converter.convert(mat, result);
|
||||
compareMats(mat, result);
|
||||
|
||||
mat = Mat.eye(5, 5, CvType.CV_8S);
|
||||
result = new org.bytedeco.opencv.opencv_core.Mat();
|
||||
this.converter.convert(mat, result);
|
||||
compareMats(mat, result);
|
||||
|
||||
mat = Imgcodecs.imread(IMAGE_PATH);
|
||||
result = new org.bytedeco.opencv.opencv_core.Mat();
|
||||
this.converter.convert(mat, result);
|
||||
compareMats(mat, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomJavacv2Opencv() {
|
||||
org.bytedeco.opencv.opencv_core.Mat mat = org.bytedeco.opencv.opencv_core.Mat.eye(3, 3, CvType.CV_8U).asMat();
|
||||
org.opencv.core.Mat result = new Mat();
|
||||
this.converter.convert(mat, result);
|
||||
compareMats(result, mat);
|
||||
|
||||
mat = org.bytedeco.opencv.opencv_core.Mat.eye(5, 5, CvType.CV_8S).asMat();
|
||||
result = new Mat();
|
||||
this.converter.convert(mat, result);
|
||||
compareMats(result, mat);
|
||||
|
||||
mat = opencv_imgcodecs.imread(IMAGE_PATH);
|
||||
result = new Mat();
|
||||
this.converter.convert(mat, result);
|
||||
compareMats(result, mat);
|
||||
}
|
||||
|
||||
private void compareMats(org.opencv.core.Mat mat1, org.bytedeco.opencv.opencv_core.Mat mat2) {
|
||||
assertThat(mat1.cols()).isEqualTo(mat2.cols());
|
||||
assertThat(mat1.rows()).isEqualTo(mat2.rows());
|
||||
assertThat(mat1.type()).isEqualTo(mat2.type());
|
||||
assertThat(mat1.channels()).isEqualTo(mat2.channels());
|
||||
assertThat(mat1.depth()).isEqualTo(mat2.depth());
|
||||
assertThat(mat1.total()).isEqualTo(mat2.total());
|
||||
|
||||
compareData(mat1, mat2);
|
||||
}
|
||||
|
||||
private void compareData(org.opencv.core.Mat mat1, org.bytedeco.opencv.opencv_core.Mat mat2) {
|
||||
if (mat1.depth() == CvType.CV_8U || mat1.depth() == CvType.CV_8S) {
|
||||
byte[] bytes1 = new byte[(int) (mat1.total() * mat1.channels())];
|
||||
mat1.get(0, 0, bytes1);
|
||||
byte[] bytes2 = new byte[(int) (mat2.total() * mat2.channels())];
|
||||
mat2.data().get(bytes2);
|
||||
assertThat(bytes1).containsExactly(bytes2);
|
||||
return;
|
||||
}
|
||||
fail("Currently only mats containing bytes should be tested");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user