o  io@sddlmZddlmZddlZddlmmZddlm Z ddl m Z ddl m Z mZmZddgZddddZddddZd ddZdd ddZdd ddZdS)!) annotations)SequenceN)Tensor)NdarrayOrTensor)convert_data_typeconvert_to_dst_typeensure_tuple_reperodedilate?maskr filter_sizeint | Sequence[int] pad_valuefloatreturncC2t|tj^}}t|||d}t||d^}}|S)a] Erode 2D/3D binary mask. Args: mask: input 2D/3D binary mask, [N,C,M,N] or [N,C,M,N,P] torch tensor or ndarray. filter_size: erosion filter size, has to be odd numbers, default to be 3. pad_value: the filled value for padding. We need to pad the input before filtering to keep the output with the same size as input. Usually use default value and not changed. Return: eroded mask, same shape and data type as input. Example: .. code-block:: python # define a naive mask mask = torch.zeros(3,2,3,3,3) mask[:,:,1,1,1] = 1.0 filter_size = 3 erode_result = erode(mask, filter_size) # expect torch.zeros(3,2,3,3,3) dilate_result = dilate(mask, filter_size) # expect torch.ones(3,2,3,3,3) rrsrcdst)rtorchrerode_trr rrmask_t_Z res_mask_tres_maskrj/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/transforms/utils_morphological_ops.pyr cCr)a\ Dilate 2D/3D binary mask. Args: mask: input 2D/3D binary mask, [N,C,M,N] or [N,C,M,N,P] torch tensor or ndarray. filter_size: dilation filter size, has to be odd numbers, default to be 3. pad_value: the filled value for padding. We need to pad the input before filtering to keep the output with the same size as input. Usually use default value and not changed. Return: dilated mask, same shape and data type as input. Example: .. code-block:: python # define a naive mask mask = torch.zeros(3,2,3,3,3) mask[:,:,1,1,1] = 1.0 filter_size = 3 erode_result = erode(mask,filter_size) # expect torch.zeros(3,2,3,3,3) dilate_result = dilate(mask,filter_size) # expect torch.ones(3,2,3,3,3) rr)rrrdilate_trrrrrr :r rrc Cst|jd}|dvrtd|d|jdt||}tdd|Dr-td|dt|jd |jd f||j}d d |D}t j | |d |d }|dkrYt j nt j }|||ddt|d}|S)a Apply a morphological filter to a 2D/3D binary mask tensor. Args: mask_t: input 2D/3D binary mask, [N,C,M,N] or [N,C,M,N,P] torch tensor. filter_size: morphological filter size, has to be odd numbers. pad_value: the filled value for padding. We need to pad the input before filtering to keep the output with the same size as input. Return: Tensor: Morphological filter result mask, same shape as input. )r#r z5spatial_dims must be either 2 or 3, got spatial_dims=z for mask tensor with shape of .css|] }|ddkVqdS)r#rNr).0sizerrr psz4get_morphological_filter_result_t..z7All dimensions in filter_size must be odd numbers, got cSs"g|] }tdD]}|dqqS)r#)range)r%r&rrrr ws"z5get_morphological_filter_result_t..constant)modevaluer)padding)r.)lenshape ValueErrorranyronestodeviceFpadrconv2dconv3dsum) rrr spatial_dimsZstructuring_elementpad_sizeZ input_paddedZconv_fnoutputrrr!get_morphological_filter_result_tZs$  &r>cCs,t|||}tt|ddkdd}|S)a  Erode 2D/3D binary mask with data type as torch tensor. Args: mask_t: input 2D/3D binary mask, [N,C,M,N] or [N,C,M,N,P] torch tensor. filter_size: erosion filter size, has to be odd numbers, default to be 3. pad_value: the filled value for padding. We need to pad the input before filtering to keep the output with the same size as input. Usually use default value and not changed. Return: Tensor: eroded mask, same shape as input. r gHz>r!)r>rwhereabsrrrr=rrrrs rcCs"t|||}t|dkdd}|S)a  Dilate 2D/3D binary mask with data type as torch tensor. Args: mask_t: input 2D/3D binary mask, [N,C,M,N] or [N,C,M,N,P] torch tensor. filter_size: dilation filter size, has to be odd numbers, default to be 3. pad_value: the filled value for padding. We need to pad the input before filtering to keep the output with the same size as input. Usually use default value and not changed. Return: Tensor: dilated mask, same shape as input. rr r!)r>rr?rArrrr"s r")r r )r rrrrrrr)r r!)rrrrrrrr) __future__rcollections.abcrrtorch.nn.functionalnn functionalr6r monai.configr monai.utilsrrr__all__r r r>rr"rrrrs     (