This repository was archived by the owner on Aug 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlocalmax2d.py
More file actions
executable file
·92 lines (77 loc) · 2.38 KB
/
localmax2d.py
File metadata and controls
executable file
·92 lines (77 loc) · 2.38 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
# from scipy.ndimage.filters import maximum_filter
from pathlib import Path
from skimage.feature import peak_local_max
from astropy.io import fits
from skimage.draw import circle
from matplotlib.pyplot import figure, show
from matplotlib.colors import LogNorm
"""
Michael Hirsch 2014
Find local maximum in FITS images (just ask and I'll add other formats)
References:
http://stackoverflow.com/questions/3684484/peak-detection-in-a-2d-array
http://scikit-image.org/docs/dev/auto_examples/plot_peak_local_max.html?highlight=local%20maxima
"""
def getfitsimg(fn):
fn = Path(fn).expanduser()
with fits.open(str(fn), mode="readonly") as f:
return f[0].data
def get2Dpeaks(img, fn, mindist, relintthres):
coord = peak_local_max(
img,
min_distance=mindist,
threshold_rel=relintthres,
exclude_border=False,
indices=True,
)
fg = figure()
ax = fg.gca()
hi = ax.imshow(img, cmap="gray", norm=LogNorm())
fg.colorbar(hi).set_label("data numbers")
ax.autoscale(False)
"""
x=col=coord[:,1]
y=row=coord[:,0]
"""
ax.plot(coord[:, 1], coord[:, 0], "r*", markersize=8)
# ax.axis('off')
ax.set_title(fn)
return coord
def getpeaksums(img, pks, crad):
sums = []
for p in pks: # iterate down rows
c = circle(p[0], p[1], radius=crad, shape=img.shape)
sums.append(img[c].sum())
return sums
if __name__ == "__main__":
from argparse import ArgumentParser
p = ArgumentParser(
description="finds local maxima in images, computes sum within radius of each local maximum"
)
p.add_argument("imgs", help="list of images you want to load", nargs="+")
p.add_argument("-c", "--crad", help="circle summation radius", type=int, default=4)
p.add_argument(
"-m",
"--mindist",
help="minimum distance to neighor peak (pixels)",
type=int,
default=80,
)
p.add_argument(
"-r",
"--relintthres",
help="minimum intensity relative to peak intensity",
type=float,
default=0.25,
)
a = p.parse_args()
fl = a.imgs
crad = a.crad
for f in fl:
img = getfitsimg(f)
peaks = get2Dpeaks(img, f, a.mindist, a.relintthres)
sums = getpeaksums(img, peaks, crad)
print("rows", peaks[:, 0])
print("sums radius", crad, sums)
show()