forked from kapoorlab/KapoorLabs-IJ-Macros
/
CountCells2D.py
65 lines (54 loc) · 2.07 KB
/
CountCells2D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# A script to find cells by difference of Gaussian using imglib2.
# Uses as an example the "first-instar-brain.tif" RGB stack availalable
# from Fiji's "Open Samples" menu.
from ij import IJ
from net.imglib2.img.display.imagej import ImageJFunctions as IJF
from net.imglib2.view import Views
from net.imglib2.converter import Converters
from net.imglib2.algorithm.dog import DogDetection
from net.imglib2.type.numeric.real import DoubleType
from jarray import zeros
from java.awt import Color
from ij.gui import PointRoi, OvalRoi , Overlay
from ij.plugin.frame import RoiManager
from ij.gui import WaitForUserDialog, Toolbar
#remove all the previous ROIS
imp = IJ.getImage()
rm = RoiManager.getInstance()
if not rm:
rm = RoiManager()
rm.runCommand("reset")
#ask the user to define a selection and get the bounds of the selection
IJ.setTool(Toolbar.RECTANGLE)
WaitForUserDialog("Select the area,then click OK.").show();
boundRect = imp.getRoi()
imp.setRoi(boundRect)
rm.addRoi(boundRect)
imp = IJ.getImage()
cal = imp.getCalibration() # in microns
img = IJF.wrap(imp)
print(img.dimensions)
# Create a variable of the correct type (UnsignedByteType) for the value-extended view
zero = img.randomAccess().get().createVariable()
# Run the difference of Gaussian
cell = 30.0 # microns in diameter
min_peak = 10.0 # min intensity for a peak to be considered
dog = DogDetection(Views.extendValue(img, zero), img,
[cal.pixelWidth, cal.pixelHeight],
cell / 2, cell,
DogDetection.ExtremaType.MAXIMA, #MAXIMA
min_peak, False,
DoubleType())
peaks = dog.getPeaks()
roi = OvalRoi(0, 0, cell/cal.pixelWidth, cell/cal.pixelHeight)
print ('Number of cells = ', len(peaks))
p = zeros(img.numDimensions(), 'i')
overlay = Overlay()
imp.setOverlay(overlay)
for peak in peaks:
# Read peak coordinates into an array of integers
peak.localize(p)
if(boundRect.contains(p[0], p[1])):
oval = OvalRoi(p[0], p[1],cell/cal.pixelWidth, cell/cal.pixelHeight)
oval.setColor(Color.RED)
overlay.add(oval)