-
Notifications
You must be signed in to change notification settings - Fork 0
/
Img2cells_2D.py
192 lines (151 loc) · 4.93 KB
/
Img2cells_2D.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 15 17:39:59 2019
@author: fwaharte
"""
#open 3D stack from .tiff multipage file
from skimage.io import imread, imsave
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Ellipse, Rectangle
from matplotlib.collections import PatchCollection
from collections import deque
def circles(x, y, s, c='b', vmin=None, vmax=None, **kwargs):
"""
See https://gist.github.com/syrte/592a062c562cd2a98a83
Make a scatter plot of circles.
Similar to plt.scatter, but the size of circles are in data scale.
Parameters
----------
x, y : scalar or array_like, shape (n, )
Input data
s : scalar or array_like, shape (n, )
Radius of circles.
c : color or sequence of color, optional, default : 'b'
`c` can be a single color format string, or a sequence of color
specifications of length `N`, or a sequence of `N` numbers to be
mapped to colors using the `cmap` and `norm` specified via kwargs.
Note that `c` should not be a single numeric RGB or RGBA sequence
because that is indistinguishable from an array of values
to be colormapped. (If you insist, use `color` instead.)
`c` can be a 2-D array in which the rows are RGB or RGBA, however.
vmin, vmax : scalar, optional, default: None
`vmin` and `vmax` are used in conjunction with `norm` to normalize
luminance data. If either are `None`, the min and max of the
color array is used.
kwargs : `~matplotlib.collections.Collection` properties
Eg. alpha, edgecolor(ec), facecolor(fc), linewidth(lw), linestyle(ls),
norm, cmap, transform, etc.
Returns
-------
paths : `~matplotlib.collections.PathCollection`
Examples
--------
a = np.arange(11)
circles(a, a, s=a*0.2, c=a, alpha=0.5, ec='none')
plt.colorbar()
License
--------
This code is under [The BSD 3-Clause License]
(http://opensource.org/licenses/BSD-3-Clause)
"""
if np.isscalar(c):
kwargs.setdefault('color', c)
c = None
if 'fc' in kwargs:
kwargs.setdefault('facecolor', kwargs.pop('fc'))
if 'ec' in kwargs:
kwargs.setdefault('edgecolor', kwargs.pop('ec'))
if 'ls' in kwargs:
kwargs.setdefault('linestyle', kwargs.pop('ls'))
if 'lw' in kwargs:
kwargs.setdefault('linewidth', kwargs.pop('lw'))
# You can set `facecolor` with an array for each patch,
# while you can only set `facecolors` with a value for all.
zipped = np.broadcast(x, y, s)
patches = [Circle((x_, y_), s_)
for x_, y_, s_ in zipped]
collection = PatchCollection(patches, **kwargs)
if c is not None:
c = np.broadcast_to(c, zipped.shape).ravel()
collection.set_array(c)
collection.set_clim(vmin, vmax)
#ax = plt.gca()
ax.add_collection(collection)
ax.autoscale_view()
plt.draw_if_interactive()
if c is not None:
plt.sci(collection)
return collection
img=imread('microglia_cloud2.tif')
print(img.shape)
#plt.imshow(img[0,:,:])
xmin = -500.
xmax = 500
ymin = -500
ymax = 500
x_range = xmax - xmin
y_range = ymax - ymin
yval = 0.0
num_cells = 0
fig_height = 7.0
fig_width = fig_height*x_range/y_range
fig = plt.figure(figsize=(fig_width,fig_height))
ax = fig.gca()
#ax.set_aspect("equal")
xlist = deque()
ylist = deque()
rlist = deque()
rgb_list = deque()
cell_radius = 8
cell_spacing = 1.2 * 2.0 * cell_radius;
print("cell radius and spacing = ",cell_radius,cell_spacing)
img_xrange = img.shape[0]
img_yrange = img.shape[1]
fp = open('cells.dat','w')
ydel = cell_spacing * np.sqrt(3.0)/2.0
#ydel = cell_radius * np.sqrt(3.0)/2.0
ny = 0
zval = 0.0
# Loop over entire (mesh) domain and create hex-packed cells only where image appears.
for yval in np.arange(ymin,ymax,ydel):
idy = int(img_yrange * (yval-ymin)/y_range)
xdel = 0.0
if ny % 2:
xdel = 0.5*cell_spacing
ny += 1
for xval in np.arange(xmin+xdel,xmax+xdel,cell_spacing):
idx = int(img_xrange * (xval-xmin)/x_range)
if idx > img_xrange:
continue
if (img[idx,idy] >=1): # red-ish
xlist.append(yval)
ylist.append(-xval)
rlist.append(cell_radius)
rgb = list(map(int, "255,0,0".split(",")))
rgb[:]=[x/255. for x in rgb]
rgb_list.append(rgb)
cell_type = 1
cell_str = '%f %f\n' % (yval , -xval)
fp.write(cell_str)
num_cells += 1
fp.close()
xvals = np.array(xlist)
yvals = np.array(ylist)
rvals = np.array(rlist)
rgbs = np.array(rgb_list)
# print('type(rgbs) = ',type(rgbs))
# print('rgbs = ',rgbs)
#print("xvals[0:5]=",xvals[0:5])
#print("rvals[0:5]=",rvals[0:5])
# print("rvals.min, max=",rvals.min(),rvals.max())
# plt.cla()
# title_str += " (" + str(num_cells) + " agents)"
# plt.title(title_str)
axes_min = xmin
axes_max = xmax
plt.xlim(xmin,xmax)
plt.ylim(ymin,ymax)
circles(xvals,yvals, s=rvals, color=rgbs, edgecolor='black') # alpha=1.0, edgecolor='black'
plt.show()