Skip to content

Instantly share code, notes, and snippets.

@huangziwei
Created May 9, 2017 12:41
Show Gist options
  • Save huangziwei/1a3949a6e53f50c63f75cd52c5bc634b to your computer and use it in GitHub Desktop.
Save huangziwei/1a3949a6e53f50c63f75cd52c5bc634b to your computer and use it in GitHub Desktop.
some functions for roi detection
def load_h5_data(file_name):
with h5py.File(file_name,'r') as f:
return {key:f[key][:] for key in list(f.keys())}
def detect_soma_centroid(M):
"""
detect the coordinate of soma on stack image.
Return
======
array([X, Y, Z])
"""
from rivuletpy.soma import Soma
threshold = np.mean(M) + np.std(M)
bimg = (M > threshold).astype('int')
s = Soma()
s.detect(bimg, simple=True)
# print()
return s.centroid[0], s.centroid[1], s.centroid[2]
def pixel_size_rec(rec, verbose=False):
"""
Return the real length (in um) of each pixel point.
"""
len_rec_x_pixel = 64
len_rec_x_um = 71.5 / rec['wParamsNum'][30]
rec_pixel_size = len_rec_x_um / len_rec_x_pixel
if verbose:
print("the real length of each pixel in this recording is: \n{0} um".format(rec_pixel_size))
return rec_pixel_size
def pixel_size_stack(stack, verbose=False):
"""
Return the real length (in um) of each pixel point.
"""
len_stack_x_pixel = 512
len_stack_x_um = 71.5 / stack['wParamsNum'][30]
stack_pixel_size = len_stack_x_um / len_stack_x_pixel
if verbose:
print("the real length of each pixel in stack image is: \n{0} um".format(stack_pixel_size))
return stack_pixel_size
def get_scale_factor(rec, stack):
"""
get the scale factor from rec to stack,
e.g. scipy.misc.imresize(rec, size=scale_factor, interp='nearest')
would make the rec into the same scale as stack.
"""
rec_pixel_size = pixel_size_rec(rec)
stack_pixel_size = pixel_size_stack(stack)
return rec_pixel_size / stack_pixel_size
def resize_roi(rec, stack):
return scipy.misc.imresize(rec['ROIs'], size=get_scale_factor(rec, stack), interp='nearest')
def resize_rec(rec, stack):
reci = rec_preprop(rec)
return scipy.misc.imresize(reci, size=get_scale_factor(rec, stack), interp='nearest')
def rotate_rec(rec, stack):
ang_deg = rec['wParamsNum'][31] # ratoate angle (degree)
ang_rad = ang_deg * np.pi / 180 # ratoate angle (radian)
rec_rec = resize_rec(rec, stack)
rec_rot = scipy.ndimage.interpolation.rotate(rec_rec, ang_deg)
return rec_rot
def rec_preprop(rec):
reci = rec['wDataCh0'].mean(2)
reci[:4, :] = reci.mean() - 0.5*reci.std()
return reci
def rotate_roi(rec, stack):
ang_deg = rec['wParamsNum'][31] # ratoate angle (degree)
ang_rad = ang_deg * np.pi / 180 # ratoate angle (radian)
rec_rois = resize_roi(rec, stack)
rec_rois_rot = scipy.ndimage.interpolation.rotate(rec_rois, ang_deg)
(shift_x, shift_y) = 0.5 * (np.array(rec_rois_rot.shape) - np.array(rec_rois.shape))
(cx, cy) = 0.5 * np.array(rec_rois.shape)
labels = np.unique(rec_rois)[:-1]
px = [np.vstack(np.where(rec_rois == i)).T[:, 0].mean() for i in labels]
py = [np.vstack(np.where(rec_rois == i)).T[:, 1].mean() for i in labels]
px -= cx
py -= cy
xn = px * np.cos(ang_rad) - py*np.sin(ang_rad)
yn = px * np.sin(ang_rad) + py*np.cos(ang_rad)
xn += (cx + shift_x)
yn += (cy + shift_y)
return rec_rois_rot, np.vstack([xn, yn]).T
def rel_position_um(soma, d):
"""
Relative position between dendrites and soma in um.
Return
======
array([YCoord_um, XCoord_um, ZCoord_um])
"""
return soma['wParamsNum'][26:29] - d['wParamsNum'][26:29]
def roi_matching(image, template):
result = match_template(image, template)
ij = np.unravel_index(np.argmax(result), result.shape)
return np.array(ij)
def trace2linestack(df, shape=(512, 512, 70)):
x = [int(i) for i in df.x.tolist()]
y = [int(i) for i in df.y.tolist()]
z = [int(i) for i in df.z.tolist()]
coords = np.vstack((np.vstack((x,y)), z)).T
linestack = np.zeros([512, 512, 70])
for c in coords:
linestack[tuple(c)] = 1
return linestack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment