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_RGB_510x509_UInt8.tif")
outputDataPath = PyIPSDK.getIPSDKDefaultDirectory(PyIPSDK.eDefaultExternalDirectory.eDED_Tmp)
histoCvsPath = os.path.join(outputDataPath, "histogram.csv")
inImg = PyIPSDK.loadTiffImageFile(inputImgPath)
binMin = 0
binMax = 255
binWidth = 2
histogramMsrParams = PyIPSDK.createHistoMsrParamsWithBinWidth(binMin, binMax, binWidth)
planIndexedHisto = glbmsr.multiSlice_histogramMsr2d(inImg, histogramMsrParams)
redHisto = planIndexedHisto.getValue(0)
greenHisto = planIndexedHisto.getValue(1)
blueHisto = planIndexedHisto.getValue(2)
print("Saving histogram data to file " + histoCvsPath)
PyIPSDK.exportToCsv(histoCvsPath, planIndexedHisto)
try:
import matplotlib.pyplot as plt
redPlot, = plt.plot(redHisto.getBinMeanColl(), redHisto.frequencies, 'r+-', label="Red")
greenPlot, = plt.plot(greenHisto.getBinMeanColl(), greenHisto.frequencies, 'go-', label="Green")
bluePlot, = plt.plot(blueHisto.getBinMeanColl(), blueHisto.frequencies, 'b^-', label="Blue")
plt.title("Image histograms")
plt.xlabel('Bin mean')
plt.ylabel('Population')
plt.legend(handles=[redPlot, greenPlot, bluePlot])
plt.grid(True)
plt.show()
except:
print("This part of sample requires matplotlib to be executed.")