Skip to content

Instantly share code, notes, and snippets.

@skuschel
Last active February 26, 2019 21:21
Show Gist options
  • Save skuschel/4823d53067263507a04c07ff7f84255f to your computer and use it in GitHub Desktop.
Save skuschel/4823d53067263507a04c07ff7f84255f to your computer and use it in GitHub Desktop.
Single photon counting
def findsinglephotons(image, thresupper, threslower, maxsize=2):
'''
identifies single photons in the image and returns the for each photon the integrated counts on the image
* a photon need to be above the thresupper in order to be found
* surrounding regions of a photon will be added as long as they are above the threslower value
* maxsize=2 controlls over how many pixels a photon may spread
Stephan Kuschel, 2019
https://gist.github.com/skuschel/4823d53067263507a04c07ff7f84255f
'''
import skimage.measure as skm
import numpy as np
up = skm.label(image > thresupper, connectivity=1)
N = np.max(up)
print('{} possible events found'.format(N))
upNs = np.bincount(up.flatten())
low = skm.label(image > threslower, connectivity=1)
lowNs = np.bincount(low.flatten())
photons = []
#finalmask = np.zeros_like(image)
for iup, upN in enumerate(upNs):
if upN > maxsize:
continue
# find index of value iup in up
upmask = up==iup
ilow = np.max(low[upmask])
if lowNs[ilow] > maxsize:
continue
mask = low==ilow
photons.append(np.sum(image[mask]))
#finalmask[mask] += 1
return np.asarray(photons)#, finalmask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment