added example test for median filtering
This commit is contained in:
@@ -3,16 +3,9 @@ package de.unidue.nes.gpu;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import de.unidue.nes.gpu.initialization.Initializer;
|
||||
import de.unidue.nes.gpu.tasks.Task;
|
||||
import de.unidue.nes.gpu.tasks.cputasks.CPUMedianFilterImpl;
|
||||
import de.unidue.nes.gpu.tasks.gputasks.GPUMedianFilterImpl;
|
||||
import de.unidue.nes.gpu.tasks.taskdefinitions.MedianFilterTask;
|
||||
|
||||
/**
|
||||
* Class containing the main method of this project.
|
||||
@@ -26,25 +19,18 @@ public class GpuTestSystem {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Starting the system");
|
||||
// testInitialization();
|
||||
System.out.println("Starting the tests");
|
||||
new GpuTestSystem().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Mat img = Imgcodecs.imread("src/main/resources/car.jpg");
|
||||
Imgproc.resize(img, img, new Size(), 2.5, 2.5);
|
||||
HighGui.imshow("The image", img);
|
||||
|
||||
List<Task> tasks = getTasks();
|
||||
tasks.forEach(t -> t.runTask(img));
|
||||
|
||||
HighGui.waitKey();
|
||||
tasks.forEach(Task::runTask);
|
||||
}
|
||||
|
||||
private List<Task> getTasks() {
|
||||
|
||||
return Arrays.asList(new CPUMedianFilterImpl(), new GPUMedianFilterImpl());
|
||||
return Arrays.asList(new MedianFilterTask());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package de.unidue.nes.gpu.initialization;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class InitializationException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1880968305764734752L;
|
||||
|
||||
public InitializationException(String string) {
|
||||
super(string);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.unidue.nes.gpu.initialization;
|
||||
|
||||
import org.bytedeco.opencv.global.opencv_core;
|
||||
import org.bytedeco.opencv.opencv_core.GpuMat;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Size;
|
||||
@@ -12,24 +13,31 @@ import org.opencv.core.Size;
|
||||
*/
|
||||
public class Initializer {
|
||||
|
||||
private Initializer() {
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
System.out.println("Loading opencv dll");
|
||||
System.loadLibrary("opencv_java411");
|
||||
|
||||
System.out.println("Loading CUDA module");
|
||||
int count = opencv_core.getCudaEnabledDeviceCount();
|
||||
System.out.println("Number of Cuda enabled devices: " + count);
|
||||
if (count <= 0) {
|
||||
throw new InitializationException("no cuda enabled device was found");
|
||||
}
|
||||
try (GpuMat gpuMat = new GpuMat()) {
|
||||
System.out.println("address of some random GpuMat: " + gpuMat.address());
|
||||
}
|
||||
testInitialization();
|
||||
}
|
||||
|
||||
private static void testInitialization() {
|
||||
Mat m = Mat.eye(new Size(3, 3), CvType.CV_8U);
|
||||
if (m.empty()) {
|
||||
throw new RuntimeException("OpenCV was not initialized");
|
||||
throw new InitializationException("OpenCV was not initialized");
|
||||
}
|
||||
|
||||
int count = opencv_core.getCudaEnabledDeviceCount();
|
||||
System.out.println("Number of Cuda enabled devices: " + count);
|
||||
if (count <= 0) {
|
||||
throw new RuntimeException("no cuda enabled device was found");
|
||||
}
|
||||
opencv_core.printCudaDeviceInfo(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package de.unidue.nes.gpu.tasks;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
/**
|
||||
* the top-level definition of a task which should be tested
|
||||
*
|
||||
@@ -9,5 +7,5 @@ import org.opencv.core.Mat;
|
||||
*/
|
||||
public interface Task {
|
||||
|
||||
void runTask(Mat mat);
|
||||
void runTask();
|
||||
}
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
package de.unidue.nes.gpu.tasks.cputasks;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import de.unidue.nes.gpu.tasks.taskdefinitions.MedianFilterTask;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class CPUMedianFilterImpl implements MedianFilterTask {
|
||||
public class CPUMedianFilterImpl {
|
||||
|
||||
@Override
|
||||
public void runTask(Mat mat) {
|
||||
System.out.println("doing some cpu median filtering");
|
||||
public void applyMedianFilter(Mat mat) {
|
||||
Imgproc.medianBlur(mat, mat, 15);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,17 +1,31 @@
|
||||
package de.unidue.nes.gpu.tasks.gputasks;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.bytedeco.opencv.global.opencv_cudafilters;
|
||||
import org.bytedeco.opencv.opencv_core.GpuMat;
|
||||
import org.bytedeco.opencv.opencv_cudafilters.Filter;
|
||||
|
||||
import de.unidue.nes.gpu.tasks.taskdefinitions.MedianFilterTask;
|
||||
import de.unidue.nes.gpu.utils.MatConverter;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class GPUMedianFilterImpl implements MedianFilterTask {
|
||||
public class GPUMedianFilterImpl {
|
||||
|
||||
@Override
|
||||
public void runTask(Mat mat) {
|
||||
System.out.println("doing some gpu median filtering");
|
||||
private MatConverter converter;
|
||||
|
||||
public GPUMedianFilterImpl() {
|
||||
this.converter = new MatConverter();
|
||||
}
|
||||
|
||||
public void applyMedianFilter(org.opencv.core.Mat mat) {
|
||||
GpuMat gpuMat = new GpuMat();
|
||||
org.bytedeco.opencv.opencv_core.Mat javacvMat = this.converter.toJavaCVMat(mat);
|
||||
gpuMat.upload(javacvMat);
|
||||
Filter filter = opencv_cudafilters.createMedianFilter(gpuMat.type(), 15);
|
||||
filter.apply(gpuMat, gpuMat);
|
||||
gpuMat.download(javacvMat);
|
||||
org.opencv.core.Mat result = this.converter.toOrgOpenCvMat(javacvMat);
|
||||
result.copyTo(mat);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+42
-1
@@ -1,12 +1,53 @@
|
||||
package de.unidue.nes.gpu.tasks.taskdefinitions;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import de.unidue.nes.gpu.tasks.Task;
|
||||
import de.unidue.nes.gpu.tasks.cputasks.CPUMedianFilterImpl;
|
||||
import de.unidue.nes.gpu.tasks.gputasks.GPUMedianFilterImpl;
|
||||
|
||||
/**
|
||||
* A task testing median filter behavior
|
||||
*
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public interface MedianFilterTask extends Task {
|
||||
public class MedianFilterTask implements Task {
|
||||
|
||||
private CPUMedianFilterImpl cpuImpl;
|
||||
private GPUMedianFilterImpl gpuImpl;
|
||||
|
||||
public MedianFilterTask() {
|
||||
this.cpuImpl = new CPUMedianFilterImpl();
|
||||
this.gpuImpl = new GPUMedianFilterImpl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runTask() {
|
||||
Mat img = Imgcodecs.imread("src/main/resources/car.jpg");
|
||||
Imgproc.resize(img, img, new Size(), 2.5, 2.5);
|
||||
Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2GRAY);
|
||||
HighGui.imshow("image for median filtering", img);
|
||||
Mat cpuImg = new Mat();
|
||||
Mat gpuImg = new Mat();
|
||||
img.copyTo(cpuImg);
|
||||
img.copyTo(gpuImg);
|
||||
|
||||
long time1 = System.currentTimeMillis();
|
||||
this.cpuImpl.applyMedianFilter(cpuImg);
|
||||
long time2 = System.currentTimeMillis();
|
||||
this.gpuImpl.applyMedianFilter(gpuImg);
|
||||
long time3 = System.currentTimeMillis();
|
||||
|
||||
System.out.println("CPU processing time: " + (time2 - time1) + "ms");
|
||||
System.out.println("GPU processing time: " + (time3 - time2) + "ms");
|
||||
|
||||
HighGui.imshow("CPU result", cpuImg);
|
||||
HighGui.imshow("GPU result", gpuImg);
|
||||
HighGui.waitKey();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package de.unidue.nes.gpu.utils;
|
||||
|
||||
import org.bytedeco.javacv.OpenCVFrameConverter;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class MatConverter {
|
||||
|
||||
private OpenCVFrameConverter.ToOrgOpenCvCoreMat converter1;
|
||||
private OpenCVFrameConverter.ToMat converter2;
|
||||
|
||||
public MatConverter() {
|
||||
this.converter1 = new OpenCVFrameConverter.ToOrgOpenCvCoreMat();
|
||||
this.converter2 = new OpenCVFrameConverter.ToMat();
|
||||
}
|
||||
|
||||
public org.bytedeco.opencv.opencv_core.Mat toJavaCVMat(org.opencv.core.Mat mat) {
|
||||
return this.converter2.convert(this.converter1.convert(mat));
|
||||
}
|
||||
|
||||
public org.opencv.core.Mat toOrgOpenCvMat(org.bytedeco.opencv.opencv_core.Mat mat) {
|
||||
return this.converter1.convert(this.converter2.convert(mat));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user