Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# run adaptive multi-scale LoG filter
im_log_max, im_sigma_max = htk_shape_filters.cdog(
im_nuclei_stain, im_nuclei_fgnd_mask,
sigma_min=min_radius / np.sqrt(2),
sigma_max=max_radius / np.sqrt(2)
)
# apply local maximum clustering
im_nuclei_seg_mask, seeds, maxima = htk.segmentation.nuclear.max_clustering(
im_log_max, im_nuclei_fgnd_mask, local_max_search_radius)
if seeds is None:
return im_nuclei_seg_mask
# split any objects with disconnected fragments
im_nuclei_seg_mask = htk.segmentation.label.split(im_nuclei_seg_mask,
conn=8)
# filter out small objects
im_nuclei_seg_mask = htk.segmentation.label.area_open(
im_nuclei_seg_mask, min_nucleus_area).astype(np.int)
return im_nuclei_seg_mask
sigma_max=max_radius / np.sqrt(2)
)
# apply local maximum clustering
im_nuclei_seg_mask, seeds, maxima = htk.segmentation.nuclear.max_clustering(
im_log_max, im_nuclei_fgnd_mask, local_max_search_radius)
if seeds is None:
return im_nuclei_seg_mask
# split any objects with disconnected fragments
im_nuclei_seg_mask = htk.segmentation.label.split(im_nuclei_seg_mask,
conn=8)
# filter out small objects
im_nuclei_seg_mask = htk.segmentation.label.area_open(
im_nuclei_seg_mask, min_nucleus_area).astype(np.int)
return im_nuclei_seg_mask
X, Y, Min, Max = seed_contours(I, Delta)
# trace contours from seeds
cXs, cYs = trace_contours(I, X, Y, Min, Max, MaxLength=255)
# score successfully traced contours
Scores = score_contours(I, cXs, cYs)
# construct label image from scored contours
Label = label_contour(I.shape, cXs, cYs, Scores)
# compact contours to remove spurs - the paper calls this "optimization"
Label = label.compact(Label, Compaction)
# cleanup label image
Label = label.split(Label)
Label = label.area_open(Label, MinArea)
Label = label.width_open(Label, MinWidth)
# split objects with concavities
Label = split_concavities(Label, MinDepth, MinConcavity)
return Label
"""
# identify contour seed points
X, Y, Min, Max = seed_contours(I, Delta)
# trace contours from seeds
cXs, cYs = trace_contours(I, X, Y, Min, Max, MaxLength=255)
# score successfully traced contours
Scores = score_contours(I, cXs, cYs)
# construct label image from scored contours
Label = label_contour(I.shape, cXs, cYs, Scores)
# compact contours to remove spurs - the paper calls this "optimization"
Label = label.compact(Label, Compaction)
# cleanup label image
Label = label.split(Label)
Label = label.area_open(Label, MinArea)
Label = label.width_open(Label, MinWidth)
# split objects with concavities
Label = split_concavities(Label, MinDepth, MinConcavity)
return Label
def create_tile_nuclei_boundary_annotations(im_nuclei_seg_mask, tile_info):
nuclei_annot_list = []
gx = tile_info['gx']
gy = tile_info['gy']
wfrac = tile_info['gwidth'] / np.double(tile_info['width'])
hfrac = tile_info['gheight'] / np.double(tile_info['height'])
by, bx = htk_seg.label.trace_object_boundaries(im_nuclei_seg_mask,
trace_all=True)
for i in range(len(bx)):
# get boundary points and convert to base pixel space
num_points = len(bx[i])
if num_points < 3:
continue
cur_points = np.zeros((num_points, 3))
cur_points[:, 0] = np.round(gx + bx[i] * wfrac, 2)
cur_points[:, 1] = np.round(gy + by[i] * hfrac, 2)
cur_points = cur_points.tolist()
# create annotation json
if region_props[i].label in valid_superpixel:
min_row, max_row, min_col, max_col = \
get_boundary_bounds(region_props[i].bbox, 0, n_rows, n_cols)
# grab label mask
lmask = (
im_label[min_row:max_row, min_col:max_col] ==
region_props[i].label
).astype(np.bool)
mask = np.zeros(
(lmask.shape[0] + 2, lmask.shape[1] + 2), dtype=np.bool)
mask[1:-1, 1:-1] = lmask
# find boundaries
bx, by = htk_seg.label.trace_object_boundaries(mask)
bx = bx[0] + min_row
by = by[0] + min_col
with np.errstate(invalid='ignore'):
# remove redundant points
mby, mbx = htk_utils.merge_colinear(
by.astype(float), bx.astype(float))
# get superpixel boundary at highest-res
y_brs.append(mbx + top)
x_brs.append(mby + left)
cen_x, cen_y = region_props[i].centroid
# get superpixel centers at highest-res
y_cent.append(
round((cen_x + top), 1))
max(0, X[i]-np.ceil(MaxLength/2.0)):
min(I.shape[1]+1, X[i]+np.ceil(MaxLength/2.0))+1]
# binary threshold corresponding to seed pixel 'i'
W = (W <= Max[i]) & (W >= Min[i])
# embed with center pixel in middle of padded window
Embed = np.zeros((W.shape[0]+2, W.shape[1]+2), dtype=np.bool)
Embed[1:-1, 1:-1] = W
# calculate location of (X[i], Y[i]) in 'Embed'
pX = X[i] - max(0, X[i]-np.ceil(MaxLength/2.0)) + 1
pY = Y[i] - max(0, Y[i]-np.ceil(MaxLength/2.0)) + 1
# trace boundary, check stopping condition, append to list of contours
cX, cY = label.trace_object_boundaries(Embed, conn=4,
x_start=pX, y_start=pY,
MaxLength=MaxLength)
if cX[0][0] == cX[0][-1] and cY[0][0] == cY[0][-1] and\
len(cX[0]) <= MaxLength:
# add window offset to contour coordinates
cX[0] = [
x + max(0, X[i]-np.ceil(MaxLength/2.0)) - 1 for x in cX[0]
]
cY[0] = [
y + max(0, Y[i]-np.ceil(MaxLength/2.0)) - 1 for y in cY[0]
]
# append to list of candidate contours
cXs.append(np.array(cX[0], dtype=np.uint32))
cYs.append(np.array(cY[0], dtype=np.uint32))
def create_tile_nuclei_boundary_annotations(im_nuclei_seg_mask, tile_info):
nuclei_annot_list = []
gx = tile_info['gx']
gy = tile_info['gy']
wfrac = tile_info['gwidth'] / np.double(tile_info['width'])
hfrac = tile_info['gheight'] / np.double(tile_info['height'])
by, bx = htk_seg.label.trace_object_boundaries(im_nuclei_seg_mask,
trace_all=True)
for i in range(len(bx)):
# get boundary points and convert to base pixel space
num_points = len(bx[i])
if num_points < 3:
continue
cur_points = np.zeros((num_points, 3))
cur_points[:, 0] = np.round(gx + bx[i] * wfrac, 2)
cur_points[:, 1] = np.round(gy + by[i] * hfrac, 2)
cur_points = cur_points.tolist()
# create annotation json
def create_tile_nuclei_boundary_annotations(im_nuclei_seg_mask, tile_info):
nuclei_annot_list = []
gx = tile_info['gx']
gy = tile_info['gy']
wfrac = tile_info['gwidth'] / np.double(tile_info['width'])
hfrac = tile_info['gheight'] / np.double(tile_info['height'])
by, bx = htk_seg.label.trace_object_boundaries(im_nuclei_seg_mask,
trace_all=True)
for i in range(len(bx)):
# get boundary points and convert to base pixel space
num_points = len(bx[i])
if num_points < 3:
continue
cur_points = np.zeros((num_points, 3))
cur_points[:, 0] = np.round(gx + bx[i] * wfrac, 2)
cur_points[:, 1] = np.round(gy + by[i] * hfrac, 2)
cur_points = cur_points.tolist()
# create annotation json
im_nuclei_fgnd_mask = im_nuclei_stain < args.foreground_threshold
# segment nuclei
im_nuclei_seg_mask = htk_nuclear.detect_nuclei_kofahi(
im_nuclei_stain,
im_nuclei_fgnd_mask,
args.min_radius,
args.max_radius,
args.min_nucleus_area,
args.local_max_search_radius
)
# Delete border nuclei
if args.ignore_border_nuclei is True:
im_nuclei_seg_mask = htk_seg_label.delete_border(im_nuclei_seg_mask)
# generate nuclei annotations
nuclei_annot_list = []
flag_nuclei_found = np.any(im_nuclei_seg_mask)
if flag_nuclei_found:
nuclei_annot_list = cli_utils.create_tile_nuclei_annotations(
im_nuclei_seg_mask, tile_info, args.nuclei_annotation_format)
return nuclei_annot_list