IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
# [DocSampleImports]
import os
import sys, getopt
import PyIPSDK
import PyIPSDK.IPSDKIPLAdvancedMorphology as advmorpho
import PyIPSDK.IPSDKIPLShapeSegmentation as shapesegmentation
import PyIPSDK.IPSDKIPLShapeAnalysis as shapeanalysis
# [DocSampleImports]
def readCmdArguments(argv):
# Default parameters values
imagesSamplePath = PyIPSDK.getIPSDKDirectory(PyIPSDK.eInternalDirectory.eID_Images)
inputImgPath = os.path.join(imagesSamplePath, "blobs_483x348_UInt8.tif")
inputBinImgPath = os.path.join(imagesSamplePath, "blobs_483x348_Binary.tif")
tmpPath = PyIPSDK.getIPSDKDefaultDirectory(PyIPSDK.eDefaultExternalDirectory.eDED_Tmp)
outputXmlResultPath = os.path.join(tmpPath, "shapeAnalysis2d.xml")
try:
# Read the command line, the second argument is a string with all the possible options (short version)
# with a ':' if the option requires an argument and the third argument is the set of trings corresponding
# to the long versions of the program options
opts, args = getopt.getopt(argv,"hi:b:o:",["inputGreyImgPath=","inputBinImgPath=","outputXmlResultPath="])
except getopt.GetoptError:
print('<application_script_filename> [--inputGreyImgPath <input_grey_image_file_path>] [--inputBinImgPath <input_binary_image_file_path>] [--outputXmlResultPath <output xml result path>]')
sys.exit(2)
# Parse the program options
for opt, arg in opts:
if opt == '-h':
print('<application_script_filename> [--inputGreyImgPath <input_grey_image_file_path>] [--inputBinImgPath <input_binary_image_file_path>] [--outputXmlResultPath <output xml result path>]')
sys.exit(0)
elif opt in ("-i", "--inputGreyImgPath"):
inputImgPath = arg
elif opt in ("-b", "--inputBinImgPath"):
inputBinImgPath = arg
elif opt in ("-o", "--outputXmlResultPath"):
outputXmlResultPath = arg
return inputImgPath, inputBinImgPath, outputXmlResultPath
def main(argv):
tmpPath = PyIPSDK.getIPSDKDefaultDirectory(PyIPSDK.eDefaultExternalDirectory.eDED_Tmp)
# [DocSampleDefineArguments]
# retrieve program parameters
inputImgPath, inputBinImgPath, outputXmlResultPath = readCmdArguments(argv)
# [DocSampleDefineArguments]
# [DocSampleLoadImage]
# opening of input image
inImg = PyIPSDK.loadTiffImageFile(inputImgPath)
inBinImg = PyIPSDK.loadTiffImageFile(inputBinImgPath)
# [DocSampleLoadImage]
# [DocSampleProcess]
# 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)
# [DocSampleProcess]
# [DocSampleExtractResults]
# 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]))
# [DocSampleExtractResults]
# [DocSampleSaveResults]
PyIPSDK.writeToXmlFile(outputXmlResultPath, measureSet)
# [DocSampleSaveResults]
try:
# [DocPlotResults]
# 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()
# [DocPlotResults]
except:
print("This part of sample requires matplotlib to be executed.")
if __name__ == "__main__":
main(sys.argv[1:])