IPSDK 0.2
IPSDK : Image Processing Software Development Kit
Distance to nearest neighbor 2dSee full documentation

computation of distance to nearest neighbor for each shape

This measure allows to compute the distance to the nearest neighbor for each shape. It is based on a geometric analysis of the shape neighborhood.

A polygonal approximation of shapes is used to compute the distances between shapes.

The parameter flag ProcessHoles allows to specify whether shape holes should be taken into account during computation :

distanceToNearestNeighbor2dMsr_processHoles.png

In the previous figure :

Here is an example of nearest neighbor computation for several shapes :

distanceToNearestNeighbor2dMsr.png

In the previous figure shapes (ie. the polygonal approximation of boundaries), are colored with respect to the computed distance to its nearest neighbor.

computation of distance to nearest neighbor for each shape

Measure synthesis :

Measure Type Measure Unit Type Parameter Type Result Type Shape Requirements
Geometry2d.png
Geometry 2d
length.png
Length
parameter.png
DistanceToNearestNeighbor2dMsrParams
Value.png
Value (ipsdk::ipReal64)
BoundaryApproximation.png
Boundary Approximation
See Shape measurement for additional information on these pictograms

Example of Python code :

Generic example in 2d case :

import PyIPSDK
import PyIPSDK.IPSDKIPLShapeAnalysis as shapeanalysis
# Create the infoset
inMeasureInfoSet2d = PyIPSDK.createMeasureInfoSet2d()
PyIPSDK.createMeasureInfo(inMeasureInfoSet2d, "DistanceToNearestNeighbor2dMsr")
#Perform the analysis
outMeasureSet = shapeanalysis.labelAnalysis2d(inGreyImg, inLabelImg2d, inMeasureInfoSet2d)
# save results to csv format
PyIPSDK.saveCsvMeasureFile(os.path.join(tmpPath, "shape_analysis_results.csv"), outMeasureSet)
# retrieve measure results
outMsr = outMeasureSet.getMeasure("DistanceToNearestNeighbor2dMsr")
# retrieve measure values
outMsrValues = outMsr.getMeasureResult().getColl(0)
print("First label measurement equal " + str(outMsrValues[1]))

Example of C++ code :

Example informations

Associated library

IPSDKIPLShapeAnalysis

Code Example

// opening grey level input image
ImagePtr pInGreyImg2d = loadTiffImageFile(inputGreyImgPath);
// read entity shape 2d collection used for processing
Shape2dCollPtr pShape2dColl = boost::make_shared<Shape2dColl>();
IPSDK_REQUIRE(readFromXmlFile(inputShape2dCollPath, *pShape2dColl) == true);
// define a measure info set
MeasureInfoSetPtr pMeasureInfoSet = MeasureInfoSet::create2dInstance();
createMeasureInfo(pMeasureInfoSet, "DistanceToNearestNeighbor2dMsr", createHolesBasicPolicyMsrParams(bProcessHoles));
// compute measure on shape 2d collection
MeasureSetPtr pOutMeasureSet = shapeAnalysis2d(pInGreyImg2d, pShape2dColl, pMeasureInfoSet);
// retrieve associated results
const MeasureConstPtr& pDistanceToNearestNeighbor2dOutMsr = pOutMeasureSet->getMeasure("DistanceToNearestNeighbor2dMsr");
const ipsdk::shape::analysis::ValueMeasureResult<ipsdk::ipReal64>& outResults = static_cast<const ipsdk::shape::analysis::ValueMeasureResult<ipsdk::ipReal64>&>(pDistanceToNearestNeighbor2dOutMsr->getMeasureResult());