當前位置: 首頁>>代碼示例>>Python>>正文


Python morphology.binary_fill_holes方法代碼示例

本文整理匯總了Python中scipy.ndimage.morphology.binary_fill_holes方法的典型用法代碼示例。如果您正苦於以下問題:Python morphology.binary_fill_holes方法的具體用法?Python morphology.binary_fill_holes怎麽用?Python morphology.binary_fill_holes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在scipy.ndimage.morphology的用法示例。


在下文中一共展示了morphology.binary_fill_holes方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: fill_holes_2d

# 需要導入模塊: from scipy.ndimage import morphology [as 別名]
# 或者: from scipy.ndimage.morphology import binary_fill_holes [as 別名]
def fill_holes_2d(z_slice):
    """
    Fill holes in the segmentation.
    :param z_slice: int 2d-array: Input 2D segmentation.
    :return: int 2d-array: Output segmentation with holes filled
    """
    assert z_slice.dtype == np.dtype('int')
    return binary_fill_holes(z_slice, structure=np.ones((3, 3))).astype(np.int) 
開發者ID:neuropoly,項目名稱:spinalcordtoolbox,代碼行數:10,代碼來源:postprocessing.py

示例2: refine_aseg

# 需要導入模塊: from scipy.ndimage import morphology [as 別名]
# 或者: from scipy.ndimage.morphology import binary_fill_holes [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) 
開發者ID:nipreps,項目名稱:niworkflows,代碼行數:30,代碼來源:freesurfer.py

示例3: fill_holes

# 需要導入模塊: from scipy.ndimage import morphology [as 別名]
# 或者: from scipy.ndimage.morphology import binary_fill_holes [as 別名]
def fill_holes(bin_img):
    """Flood fills holes in a binary mask

    Inputs:
    bin_img      = Binary image data

    Returns:
    filtered_img = image with objects filled

    :param bin_img: numpy.ndarray
    :return filtered_img: numpy.ndarray
    """
    params.device += 1

    # Make sure the image is binary
    if len(np.shape(bin_img)) != 2 or len(np.unique(bin_img)) != 2:
        fatal_error("Image is not binary")

    # Cast binary image to boolean
    bool_img = bin_img.astype(bool)

    # Flood fill holes
    bool_img = binary_fill_holes(bool_img)

    # Cast boolean image to binary and make a copy of the binary image for returning
    filtered_img = np.copy(bool_img.astype(np.uint8) * 255)

    if params.debug == 'print':
        print_image(filtered_img, os.path.join(params.debug_outdir, str(params.device) + '_fill_holes' + '.png'))
    elif params.debug == 'plot':
        plot_image(filtered_img, cmap='gray')

    return filtered_img 
開發者ID:danforthcenter,項目名稱:plantcv,代碼行數:35,代碼來源:fill_holes.py

示例4: mask_image

# 需要導入模塊: from scipy.ndimage import morphology [as 別名]
# 或者: from scipy.ndimage.morphology import binary_fill_holes [as 別名]
def mask_image(input_img,
               update_factor=0.5,
               init_percentile=2,
               iterations_closing=5,
               return_inverse=False,
               out_dtype=None):
    """
    Estimates the foreground mask for a given image.
    Similar to 3dAutoMask from AFNI.


    iterations_closing : int
        Number of iterations of binary_closing to apply at the end.

    """

    prev_clip_level = np.percentile(input_img, init_percentile)
    while True:
        mask_img = input_img >= prev_clip_level
        cur_clip_level = update_factor * np.median(input_img[mask_img])
        if np.isclose(cur_clip_level, prev_clip_level, rtol=0.05):
            break
        else:
            prev_clip_level = cur_clip_level

    if len(input_img.shape) == 3:
        se = ndimage.generate_binary_structure(3, 6)
    elif len(input_img.shape) == 2:
        se = ndimage.generate_binary_structure(2, 4)
    else:
        raise ValueError('Image must be 2D or 3D')

    mask_img = binary_closing(mask_img, se, iterations=iterations_closing)
    mask_img = binary_fill_holes(mask_img, se)

    if return_inverse:
        mask_img = np.logical_not(mask_img)

    if out_dtype is not None:
        mask_img = mask_img.astype(out_dtype)

    return mask_img

# alias 
開發者ID:raamana,項目名稱:visualqc,代碼行數:46,代碼來源:image_utils.py

示例5: _run_interface

# 需要導入模塊: from scipy.ndimage import morphology [as 別名]
# 或者: from scipy.ndimage.morphology import binary_fill_holes [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 
開發者ID:nipreps,項目名稱:niworkflows,代碼行數:48,代碼來源:nilearn.py


注:本文中的scipy.ndimage.morphology.binary_fill_holes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。