ImageDev

Denoising

This example shows how to denoise a grayscale image with three different algorithms available in ImageDev.
The first denoising algorithm applied in this example is BoxFilter2d. This linear filter computes a moving average on a square window with a user-defined size. It is fast to apply but softens the objects edges and adds blur to the input image.

The second denoising algorithm is MedianFilter2d. This non-linear filter replaces each pixel by the median value of its neighborhood. It is slower to compute than a box filter but preserves the edges better. It is especially efficient for removing impulse noise.

At last, a NonLocalMeansFilter2d algorithm is applied. This adaptive filter performs a weighted averaging of each pixel with similar pixels of its neighborhood. It is very slow to compute but preserves edges very efficiently.

<b>(a)</b>
(a)
<b>(b)</b>
(b)
<b>(c)</b>
(c)
<b>(d)</b>
(d)
Figure 1. Denoising filters (a) the initial image, (b) the box filter result,
(c) the median filter result, and (d) the non-local means filter result

Each filter result is saved in the project directory. As expected, the box filter generates blur, the median filter preserves edges better, and the non-local means filter preserves also the internal structure of nuclei.

#include <ImageDev/ImageDev.h>
#include <ioformat/IOFormat.h>

using namespace imagedev;

int
main()
{
    // ImageDev library initialization
    if ( imagedev::isInitialized() == false )
        imagedev::init();

    // Open a tif file to denoise
    auto imageInput = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "mnu.tif" );

    // Apply an average filter
    std::cout << "Applying a box filter... " << std::endl;
    auto imageOutput = boxFilter2d( imageInput, 7, 7, BoxFilter2d::AutoScale::YES );
    ioformat::writeView( imageOutput, R"(T03_01_box.png)" );

    // Apply a median filter processing
    std::cout << "Applying a median filter... " << std::endl;
    imageOutput =
        medianFilter2d( imageInput, 3, MedianFilter2d::KernelMode::SQUARE, MedianFilter2d::SearchMode::AUTOMATIC );
    ioformat::writeView( imageOutput, R"(T03_01_median.png)" );

    // Apply a non-local means filter processing
    std::cout << "Applying a non-local means filter... " << std::endl;
    imageOutput = nonLocalMeansFilter2d( imageInput, 3, 3, 0.6, NonLocalMeansFilter2d::SQUARE );
    ioformat::writeView( imageOutput, R"(T03_01_nlm.png)" );

    // ImageDev library finalization
    imagedev::finish();

    return 0;
}


See also