added median and gauss filtering
This commit is contained in:
@@ -11,7 +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.FeatureMatchingTask;
|
||||
import de.unidue.nes.gpu.tasks.taskdefinitions.FilteringTask;
|
||||
|
||||
/**
|
||||
* Class containing the main method of this project.
|
||||
@@ -51,7 +51,7 @@ public class GpuTestSystem {
|
||||
|
||||
private List<Task> getTasks() {
|
||||
|
||||
return Arrays.asList(new FeatureMatchingTask());
|
||||
return Arrays.asList(new FilteringTask());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ public class GPUFeatureMatching {
|
||||
Imgproc.cvtColor(_originalImage, _originalImage, Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.cvtColor(_wrongImage, _wrongImage, Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.cvtColor(_correctImage, _correctImage, Imgproc.COLOR_BGR2GRAY);
|
||||
// Watch out:https://github.com/opencv/opencv/issues/10573
|
||||
Imgproc.resize(_originalImage, _originalImage, new Size(), 15, 15);
|
||||
Imgproc.resize(_wrongImage, _wrongImage, new Size(), 15, 15);
|
||||
Imgproc.resize(_correctImage, _correctImage, new Size(), 15, 15);
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package de.unidue.nes.gpu.tasks.taskdefinitions;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class CPUFiltering {
|
||||
|
||||
private static final Logger LOG = LogManager.getLogger(CPUFiltering.class);
|
||||
|
||||
private static final String IMG_PATH = "src/main/resources/car.jpg";
|
||||
|
||||
private Mat[] testImages;
|
||||
|
||||
public CPUFiltering() {
|
||||
this.testImages = new Mat[] { Imgcodecs.imread(IMG_PATH), Imgcodecs.imread(IMG_PATH), Imgcodecs.imread(IMG_PATH), Imgcodecs.imread(IMG_PATH) };
|
||||
Imgproc.resize(this.testImages[1], this.testImages[1], new Size(), 2.5, 2.5);
|
||||
Imgproc.resize(this.testImages[2], this.testImages[2], new Size(), 5, 5);
|
||||
Imgproc.resize(this.testImages[3], this.testImages[3], new Size(), 10, 10);
|
||||
Imgproc.cvtColor(this.testImages[0], this.testImages[0], Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.cvtColor(this.testImages[1], this.testImages[1], Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.cvtColor(this.testImages[2], this.testImages[2], Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.cvtColor(this.testImages[3], this.testImages[3], Imgproc.COLOR_BGR2GRAY);
|
||||
|
||||
}
|
||||
|
||||
public void runMedianFiltering() {
|
||||
Mat dst = new Mat();
|
||||
|
||||
long time1 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Imgproc.medianBlur(this.testImages[0], dst, 15);
|
||||
}
|
||||
long time2 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Imgproc.medianBlur(this.testImages[1], dst, 15);
|
||||
}
|
||||
long time3 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Imgproc.medianBlur(this.testImages[2], dst, 15);
|
||||
}
|
||||
long time4 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Imgproc.medianBlur(this.testImages[3], dst, 15);
|
||||
}
|
||||
long time5 = System.currentTimeMillis();
|
||||
|
||||
LOG.info(() -> "original image median filter: " + (time2 - time1) + "ms");
|
||||
LOG.info(() -> "2.5x image median filter: " + (time3 - time2) + "ms");
|
||||
LOG.info(() -> "5x image median filter: " + (time4 - time3) + "ms");
|
||||
LOG.info(() -> "10x image median filter: " + (time5 - time4) + "ms");
|
||||
|
||||
}
|
||||
|
||||
public void runGaussianBlur() {
|
||||
Mat dst = new Mat();
|
||||
Size size = new Size(15, 15);
|
||||
|
||||
long time1 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Imgproc.GaussianBlur(this.testImages[0], dst, size, 0, 0);
|
||||
}
|
||||
long time2 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Imgproc.GaussianBlur(this.testImages[1], dst, size, 0, 0);
|
||||
}
|
||||
long time3 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Imgproc.GaussianBlur(this.testImages[2], dst, size, 0, 0);
|
||||
}
|
||||
long time4 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Imgproc.GaussianBlur(this.testImages[3], dst, size, 0, 0);
|
||||
}
|
||||
long time5 = System.currentTimeMillis();
|
||||
|
||||
LOG.info(() -> "original image gaussian filter: " + (time2 - time1) + "ms");
|
||||
LOG.info(() -> "2.5x image gaussian filter: " + (time3 - time2) + "ms");
|
||||
LOG.info(() -> "5x image gaussian filter: " + (time4 - time3) + "ms");
|
||||
LOG.info(() -> "10x image gaussian filter: " + (time5 - time4) + "ms");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,9 +7,21 @@ import de.unidue.nes.gpu.tasks.Task;
|
||||
*/
|
||||
public class FilteringTask implements Task {
|
||||
|
||||
private CPUFiltering cpuTask;
|
||||
|
||||
private GPUFiltering gpuTask;
|
||||
|
||||
public FilteringTask() {
|
||||
this.cpuTask = new CPUFiltering();
|
||||
this.gpuTask = new GPUFiltering();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runTask() {
|
||||
|
||||
this.cpuTask.runMedianFiltering();
|
||||
this.cpuTask.runGaussianBlur();
|
||||
this.gpuTask.runMedianFiltering();
|
||||
this.gpuTask.runGaussianBlur();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
package de.unidue.nes.gpu.tasks.taskdefinitions;
|
||||
|
||||
import static org.bytedeco.opencv.global.opencv_core.CV_8UC1;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bytedeco.opencv.global.opencv_cudafilters;
|
||||
import org.bytedeco.opencv.opencv_core.GpuMat;
|
||||
import org.bytedeco.opencv.opencv_core.Mat;
|
||||
import org.bytedeco.opencv.opencv_cudafilters.Filter;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import de.unidue.nes.gpu.utils.MatConverter;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class GPUFiltering {
|
||||
|
||||
private static final Logger LOG = LogManager.getLogger(GPUFiltering.class);
|
||||
|
||||
private static final String IMG_PATH = "src/main/resources/car.jpg";
|
||||
|
||||
private org.opencv.core.Mat[] testImages;
|
||||
|
||||
private MatConverter converter;
|
||||
|
||||
public GPUFiltering() {
|
||||
this.testImages = new org.opencv.core.Mat[] { Imgcodecs.imread(IMG_PATH), Imgcodecs.imread(IMG_PATH), Imgcodecs.imread(IMG_PATH),
|
||||
Imgcodecs.imread(IMG_PATH) };
|
||||
Imgproc.resize(this.testImages[1], this.testImages[1], new Size(), 2.5, 2.5);
|
||||
Imgproc.resize(this.testImages[2], this.testImages[2], new Size(), 5, 5);
|
||||
Imgproc.resize(this.testImages[3], this.testImages[3], new Size(), 10, 10);
|
||||
Imgproc.cvtColor(this.testImages[0], this.testImages[0], Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.cvtColor(this.testImages[1], this.testImages[1], Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.cvtColor(this.testImages[2], this.testImages[2], Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.cvtColor(this.testImages[3], this.testImages[3], Imgproc.COLOR_BGR2GRAY);
|
||||
this.converter = new MatConverter();
|
||||
}
|
||||
|
||||
public void runMedianFiltering() {
|
||||
GpuMat img1 = new GpuMat();
|
||||
Mat mat1 = this.converter.convert2Javacv(this.testImages[0]);
|
||||
img1.upload(mat1);
|
||||
GpuMat img2 = new GpuMat();
|
||||
Mat mat2 = this.converter.convert2Javacv(this.testImages[1]);
|
||||
img2.upload(mat2);
|
||||
GpuMat img3 = new GpuMat();
|
||||
Mat mat3 = this.converter.convert2Javacv(this.testImages[2]);
|
||||
img3.upload(mat3);
|
||||
GpuMat img4 = new GpuMat();
|
||||
Mat mat4 = this.converter.convert2Javacv(this.testImages[3]);
|
||||
img4.upload(mat4);
|
||||
GpuMat dst = new GpuMat();
|
||||
Filter filter = opencv_cudafilters.createMedianFilter(CV_8UC1, 15);
|
||||
long time1 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
filter.apply(img1, dst);
|
||||
}
|
||||
long time2 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
filter.apply(img2, dst);
|
||||
}
|
||||
long time3 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
filter.apply(img3, dst);
|
||||
}
|
||||
long time4 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
filter.apply(img4, dst);
|
||||
}
|
||||
long time5 = System.currentTimeMillis();
|
||||
|
||||
LOG.info(() -> "original image median filter: " + (time2 - time1) + "ms");
|
||||
LOG.info(() -> "2.5x image median filter: " + (time3 - time2) + "ms");
|
||||
LOG.info(() -> "5x image median filter: " + (time4 - time3) + "ms");
|
||||
LOG.info(() -> "10x image median filter: " + (time5 - time4) + "ms");
|
||||
}
|
||||
|
||||
public void runGaussianBlur() {
|
||||
GpuMat img1 = new GpuMat();
|
||||
Mat mat1 = this.converter.convert2Javacv(this.testImages[0]);
|
||||
img1.upload(mat1);
|
||||
GpuMat img2 = new GpuMat();
|
||||
Mat mat2 = this.converter.convert2Javacv(this.testImages[1]);
|
||||
img2.upload(mat2);
|
||||
GpuMat img3 = new GpuMat();
|
||||
Mat mat3 = this.converter.convert2Javacv(this.testImages[2]);
|
||||
img3.upload(mat3);
|
||||
GpuMat img4 = new GpuMat();
|
||||
Mat mat4 = this.converter.convert2Javacv(this.testImages[3]);
|
||||
img4.upload(mat4);
|
||||
GpuMat dst = new GpuMat();
|
||||
Filter filter = opencv_cudafilters.createGaussianFilter(CV_8UC1, CV_8UC1, new org.bytedeco.opencv.opencv_core.Size(15, 15), 0);
|
||||
|
||||
long time1 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
filter.apply(img1, dst);
|
||||
}
|
||||
long time2 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
filter.apply(img2, dst);
|
||||
}
|
||||
long time3 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
filter.apply(img3, dst);
|
||||
}
|
||||
long time4 = System.currentTimeMillis();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
filter.apply(img4, dst);
|
||||
}
|
||||
long time5 = System.currentTimeMillis();
|
||||
|
||||
LOG.info(() -> "original image gaussian filter: " + (time2 - time1) + "ms");
|
||||
LOG.info(() -> "2.5x image gaussian filter: " + (time3 - time2) + "ms");
|
||||
LOG.info(() -> "5x image gaussian filter: " + (time4 - time3) + "ms");
|
||||
LOG.info(() -> "10x image gaussian filter: " + (time5 - time4) + "ms");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -88,18 +88,16 @@ public class MatConverter {
|
||||
*
|
||||
* @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
|
||||
* @implNote uses double-brace-initialization, which should be avoided. See
|
||||
* also:
|
||||
* https://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization
|
||||
*/
|
||||
// TODO: use this, but override release and deallocation methods to prevent
|
||||
// dangling pointers
|
||||
@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();
|
||||
}
|
||||
};
|
||||
@@ -109,9 +107,7 @@ public class MatConverter {
|
||||
*
|
||||
* @param src
|
||||
* @return
|
||||
* @deprecated
|
||||
*/
|
||||
@Deprecated
|
||||
public org.opencv.core.Mat convert2Opencv(org.bytedeco.opencv.opencv_core.Mat src) {
|
||||
return new Mat(src.address());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user