import os
import sys, getopt
import PyIPSDK
import PyIPSDK.IPSDKIPLGlobalMeasure as glbmsr
imagesSamplePath = PyIPSDK.getIPSDKDirectory(PyIPSDK.eInternalDirectory.eID_Images)
inputImgPath = os.path.join(imagesSamplePath, "Lena_510x509_UInt8.tif")
outputDataPath = PyIPSDK.getIPSDKDefaultDirectory(PyIPSDK.eDefaultExternalDirectory.eDED_Tmp)
refCsvPath = os.path.join(outputDataPath, "ref_histogram.csv")
estimCsvPath = os.path.join(outputDataPath, "estim_histogram.csv")
inImg = PyIPSDK.loadTiffImageFile(inputImgPath)
nbTotPixels = inImg.getGeometry().getNbPixels()
binMin = 0
binMax = 255
binWidth = 2
histogramMsrParams = PyIPSDK.createHistoMsrParamsWithBinWidth(binMin, binMax, binWidth)
refHisto = glbmsr.histogramMsr2d(inImg, histogramMsrParams)
kdeDataSet = glbmsr.kernelDensityEstimator2d(inImg)
estimHisto = PyIPSDK.generateHistogram(kdeDataSet,
binMin, binMax, binWidth,
nbTotPixels)
print("Saving histograms data to directory " + outputDataPath)
PyIPSDK.exportToCsv(refCsvPath, refHisto)
PyIPSDK.exportToCsv(estimCsvPath, estimHisto)
try:
import matplotlib.pyplot as plt
refPlot, = plt.plot(refHisto.getBinMeanColl(), refHisto.frequencies, 'g+-', label="Reference")
estimPlot, = plt.plot(estimHisto.getBinMeanColl(), estimHisto.frequencies, 'ro-', label="Estimation")
plt.title("Image histograms")
plt.xlabel('Bin mean')
plt.ylabel('Population')
plt.legend(handles=[refPlot, estimPlot])
plt.grid(True)
plt.show()
except:
print("This part of sample requires matplotlib to be executed.")