Perform erosion or dilation on a binary mask depending on the value of act. The operations utilize a defined kernel to affect neighboring pixels based on the specified val
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=I4), | intent(inout), | dimension(1:long,1:larg) | :: | msk |
2D mask |
|
integer(kind=I4), | intent(in) | :: | long |
2D mask length |
||
integer(kind=I4), | intent(in) | :: | larg |
2D mask height |
||
integer(kind=I4), | intent(in) | :: | val |
size of the structuring element for the erosion/dilation operation |
||
character(len=5), | intent(in) | :: | act |
action to be performed, either “erode” or another operation, presumably “dilate”. |
subroutine erode_dilate(msk, long, larg, val, act) !================================================================================================ !! Perform erosion or dilation on a binary mask depending on the value of act. !! The operations utilize a defined kernel to affect neighboring pixels based on the specified val !------------------------------------------------------------------------------------------------ implicit none integer(kind=I4), intent(in ) :: long !! *2D mask length* integer(kind=I4), intent(in ) :: larg !! *2D mask height* integer(kind=I4), intent(in ) :: val !! *size of the structuring element for the erosion/dilation operation* character(len=5), intent(in ) :: act !! *action to be performed, either "erode" or another operation, presumably "dilate".* integer(kind=I4), intent(inout), dimension(1:long,1:larg) :: msk !! *2D mask* integer(kind=I4) :: i, j, fin, ival integer(kind=I4), dimension(-val:val, -val:val) :: ker ! If the action specified is "erode", the mask is inverted (0s become 1s and 1s become 0s). This prepares the mask for the erosion operation. if (act =="erode") msk(1:long, 1:larg) = 1 - msk(1:long, 1:larg) fin = +2 ! The forall construct fills the kernel with the value fin (2) for all points (i, j) within a circular area defined by the radius val. ! The center of the kernel (ker(0, 0)) is explicitly set to 0. ker(-val:val, -val:val) = 0 forall (i = -val:+val, j = -val:+val, i**2 +j**2 <= val**2) ker(i, j) = fin ker(0, 0) = 0 do j = 1 +val, larg - val do i = 1 +val, long - val ! If the pixel value at position (i, j) in the mask is even, the loop skips any further processing for that pixel and continues to the next iteration. if (mod(msk(i, j), 2) == 0) cycle ! If the pixel value is odd, the corresponding area in the mask defined by the kernel is updated by adding the kernel values to it. ! This implements the dilation operation. msk(i - val:i + val, j - val:j + val) = msk(i - val:i + val, j - val:j + val) + ker(-val:val, -val:val) enddo enddo ! This line sets all elements of the mask that are greater than or equal to 1 to 1, effectively binarizing the mask after dilation. where(msk(1:long,1:larg)>=1) msk = 1 ! If the action specified is "erode", the mask is inverted again, restoring it to its original form after dilation. if (act=="erode") msk(1:long,1:larg) = 1 -msk(1:long,1:larg) return endsubroutine erode_dilate