Commit 6d68f32c authored by Gustavo Pinzon's avatar Gustavo Pinzon
Browse files

findHistogramPeaks now allows to save the plot

parent 1d3af8e7
Pipeline #61223 passed with stages
in 25 minutes and 43 seconds
......@@ -4,7 +4,7 @@ import matplotlib.pyplot as plt
import numpy
def findHistogramPeaks(image, valley1=20000, valley2=[], phases=2, gaussianFit=False, returnSigma=False, mask=True, showGraph=False, greyRange=[0,65535], bins=256):
def findHistogramPeaks(image, valley1=20000, valley2=[], phases=2, gaussianFit=False, returnSigma=False, mask=True, showGraph=False, greyRange=[0,65535], bins=256, saveFigPath = None):
"""
This function finds the peaks of the phases of a greylevel image (16bit or 8bit). The peaks are in greylevel units. Minimum number of Phases is 2 and Maximum is 3.
......@@ -47,6 +47,10 @@ def findHistogramPeaks(image, valley1=20000, valley2=[], phases=2, gaussianFit=F
bins : int, optional
Default = 256
saveFigPath : string, optional
Path to save figure to.
Default = None
Returns
--------
......@@ -126,23 +130,27 @@ def findHistogramPeaks(image, valley1=20000, valley2=[], phases=2, gaussianFit=F
peakPhases = numpy.array([peakPhase1,peakPhase2])
if showGraph == True:
fig = plt.figure(1)
plt.plot(totalgreylevel,totalCounts,label="Histogram")
fig = plt.figure(1)
plt.plot(totalgreylevel,totalCounts,label="Histogram")
plt.plot(peakPhase1,countsPhase1[countsPhase1==countsPhase1.max()],"ro",label="Peak Phase 1 @ {:5.0f}".format(float(peakPhase1)))
plt.plot(peakPhase2,countsPhase2[countsPhase2==countsPhase2.max()],"yo",label="Peak Phase 2 @ {:5.0f}".format(float(peakPhase2)))
plt.xlabel("Greylevel")
plt.ylabel("Counts")
plt.plot(peakPhase1,countsPhase1[countsPhase1==countsPhase1.max()],"ro",label="Peak Phase 1 @ {:5.0f}".format(float(peakPhase1)))
plt.plot(peakPhase2,countsPhase2[countsPhase2==countsPhase2.max()],"yo",label="Peak Phase 2 @ {:5.0f}".format(float(peakPhase2)))
plt.xlabel("Greylevel")
plt.ylabel("Counts")
if gaussianFit:
plt.plot(totalgreylevel[startIndex1:stopIndex1], gauss(totalgreylevel[startIndex1:stopIndex1], a1, peakPhase1, sigmaPhase1, offset1), label='Fit 1')
plt.plot(totalgreylevel[startIndex2:stopIndex2], gauss(totalgreylevel[startIndex2:stopIndex2], a2, peakPhase2, sigmaPhase2, offset2), label='Fit 2')
if gaussianFit:
plt.plot(totalgreylevel[startIndex1:stopIndex1], gauss(totalgreylevel[startIndex1:stopIndex1], a1, peakPhase1, sigmaPhase1, offset1), label='Fit 1')
plt.plot(totalgreylevel[startIndex2:stopIndex2], gauss(totalgreylevel[startIndex2:stopIndex2], a2, peakPhase2, sigmaPhase2, offset2), label='Fit 2')
plt.legend()
fig.tight_layout()
plt.legend()
fig.tight_layout()
if saveFigPath is not None:
plt.savefig(saveFigPath)
if showGraph == True:
plt.show()
plt.close()
if phases==3:
......
......@@ -83,8 +83,11 @@ class testAll(unittest.TestCase):
# Check that it does not fail when the image is too small and gaussianFit = True
im = spam.label.label.Spheroid(10, 10, numpy.asarray([0, 0, 1])).digitize()
#im = spam.kalisphera.kalisphera.makeBlurryNoisySphere([10,10,10], [5,5,5], 4, 0.5, 0.05, background=0.25, foreground=0.75)
im = im.astype(int)
res = spam.helpers.histogramTools.findHistogramPeaks(im, valley1 = 0.5, greyRange = [0, 1], gaussianFit = True)
im = (im*0.5)+0.25
res = spam.helpers.histogramTools.findHistogramPeaks(im, valley1 = 0.5, greyRange = [0, 1], gaussianFit = False)
self.assertIsNot(res, None)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment