added logic to upload/download task and extended MatConverter
This commit is contained in:
@@ -41,9 +41,9 @@ public class ConvertingTask implements Task {
|
||||
}
|
||||
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));
|
||||
System.out.println("converting small mat: " + (time2 - time1) + "ms");
|
||||
System.out.println("converting small image: " + (time3 - time2) + "ms");
|
||||
System.out.println("converting big image: " + (time4 - time3) + "ms");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
package de.unidue.nes.gpu.tasks.taskdefinitions;
|
||||
|
||||
import org.bytedeco.opencv.global.opencv_imgcodecs;
|
||||
import org.bytedeco.opencv.global.opencv_imgproc;
|
||||
import org.bytedeco.opencv.opencv_core.GpuMat;
|
||||
import org.bytedeco.opencv.opencv_core.Size;
|
||||
import org.opencv.core.CvType;
|
||||
|
||||
import de.unidue.nes.gpu.tasks.Task;
|
||||
|
||||
/**
|
||||
@@ -10,6 +16,36 @@ public class UploadDownloadTask implements Task {
|
||||
@Override
|
||||
public void runTask() {
|
||||
|
||||
org.bytedeco.opencv.opencv_core.Mat smallMat = org.bytedeco.opencv.opencv_core.Mat.eye(3, 3, CvType.CV_8U).asMat();
|
||||
|
||||
org.bytedeco.opencv.opencv_core.Mat smallImage = opencv_imgcodecs.imread("src/main/resources/car.jpg");
|
||||
|
||||
org.bytedeco.opencv.opencv_core.Mat bigImage = new org.bytedeco.opencv.opencv_core.Mat();
|
||||
opencv_imgproc.resize(smallImage, bigImage, new Size(smallImage.cols() * 10, smallImage.rows() * 10));
|
||||
try (GpuMat smallGpuMat = new GpuMat(); GpuMat smallGpuImage = new GpuMat(); GpuMat bigGpuImage = new GpuMat()) {
|
||||
|
||||
long time1 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
smallGpuMat.upload(smallMat);
|
||||
smallGpuMat.download(smallMat);
|
||||
}
|
||||
long time2 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
smallGpuImage.upload(smallImage);
|
||||
smallGpuImage.download(smallImage);
|
||||
}
|
||||
long time3 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
bigGpuImage.upload(bigImage);
|
||||
bigGpuImage.download(bigImage);
|
||||
}
|
||||
long time4 = System.currentTimeMillis();
|
||||
|
||||
System.out.println("Small mat upload + download: " + (time2 - time1) + "ms");
|
||||
System.out.println("Small image upload + download: " + (time3 - time2) + "ms");
|
||||
System.out.println("Big image upload + download: " + (time4 - time3) + "ms");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package de.unidue.nes.gpu.utils;
|
||||
|
||||
import org.bytedeco.javacpp.Pointer;
|
||||
import org.bytedeco.javacv.OpenCVFrameConverter;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
/**
|
||||
* Class which is converting between {@link org.opencv.core.Mat} and
|
||||
@@ -27,6 +29,12 @@ public class MatConverter {
|
||||
this.converter2 = new OpenCVFrameConverter.ToMat();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param mat
|
||||
* @return
|
||||
* @deprecated
|
||||
*/
|
||||
@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) {
|
||||
@@ -35,6 +43,12 @@ public class MatConverter {
|
||||
return this.converter2.convert(this.converter1.convert(mat));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param mat
|
||||
* @return
|
||||
* @deprecated
|
||||
*/
|
||||
@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) {
|
||||
@@ -69,4 +83,33 @@ public class MatConverter {
|
||||
dst.put(0, 0, buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param src
|
||||
* @return
|
||||
* @deprecated uses double-brace-initialization, which should be avoided. See
|
||||
* also:
|
||||
* https://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization
|
||||
*/
|
||||
@Deprecated
|
||||
@SuppressWarnings("squid:S3599")
|
||||
public org.bytedeco.opencv.opencv_core.Mat convert2Javacv(org.opencv.core.Mat src) {
|
||||
|
||||
return new org.bytedeco.opencv.opencv_core.Mat((Pointer) null) {
|
||||
{// NOSONAR
|
||||
this.address = src.getNativeObjAddr();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param src
|
||||
* @return
|
||||
* @deprecated
|
||||
*/
|
||||
@Deprecated
|
||||
public org.opencv.core.Mat convert2Opencv(org.bytedeco.opencv.opencv_core.Mat src) {
|
||||
return new Mat(src.address());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,6 +94,36 @@ public class MatConverterTest {
|
||||
compareMats(result, mat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdressMagicOpencv2Javacv() {
|
||||
org.opencv.core.Mat mat = Mat.eye(3, 3, CvType.CV_8U);
|
||||
org.bytedeco.opencv.opencv_core.Mat result = this.converter.convert2Javacv(mat);
|
||||
compareMats(mat, result);
|
||||
|
||||
mat = Mat.eye(5, 5, CvType.CV_8S);
|
||||
result = this.converter.convert2Javacv(mat);
|
||||
compareMats(mat, result);
|
||||
|
||||
mat = Imgcodecs.imread(IMAGE_PATH);
|
||||
result = this.converter.convert2Javacv(mat);
|
||||
compareMats(mat, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdressMagicJavacv2Opencv() {
|
||||
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.convert2Opencv(mat);
|
||||
compareMats(result, mat);
|
||||
|
||||
mat = org.bytedeco.opencv.opencv_core.Mat.eye(5, 5, CvType.CV_8S).asMat();
|
||||
result = this.converter.convert2Opencv(mat);
|
||||
compareMats(result, mat);
|
||||
|
||||
mat = opencv_imgcodecs.imread(IMAGE_PATH);
|
||||
result = this.converter.convert2Opencv(mat);
|
||||
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());
|
||||
|
||||
Reference in New Issue
Block a user