The median filter is a non-linear digital filtering technique, often used to remove noise from an image or signal. Such noise reduction is a typical pre-processing step to improve the results of later processing (for example, edge detection on an image). Median filtering is very widely used in digital image processing because, under certain conditions, it preserves edges while removing noise (but see the discussion below), also having applications in signal processing. (1)
- C#
- .NET
- BenchmarkDotNet
- managedCUDA (library for CUDA usage from C#)
- CUDA
Optimalization of median filtering is using Divide-And-Conquer technique.
Device | Description |
---|---|
CPU | We are dividing photo on N parts (N is amount of used threads) |
GPU | We are using shared memory from device to get access to read all colors values and calculate one pixel per thread. |
We are calculating median for each of ARGB value per pixel and deciding which one should be at output image.
Input | Result |
---|---|
Computer data: CPU: AMD Ryzen 5 4800H ( 8 cores, 16 threads) 2.9Ghz to 4.2Ghz RAM: 16GB 2300Mhz GPU: NVidia RTX2060 6GB
Device | Threads | Mean | Error | StdDev |
---|---|---|---|---|
CPU | 1 | 342.28 ms | 6.261 ms | 5.856 ms |
CPU | 4 | 99.40 ms | 1.981 ms | 4.348 ms |
CPU | 8 | 100.43 ms | 1.999 ms | 4.303 ms |
CPU | 16 | 46.23 ms | 0.899 ms | 0.883 ms |
CPU | 32 | 48.67 ms | 0.968 ms | 0.905 ms |
GPU | N/A | 0.833024 ms | N/A | N/A |
Method | Threads | Mean |
---|---|---|
CPU | 1 | 60.653 s |
CPU | 4 | 17.720 s |
CPU | 8 | 16.438 s |
CPU | 16 | 7.858 s |
CPU | 32 | 8.161 s |
GPU | N/A | 0.08512009 s |
Method | Threads | Mean |
---|---|---|
CPU | 1 | 139.89 s |
CPU | 4 | 40.80 s |
CPU | 8 | 43.31 s |
CPU | 16 | 19.19 s |
CPU | 32 | 19.40 s |
GPU | N/A | 0.1454972 s |
As we can see switching calculations to GPU gives us a lot of improvement.
using(var image = Image(path)
{
using(var filter = BasicMedianFilter(image))
{
//CPU
filter.CalculateOnCPU(numberOfThreads);
//GPU
filter.PrepareForGPU(); // this would allocate memory on our GPU and transfer the data
var timeOfExcecution = filter.CalculateOnGpu();
calculator.FinalizeGpu(); //transfer the data from device to host
}
}
-
Free High-Resolution Satellite Images Samples | EffigisI've decided not to test on 1 thread because it's taking too much time to complete the task.