ImageDev

Hello ImageDev

This first example, also presented in the Getting Started section of the Reference Guide, briefly introduces how to open an image from a file and apply two consecutive image processing algorithms on it.
The initial image is a grayscale image that appears almost totally gray when opened with a basic image viewer.

<b>Figure 1.</b> Default display of the initial image
Figure 1. Default display of the initial image

The reason of this aspect is the poor contrast of the image. Almost all pixel intensities are 128 or 129 which are basically displayed in a same neutral gray. A few sparse pixels have the value 0 (black) or 255 (white).

The first step of this example applies the RescaleIntensity algorithm in Percentile mode. This algorithm consists of expanding the image dynamics from two input values to the output range [0, 255]. Then, when applying the normalization, all pixels having an intensity lower than or equal to 128 are set to 0 and pixels having an intensity greater than or equal to 129 are set to 255.

You can notice that the syntax to invoke this first algorithm is the function mode. The algorithm is executed in a single instruction.

<b>Figure 2.</b> Result of the image normalization
Figure 2. Result of the image normalization

The magic happens and we can now read the text. The image still contains some salt noise which corresponds to the 255 values from the initial image.

To remove the white spots, the example applies a median filter which is an efficient filter for removing impulse noise.

The syntax used to apply this second algorithm is the class mode. The syntax change here serves the purpose of demonstrating the options of both class and function programming using ImageDev. This step also could be launched with a single instruction like in the previous step.

<b>Figure 3.</b> Final image after median filter denoising
Figure 3. Final image after median filter denoising

Notes:
#include <ImageDev/ImageDev.h>
#include <ioformat/IOFormat.h>
#include <iolink/view/ImageViewProvider.h>
#include <string.h>

using namespace imagedev;
using namespace ioformat;
using namespace iolink;

int
main( int argc, char* argv[] )
{
    int status = 0;

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

        // Open a tif file
        auto imageInput = readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "hello_imagedev.tif" );

        // Apply a normalization of its graylevels using function coding style
        auto imageNorm = rescaleIntensity( imageInput,
                                           RescaleIntensity::OutputType::SAME_AS_INPUT,
                                           RescaleIntensity::RangeMode::PERCENTILE,
                                           { 2, 98 },
                                           { 0, 255 },
                                           { 0, 255 } );

        // Save the normalized image as a png file
        writeView( imageNorm, "T01_01_norm.png" );

        // Apply a median filter processing using class coding style
        MedianFilter2d processMedian;
        processMedian.setInputImage( imageNorm );
        processMedian.setKernelMode( MedianFilter2d::KernelMode::DISK );
        processMedian.setKernelRadius( 1 );
        processMedian.execute();
        auto imageMedian = processMedian.outputImage();

        // Save the resulting image as a png file
        writeView( processMedian.outputImage(), "T01_01_output.png" );

        std::cout << "This example ran successfully." << std::endl;
    }
    catch ( const imagedev::Exception& error )
    {
        // Print potential exception in the standard output
        std::cerr << "ImageDev exception: " << error.what() << std::endl;
        status = -1;
    }

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

    // Check if we must ask for an enter key to close the program
    if ( !( ( argc == 2 ) && strcmp( argv[1], "--no-stop-at-end" ) == 0 ) )
        std::cout << "Press Enter key to close this window." << std::endl, getchar();

    return status;
}


See also