本文整理汇总了Python中skimage.morphology.binary_closing方法的典型用法代码示例。如果您正苦于以下问题:Python morphology.binary_closing方法的具体用法?Python morphology.binary_closing怎么用?Python morphology.binary_closing使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类skimage.morphology
的用法示例。
在下文中一共展示了morphology.binary_closing方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compute_binary_mask_lasseck
# 需要导入模块: from skimage import morphology [as 别名]
# 或者: from skimage.morphology import binary_closing [as 别名]
def compute_binary_mask_lasseck(spectrogram, threshold):
# normalize to [0, 1)
norm_spectrogram = normalize(spectrogram)
# median clipping
binary_image = median_clipping(norm_spectrogram, threshold)
# closing binary image (dilation followed by erosion)
binary_image = morphology.binary_closing(binary_image, selem=np.ones((4, 4)))
# dialate binary image
binary_image = morphology.binary_dilation(binary_image, selem=np.ones((4, 4)))
# apply median filter
binary_image = filters.median(binary_image, selem=np.ones((2, 2)))
# remove small objects
binary_image = morphology.remove_small_objects(binary_image, min_size=32, connectivity=1)
mask = np.array([np.max(col) for col in binary_image.T])
mask = smooth_mask(mask)
return mask
# TODO: This method needs some real testing
示例2: clean_mask
# 需要导入模块: from skimage import morphology [as 别名]
# 或者: from skimage.morphology import binary_closing [as 别名]
def clean_mask(m, c):
# threshold
m_thresh = threshold_otsu(m)
c_thresh = threshold_otsu(c)
m_b = m > m_thresh
c_b = c > c_thresh
# combine contours and masks and fill the cells
m_ = np.where(m_b | c_b, 1, 0)
m_ = ndi.binary_fill_holes(m_)
# close what wasn't closed before
area, radius = mean_blob_size(m_b)
struct_size = int(1.25 * radius)
struct_el = morph.disk(struct_size)
m_padded = pad_mask(m_, pad=struct_size)
m_padded = morph.binary_closing(m_padded, selem=struct_el)
m_ = crop_mask(m_padded, crop=struct_size)
# open to cut the real cells from the artifacts
area, radius = mean_blob_size(m_b)
struct_size = int(0.75 * radius)
struct_el = morph.disk(struct_size)
m_ = np.where(c_b & (~m_b), 0, m_)
m_padded = pad_mask(m_, pad=struct_size)
m_padded = morph.binary_opening(m_padded, selem=struct_el)
m_ = crop_mask(m_padded, crop=struct_size)
# join the connected cells with what we had at the beginning
m_ = np.where(m_b | m_, 1, 0)
m_ = ndi.binary_fill_holes(m_)
# drop all the cells that weren't present at least in 25% of area in the initial mask
m_ = drop_artifacts(m_, m_b, min_coverage=0.25)
return m_
示例3: refine_aseg
# 需要导入模块: from skimage import morphology [as 别名]
# 或者: from skimage.morphology import binary_closing [as 别名]
def refine_aseg(aseg, ball_size=4):
"""
Refine the ``aseg.mgz`` mask of Freesurfer.
First step to reconcile ANTs' and FreeSurfer's brain masks.
Here, the ``aseg.mgz`` mask from FreeSurfer is refined in two
steps, using binary morphological operations:
1. With a binary closing operation the sulci are included
into the mask. This results in a smoother brain mask
that does not exclude deep, wide sulci.
2. Fill any holes (typically, there could be a hole next to
the pineal gland and the corpora quadrigemina if the great
cerebral brain is segmented out).
"""
# Read aseg data
bmask = aseg.copy()
bmask[bmask > 0] = 1
bmask = bmask.astype(np.uint8)
# Morphological operations
selem = sim.ball(ball_size)
newmask = sim.binary_closing(bmask, selem)
newmask = binary_fill_holes(newmask.astype(np.uint8), selem).astype(np.uint8)
return newmask.astype(np.uint8)
示例4: segment_watershed
# 需要导入模块: from skimage import morphology [as 别名]
# 或者: from skimage.morphology import binary_closing [as 别名]
def segment_watershed(seg, centers, post_morph=False):
""" perform watershed segmentation on input imsegm
and optionally run some postprocessing using morphological operations
:param ndarray seg: input image / segmentation
:param [[int, int]] centers: position of centres / seeds
:param bool post_morph: apply morphological postprocessing
:return ndarray, [[int, int]]: resulting segmentation, updated centres
"""
logging.debug('segment: watershed...')
seg_binary = (seg > 0)
seg_binary = ndimage.morphology.binary_fill_holes(seg_binary)
# thr_area = int(0.05 * np.sum(seg_binary))
# seg_binary = morphology.remove_small_holes(seg_binary, min_size=thr_area)
distance = ndimage.distance_transform_edt(seg_binary)
markers = np.zeros_like(seg)
for i, pos in enumerate(centers):
markers[int(pos[0]), int(pos[1])] = i + 1
segm = morphology.watershed(-distance, markers, mask=seg_binary)
# if morphological postprocessing was not selected, ends here
if not post_morph:
return segm, centers, None
segm_clean = np.zeros_like(segm)
for lb in range(1, np.max(segm) + 1):
seg_lb = (segm == lb)
# some morphology operartion for cleaning
seg_lb = morphology.binary_closing(seg_lb, selem=morphology.disk(5))
seg_lb = ndimage.morphology.binary_fill_holes(seg_lb)
# thr_area = int(0.15 * np.sum(seg_lb))
# seg_lb = morphology.remove_small_holes(seg_lb, min_size=thr_area)
seg_lb = morphology.binary_opening(seg_lb, selem=morphology.disk(15))
segm_clean[seg_lb] = lb
return segm_clean, centers, None
示例5: closing
# 需要导入模块: from skimage import morphology [as 别名]
# 或者: from skimage.morphology import binary_closing [as 别名]
def closing(gray_img, kernel=None):
"""Wrapper for scikit-image closing functions. Opening can remove small dark spots (i.e. pepper).
Inputs:
gray_img = input image (grayscale or binary)
kernel = optional neighborhood, expressed as an array of 1s and 0s. If None, use cross-shaped structuring element.
:param gray_img: ndarray
:param kernel = ndarray
:return filtered_img: ndarray
"""
params.device += 1
# Make sure the image is binary/grayscale
if len(np.shape(gray_img)) != 2:
fatal_error("Input image must be grayscale or binary")
# If image is binary use the faster method
if len(np.unique(gray_img)) == 2:
bool_img = morphology.binary_closing(image=gray_img, selem=kernel)
filtered_img = np.copy(bool_img.astype(np.uint8) * 255)
# Otherwise use method appropriate for grayscale images
else:
filtered_img = morphology.closing(gray_img, kernel)
if params.debug == 'print':
print_image(filtered_img, os.path.join(params.debug_outdir, str(params.device) + '_opening' + '.png'))
elif params.debug == 'plot':
plot_image(filtered_img, cmap='gray')
return filtered_img
示例6: get_segmented_lungs
# 需要导入模块: from skimage import morphology [as 别名]
# 或者: from skimage.morphology import binary_closing [as 别名]
def get_segmented_lungs(im, plot=False):
# Step 1: Convert into a binary image.
binary = im < -400
# Step 2: Remove the blobs connected to the border of the image.
cleared = clear_border(binary)
# Step 3: Label the image.
label_image = label(cleared)
# Step 4: Keep the labels with 2 largest areas.
areas = [r.area for r in regionprops(label_image)]
areas.sort()
if len(areas) > 2:
for region in regionprops(label_image):
if region.area < areas[-2]:
for coordinates in region.coords:
label_image[coordinates[0], coordinates[1]] = 0
binary = label_image > 0
# Step 5: Erosion operation with a disk of radius 2. This operation is seperate the lung nodules attached to the blood vessels.
selem = disk(2)
binary = binary_erosion(binary, selem)
# Step 6: Closure operation with a disk of radius 10. This operation is to keep nodules attached to the lung wall.
selem = disk(10) # CHANGE BACK TO 10
binary = binary_closing(binary, selem)
# Step 7: Fill in the small holes inside the binary mask of lungs.
edges = roberts(binary)
binary = ndi.binary_fill_holes(edges)
# Step 8: Superimpose the binary mask on the input image.
get_high_vals = binary == 0
im[get_high_vals] = -2000
return im, binary
示例7: _run_interface
# 需要导入模块: from skimage import morphology [as 别名]
# 或者: from skimage.morphology import binary_closing [as 别名]
def _run_interface(self, runtime):
in_files = self.inputs.in_files
if self.inputs.enhance_t2:
in_files = [_enhance_t2_contrast(f, newpath=runtime.cwd) for f in in_files]
masknii = compute_epi_mask(
in_files,
lower_cutoff=self.inputs.lower_cutoff,
upper_cutoff=self.inputs.upper_cutoff,
connected=self.inputs.connected,
opening=self.inputs.opening,
exclude_zeros=self.inputs.exclude_zeros,
ensure_finite=self.inputs.ensure_finite,
target_affine=self.inputs.target_affine,
target_shape=self.inputs.target_shape,
)
if self.inputs.closing:
closed = sim.binary_closing(
np.asanyarray(masknii.dataobj).astype(np.uint8), sim.ball(1)
).astype(np.uint8)
masknii = masknii.__class__(closed, masknii.affine, masknii.header)
if self.inputs.fill_holes:
filled = binary_fill_holes(
np.asanyarray(masknii.dataobj).astype(np.uint8), sim.ball(6)
).astype(np.uint8)
masknii = masknii.__class__(filled, masknii.affine, masknii.header)
if self.inputs.no_sanitize:
in_file = self.inputs.in_files
if isinstance(in_file, list):
in_file = in_file[0]
nii = nb.load(in_file)
qform, code = nii.get_qform(coded=True)
masknii.set_qform(qform, int(code))
sform, code = nii.get_sform(coded=True)
masknii.set_sform(sform, int(code))
self._results["out_mask"] = fname_presuffix(
self.inputs.in_files[0], suffix="_mask", newpath=runtime.cwd
)
masknii.to_filename(self._results["out_mask"])
return runtime