ImageDev

Label Analysis

This example shows how to extract individual measurements for each object of a segmented image.
First, a binary image is opened from a vip file. The vip file format is an internal format dedicated to store images with all relevant information for ImageDev. This image is immediately identified as a binary image and can be labeled with the Label2d algorithm.

Then, an AnalysisMsr object is initialized. It is set up to contain two measurements: EquivalentDiameter2d and FeretRatio2d. At last, the analysis is performed and both measurements are computed for each object of the label image. The results are printed in the standard output.

Performing the label analysis...
Number of objects = 58
Label EquivalentDiameter FeretRatio2d
157.831.04
249.031.00
350.491.02
460.691.02

#include <ImageDev/ImageDev.h>
#include <ioformat/IOFormat.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 ( isInitialized() == false )
            imagedev::init();

        // Open a binary image from a vip file
        auto imageInput = readVipImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene_sep.vip" );

        // Connected component labeling of the binary image
        std::cout << "Labeling the binary input..." << std::endl;
        auto imageLab = labeling2d( imageInput, Labeling2d::LABEL_8_BIT, Labeling2d::CONNECTIVITY_8 );

        // Define the analysis features to be computed
        std::cout << "Performing the label analysis..." << std::endl;
        AnalysisMsr::Ptr analysis = std::make_shared< AnalysisMsr >();
        auto diameter = analysis->select( NativeMeasurements::equivalentDiameter );
        auto feretRatio = analysis->select( NativeMeasurements::feretRatio2d );

        // Launch the feature extraction on the segmented image
        labelAnalysis( imageLab, imageInput, analysis );
        size_t labelCount = analysis->labelCount();
        std::cout << "  - Number of objects = " << labelCount << std::endl;
        // Print the 4 first results of the analysis
        std::cout << "Label\t" << diameter->name() << "\t" << feretRatio->name() << std::endl;
        for ( int i = 0; i < 4; i++ )
        {
            std::cout << ( i + 1 ) << "\t" << diameter->value( i ) << "\t\t\t" << feretRatio->value( i ) << std::endl;
        }

        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;
}
using System;
using ImageDev;
using IOLink;
using IOFormat;

namespace T04_02_LabelAnalysis
{
    class Program
    {
        static void Main(string[] args)
        {
            int status = 0;

            try
            {
                // ImageDev library initialization
                if (Initialization.IsInitialized() == false)
                    Initialization.Init();

                // Open a binary image from a vip file
                ImageView imageInput = Data.ReadVipImage("Data/images/polystyrene_sep.vip") as ImageView;

                // Connected component labeling of a binary image
                Console.WriteLine("Labeling the binary input...");
                var imageLab = Processing.Labeling2d(imageInput);

                // Define the analysis features to be computed
                Console.WriteLine("Performing the label analysis...");
                AnalysisMsr analysis = new AnalysisMsr();
                var diameter = analysis.Select(NativeMeasurements.EquivalentDiameter);
                var feretRatio = analysis.Select(NativeMeasurements.FeretRatio2d);

                // Launch the feature extraction on the segmented image
                Processing.LabelAnalysis(imageLab, imageInput, analysis);
                int labelCount = analysis.LabelCount();
                Console.WriteLine("  - Number of objects = " + labelCount);
                Console.WriteLine("Label\t" + diameter.Name() + "\t" + feretRatio.Name());
                // Print the 4 first results of the analysis
                for (int i = 0; i < 4; i++)
                {
                    Console.WriteLine((i + 1) + "\t" + diameter.Value(i).ToString("0.00") + "\t\t\t" +
                                       feretRatio.Value(i).ToString("0.00"));
                }

                // Notify the garbage collector that the created images can be freed
                imageInput.Dispose();
                imageLab.Dispose();

            }
            catch (Exception error)
            {
                // Print potential exception in the standard output
                System.Console.WriteLine("HelloImageDev exception: " + error.ToString());
                status = -1;
            }

            // ImageDev library finalization
            Initialization.Finish();

            // Check if we must ask for an enter key to close the program
            if (!((args.Length >= 1) && (args[0] == "--no-stop-at-end")))
            {
                System.Console.WriteLine("Press Enter key to close this window.");
                System.Console.ReadKey();
            }

            System.Environment.Exit(status);
        }
    }
}
import imagedev
import imagedev_data

# Open a binary image from a vip file
image_input = imagedev.read_vip_image(imagedev_data.get_image_path("polystyrene_sep.vip"))

# Connected component labeling of the binary image
print("Labeling the binary input...")
image_lab = imagedev.labeling_2d(image_input, imagedev.Labeling2d.LabelType.LABEL_8_BIT)

# Define the analysis features to be computed
print("Performing the label analysis...")
analysis = imagedev.AnalysisMsr()
diameter = analysis.select(imagedev.native_measurements.EquivalentDiameter)
feret_ratio = analysis.select(imagedev.native_measurements.FeretRatio2d)

# Launch the feature extraction on the segmented image
imagedev.label_analysis(image_lab, image_input, analysis)
label_count = analysis.label_count
print("  - Number of objects = " + str(label_count))
# Print the 4 first results of the analysis
print("Label\t" + diameter.name + "\t" + feret_ratio.name)
for i in range(3):
  print(str(i+1) + '\t\t\t' + "{:.2f}".format(diameter.value(i)) + '\t\t\t\t' + "{:.2f}".format(feret_ratio.value(i)))


See also