import os
import sys, getopt
import PyIPSDK
import PyIPSDK.IPSDKIPLAdvancedMorphology as advmorpho
import PyIPSDK.IPSDKIPLShapeSegmentation as shapesegmentation
import PyIPSDK.IPSDKIPLShapeAnalysis as shapeanalysis
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:
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)
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
tmpPath = PyIPSDK.getIPSDKDefaultDirectory(PyIPSDK.eDefaultExternalDirectory.eDED_Tmp)
inImg = PyIPSDK.loadTiffImageFile(inputImgPath)
inBinImg = PyIPSDK.loadTiffImageFile(inputBinImgPath)
labelImg = advmorpho.connectedComponent2dImg(inBinImg)
PyIPSDK.saveTiffImageFile(os.path.join(tmpPath, "labelImg.tif"), labelImg)
shape2dColl = shapesegmentation.labelShapeExtraction2d(labelImg)
geometricCalibration = PyIPSDK.createGeometricCalibration2d(0.01, 0.02, "mm");
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")
measureSet = shapeanalysis.shapeAnalysis2d(inImg, shape2dColl, measureInfoSet2d)
areaMinusHolesOutMsr = measureSet.getMeasure("AreaMinusHoles")
areaMinusHolesRes = areaMinusHolesOutMsr.getMeasureResult()
print("First area minus holes = " + str(areaMinusHolesRes.getColl(0)[1]))
PyIPSDK.writeToXmlFile(outputXmlResultPath, measureSet)
try:
areaHistogram = areaMinusHolesRes.extractHistogram(15, 0)
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()
except:
print("This part of sample requires matplotlib to be executed.")
if __name__ == "__main__":