IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
# [DocSampleImports]
import os
import sys, getopt
import PyIPSDK
import PyIPSDK.IPSDKIPLFiltering as filter
import PyIPSDK.IPSDKIPLBinarization as bin
import PyIPSDK.IPSDKIPLAdvancedMorphology as advmorpho
import PyIPSDK.IPSDKIPLShapeAnalysis as shapeanalysis
# [DocSampleImports]
def readCmdArguments(argv):
# Default parameters values
imagesSamplePath = PyIPSDK.getIPSDKDirectory(PyIPSDK.eInternalDirectory.eID_Images)
inputImgPath = os.path.join(imagesSamplePath, "blobs3d_483x348x31_UInt8.tif")
tmpPath = PyIPSDK.getIPSDKDefaultDirectory(PyIPSDK.eDefaultExternalDirectory.eDED_Tmp)
outputReportPath = os.path.join(tmpPath, "quantification3d.csv")
inHalfKnlSize = 3
inSpaceSigma = 8
inDilateFactor = 5
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:o:k:s:f:",["inputImg3dFilePath=","outputReportPath=", "inHalfKnlSize=", "inSpaceSigma=", "inDilateFactor="])
except getopt.GetoptError:
print('<application_script_filename> [--inputImg3dFilePath <input_3d_image_file_path>] [--outputReportPath <output_report_file_path>] [--inHalfKnlSize <half_kernel_size>] [--inSpaceSigma <space_sigma_value>] [--inDilateFactor <dilate_factor>]')
sys.exit(2)
# Parse the program options
for opt, arg in opts:
if opt == '-h':
print('<application_script_filename> [--inputImg3dFilePath <input_3d_image_file_path>] [--outputReportPath <output_report_file_path>] [--inHalfKnlSize <half_kernel_size>] [--inSpaceSigma <space_sigma_value>] [--inDilateFactor <dilate_factor>]')
sys.exit(0)
elif opt in ("-i", "--inputImg3dFilePath"):
inputImgPath = arg
elif opt in ("-k", "--inHalfKnlSize"):
inHalfKnlSize = int(arg)
elif opt in ("-s", "--inSpaceSigma"):
inSpaceSigma = float(arg)
elif opt in ("-f", "--inDilateFactor"):
inDilateFactor = int(arg)
return inputImgPath, outputReportPath, inHalfKnlSize, inSpaceSigma, inDilateFactor
def main(argv):
tmpPath = PyIPSDK.getIPSDKDefaultDirectory(PyIPSDK.eDefaultExternalDirectory.eDED_Tmp)
# [DocSampleDefineArguments]
# retrieve program parameters
inputImgPath, outputReportPath, inHalfKnlSize, inSpaceSigma, inDilateFactor = readCmdArguments(argv)
# [DocSampleDefineArguments]
# [DocSampleLoadImage]
# opening of input image
greyImg = PyIPSDK.loadTiffImageFile(inputImgPath, PyIPSDK.eTiffDirectoryMode.eTDM_Volume)
# [DocSampleLoadImage]
# [DocSampleFilteringStep]
# filtering of input image to smooth defaults
greyFilteredImg = filter.separatedBilateral3dImg(greyImg, 3, 8)
# [DocSampleFilteringStep]
# [DocSampleBinarizationStep]
# auto threshold on filtered image
binImg, threshold = bin.otsuThresholdImg(greyFilteredImg)
PyIPSDK.saveTiffImageFile(os.path.join(tmpPath, "binImg.tif"), binImg)
# [DocSampleBinarizationStep]
# [DocSampleSeparationStep]
# automatic separation of binary shapes
binSepImg = advmorpho.watershedBinarySeparation3dImg(binImg, 5, PyIPSDK.eWatershedSeparationMode.eWSM_Split)
PyIPSDK.saveTiffImageFile(os.path.join(tmpPath, "binSepImg.tif"), binSepImg)
# [DocSampleSeparationStep]
# [DocSampleConnectedComponentsStep]
# connected components analysis of separated binary image
labelImg = advmorpho.connectedComponent3dImg(binSepImg)
PyIPSDK.saveTiffImageFile(os.path.join(tmpPath, "labelImg.tif"), labelImg)
# [DocSampleConnectedComponentsStep]
# [DocSampleDefineMeasureSet]
# definition of a geometric calibration (if available)
geometricCalibration = PyIPSDK.createGeometricCalibration3d(0.01, 0.02, 0.05, "mm");
# definition of measure set on shapes
measureInfoSet3d = PyIPSDK.createMeasureInfoSet3d(geometricCalibration);
PyIPSDK.createMeasureInfo(measureInfoSet3d, "Area3dMsr");
PyIPSDK.createMeasureInfo(measureInfoSet3d, "EquivalentRayMsr");
PyIPSDK.createMeasureInfo(measureInfoSet3d, "MaxFeretDiameterMsr", shapeanalysis.createMaxFeretDiameterMsrParams(180));
PyIPSDK.createMeasureInfo(measureInfoSet3d, "MeanMsr");
# [DocSampleDefineMeasureSet]
# [DocSampleShapeAnalysis]
# shape analysis computation
measureSet = shapeanalysis.labelAnalysis3d(greyImg, labelImg, measureInfoSet3d)
# [DocSampleShapeAnalysis]
# [DocSampleSaveReport]
# save report in csv format
print("Saving measure report in file " + outputReportPath)
PyIPSDK.saveCsvMeasureFile(outputReportPath, measureSet)
# [DocSampleSaveReport]
# [DocSampleAccessData]
# retrieve area measure results
areaMsrResults = measureSet.getMeasure("Area3dMsr").getMeasureResult()
# extract associated results collection
areaValues = areaMsrResults.getColl(0)
print("First label area equals " + str(areaValues[1]))
# [DocSampleAccessData]
try:
# [DocPlotResults]
# retrieve histogram from measure results
areaHistogram = areaMsrResults.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:])