IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit

module demonstrating histogram computation in case of a 2d image More...

module demonstrating histogram computation in case of a 2d image

Overview

This application computes histogram of an input 2d rgb image. Histogram results are saved in a given csv file.

See also
histogram 2d algorithm

Usage

This is a standalone script file which takes no input argument.

Here is a snapshot of input image used by the application and of corresponding results :

inputHistogram2d.png

Source code documentation

We start by importing all necessary libraries:

import os
import sys, getopt
import PyIPSDK
import PyIPSDK.IPSDKIPLGlobalMeasure as glbmsr

Then we define the input parameters.

# define input image path
imagesSamplePath = PyIPSDK.getIPSDKDirectory(PyIPSDK.eInternalDirectory.eID_Images)
inputImgPath = os.path.join(imagesSamplePath, "Lena_RGB_510x509_UInt8.tif")
# define output data path
outputDataPath = PyIPSDK.getIPSDKDefaultDirectory(PyIPSDK.eDefaultExternalDirectory.eDED_Tmp)
histoCvsPath = os.path.join(outputDataPath, "histogram.csv")

We load our input image from the associated Tiff file, by calling the function ipsdk::image::file::loadTiffImageFile.

# opening of input image
inImg = PyIPSDK.loadTiffImageFile(inputImgPath)

We then apply the histogram computation on the input image using a classical histogram algorithm and a faster one based on a kernel density estimation algorithm.

# definition of histogram measure parameters
binMin = 0
binMax = 255
binWidth = 2
histogramMsrParams = PyIPSDK.createHistoMsrParamsWithBinWidth(binMin, binMax, binWidth)
# computation of histogram for each color planes
planIndexedHisto = glbmsr.multiSlice_histogramMsr2d(inImg, histogramMsrParams)
# retrieve 'by plane' histograms (used for plot, see below)
redHisto = planIndexedHisto.getValue(0)
greenHisto = planIndexedHisto.getValue(1)
blueHisto = planIndexedHisto.getValue(2)

We then save the results into output csv files.

# save generated histograms
print("Saving histogram data to file " + histoCvsPath)
PyIPSDK.exportToCsv(histoCvsPath, planIndexedHisto)

Lastly, if matplotlib python library is available, we plot results into a graph.

# plot histogram results
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()
histogram2d.png

See the full source listing