ImageDev

Create 2D Image

This example illustrates how to create a data buffer corresponding to a 2D image, and copy it in an object connectable to an ImageDev algorithm.
To invoke an ImageDev algorithm, the data to process needs to be previously imported into an ImageView object of the IOLink library.

The first part of this example simply creates a buffer representing a 768x1027 pixel image with a filled square drawn inside. This step is for demonstration purposes only. In practice, you should already have the content of the image to process in a buffer stored in the data model used by your application.

Then some IOLink instructions create a new ImageView object. The buffer previously created is copied in this image with the WriteRegion method.

Two ImageDev algorithms are applied afterward to show that it is now possible to process this image with ImageDev.
<b>Figure 1.</b> The 2D image generated by this example
Figure 1. The 2D image generated by this example

#include <ImageDev/Data/ImageViewHelper.h>
#include <ImageDev/ImageDev.h>
#include <ioformat/IOFormat.h>
#include <iolink/view/ImageViewFactory.h>
#include <iolink/view/ImageViewProvider.h>

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

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

    // Initialize an unsigned 8-bit array storing data of a 2D image
    const uint64_t rowCount = 768;
    const uint64_t colCount = 1024;
    std::vector< uint8_t > imageData( rowCount * colCount );

    // Define a synthetic square in this array
    const uint64_t side = colCount / 2; // side in pixels of the square to draw

    // Loop on image rows
    for ( uint64_t i = 0; i < rowCount; ++i )
    {
        // Loop on image columns
        for ( uint64_t j = 0; j < colCount; ++j )
        {
            if ( ( i >= ( rowCount - side ) / 2 ) && ( i <= ( rowCount + side ) / 2 ) &&
                 ( j >= ( colCount - side ) / 2 ) && ( j <= ( colCount + side ) / 2 ) )
                imageData[i * colCount + j] = 228; // Value inside the square
            else
                imageData[i * colCount + j] = 0; // Background value
        }
    }

    // Create an image view of same dimensions
    VectorXu64 imageShape{ colCount, rowCount };
    auto image = ImageViewFactory::allocate( imageShape, DataTypeId::UINT8 );
    setDimensionalInterpretation( image, ImageTypeId::IMAGE );

    // Define the region where to write the data
    VectorXu64 imageOrig{ 0, 0 };
    RegionXu64 imageRegion( imageOrig, imageShape );

    // Copy the data in the image view
    image->writeRegion( imageRegion, imageData.data() );

    // This image can now be processed by any ImageDev algorithm, for instance for building a color image
    auto imageVoid = resetImage( image, 0.0f );
    auto imageRgb = grayscaleToColor( image, image, imageVoid );

    // Save the created image with IOFormat
    writeView( imageRgb, R"(T02_01_output.png)" );

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

    return 0;
}


See also