IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit

module demonstrating shape analysis and measurement in 2d case More...

module demonstrating shape analysis and measurement in 2d case

Overview

This application computes measurements on 2d shapes from an input image TIFF files couple (a grey level and a binary image).

See also
Shape Analysis 2d algorithm
Shape measurement

Usage

The application can be called through a command line as follows:

   <application_exe_filename> [--inputGreyImgPath <input_grey_image_file_path>] [--inputBinImgPath <input_binary_image_file_path>] [--outputXmlResultPath <output xml result path>]
     
   Arguments:
      --inputGreyImgPath      optional; specifies the name of the TIFF file, from
                              which the input grey level image will be loaded; if not 
                              specified by the user, the input image is loaded from
                              file 
                              <DEV_ROOT>/data/Sample/images/blobs_483x348_UInt8.tif
                          
      --inputBinImgPath       optional; specifies the name of the TIFF file, from
                              which the input binary image will be loaded; if not 
                              specified by the user, the input image is loaded from
                              file 
                              <DEV_ROOT>/data/Sample/images/blobs_483x348_Binary.tif

       --outputXmlResultPath  optional; specifies the name of the XML file, in
                              which the output analysis results will be saved; if not 
                              specified by the user, the output results file is saved to
                              file 
                              <TEMPORARY_IPSDK_DIR>/Sample/shapeAnalysis2d.xml

Here is a snapshot of default input images used by the application and of corresponding output results when application is launched without any argument:

Sample_ShapeAnalysis2d.png
Note
Connected components (labels) are indexed from left to right and top to bottom.

Source code documentation

We start by importing all necessary libraries:

import os
import sys, getopt
import PyIPSDK
import PyIPSDK.IPSDKIPLAdvancedMorphology as advmorpho
import PyIPSDK.IPSDKIPLShapeSegmentation as shapesegmentation
import PyIPSDK.IPSDKIPLShapeAnalysis as shapeanalysis

Then we define the input parameters.

# retrieve program parameters
inputImgPath, inputBinImgPath, outputXmlResultPath = readCmdArguments(argv)

We load our input grey level and binary images from the associated Tiff file, by calling the function ipsdk::image::file::loadTiffImageFile.

# opening of input image
inImg = PyIPSDK.loadTiffImageFile(inputImgPath)
inBinImg = PyIPSDK.loadTiffImageFile(inputBinImgPath)

We then apply a connected components algorithm on input binary image (see Connected Component 2d).

A contour based version of this 'labeled' image is then processed. The user should note that a polygonal approximation of contours is processed during previous step (see Label shape extraction 2d).

We then define a geometric calibration which will be used during shape measurement process (see Handling geometric calibration).

At last we define a collection of measures to be processed and we launch the shape analysis (see Shape Analysis 2d).

# connected components analysis of separated binary image
labelImg = advmorpho.connectedComponent2dImg(inBinImg)
PyIPSDK.saveTiffImageFile(os.path.join(tmpPath, "labelImg.tif"), labelImg)
# extraction of contours from connected components image
shape2dColl = shapesegmentation.labelShapeExtraction2d(labelImg)
# definition of a geometric calibration (if available)
geometricCalibration = PyIPSDK.createGeometricCalibration2d(0.01, 0.02, "mm");
# definition of measure set on shapes
measureInfoSet2d = PyIPSDK.createMeasureInfoSet2d(geometricCalibration)
PyIPSDK.createMeasureInfo(measureInfoSet2d, "AreaMinusHoles", "Area2dMsr", shapeanalysis.createHolesBasicPolicyMsrParams(True))
PyIPSDK.createMeasureInfo(measureInfoSet2d, "AreaWithHoles", "Area2dMsr", shapeanalysis.createHolesBasicPolicyMsrParams(False))
PyIPSDK.createMeasureInfo(measureInfoSet2d, "EquivalentRay", "EquivalentRayMsr")
PyIPSDK.createMeasureInfo(measureInfoSet2d, "SumMsr")
# shape analysis computation
measureSet = shapeanalysis.shapeAnalysis2d(inImg, shape2dColl, measureInfoSet2d)

Measure results can then be individually extracted from returned object measureSet has demonstrated by following code (extracted type must match measure results type, see measure result type associated to Area 2d for example) :

# retrieve area 2d results for 'minus holes' parametrization for example
# (note that in case of default input image, shapes are not associated to holes
# so both area measures have same values)
areaMinusHolesOutMsr = measureSet.getMeasure("AreaMinusHoles")
areaMinusHolesRes = areaMinusHolesOutMsr.getMeasureResult()
print("First area minus holes = " + str(areaMinusHolesRes.getColl(0)[1]))

Measure results can also be saved to file using XML file format (for example) using following code :

PyIPSDK.writeToXmlFile(outputXmlResultPath, measureSet)

Or plotted using histogram measure feature (requests matplotlib to be installed):

# retrieve histogram from measure results
areaHistogram = areaMinusHolesRes.extractHistogram(15, 0)
# plot histogram results
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.bar(areaHistogram.getBinMidValues(), areaHistogram.getFrequencies(), areaHistogram.getBinWidth(), color='r')
plt.title("Area histogram")
plt.xlabel('Bin mean')
plt.ylabel('Frequencies')
plt.grid(True)
plt.show()
Sample_ShapeAnalysis2d_plot.png

See the full source listing