Main Content

imkeepborder

Retain light structures connected to image border

Since R2023b

Description

example

J = imkeepborder(I) retains only the structures in the image I that are lighter than their surroundings and connected to the image border, while suppressing all other structures. If I is a binary image, then a structure is a connected group of white pixels. Use this function to keep the image border while clearing structures that are not touching the border. The output image J is grayscale or binary, depending on the input.

example

J = imkeepborder(I,Name=Value) specifies options for border structure selection using one or more name-value arguments. For example, imkeepborder(I,Borders=["left" "right"]) retains only the structures touching the left or right image border.

Examples

collapse all

Read a binary image (a postprocessed image of microscopic quartz columnar grains [2]) into the workspace, and display it.

originalBW = imread("quartz_columns.png");
imshow(originalBW)

Keep only the light objects in the image that are connected to the image border, removing the rest.

BWborder=imkeepborder(originalBW);
imshow(BWborder)

Read a binary image (a postprocessed image of microscopic quartz columnar grains [2]) into the workspace, and display it.

originalBW = imread("quartz_columns.png");
imshow(originalBW)

Keep only the objects which are connected to the top or bottom border of the image, and remove the rest.

BWkeep2B = imkeepborder(originalBW,Borders=["top" "bottom"]);
imshow(BWkeep2B)

Input Arguments

collapse all

Grayscale or binary image, specified as a numeric or logical array.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | logical

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: imkeepborder(I,Borders=["left" "right"]) retains light structures touching the left or right image border of an image.

Image borders at which to retain structures, specified as a vector of strings or an N-by-2 matrix of 0s and 1s:

  • Vector of strings — Specifies at which borders of a 2-D image to retain structures as any combination of "left", "right", "top", and "bottom". When you specify I as a 2-D image, the default value of Borders is ["left" "right" "top" "bottom"].

  • N-by-2 matrix of 0s and 1s — Specifies borders of an N-dimensional image at which to retain structures, where the first element of each row represents the first border in the corresponding dimension and the second element represents the second border in that dimension. For example, if Borders(k,1) is 1, then structures which touch the first border in the k-th dimension are selected. If Borders(k,2) is 1, then structures which touch the second border in the k-th dimension are selected. For example, specifying Borders = [0 0; 1 1; 0 0] is equivalent to specifying Borders = ["left" "right"]. The default value of Borders for N-dimensional images is ones(ndims(I),2), which specifies to retain structures touching all borders of the image.

Pixel connectivity, specified as one of the values in this table or a 3-by-3-by- ... -by-3 matrix of 0s and 1s. The default connectivity is 8 for 2-D images and 26 for 3-D images.

Value

Meaning

Two-Dimensional Connectivities

4

Pixels are connected if their edges touch. The neighborhood of a pixel are the adjacent pixels in the horizontal or vertical direction.

3-by-3 pixel neighborhood with four pixels connected to the center pixel

Current pixel is shown in gray.

8

Pixels are connected if their edges or corners touch. The neighborhood of a pixel are the adjacent pixels in the horizontal, vertical, or diagonal direction.

3-by-3 pixel neighborhood with 8 pixels connected to the center pixel

Current pixel is shown in gray.

Three-Dimensional Connectivities

6

Pixels are connected if their faces touch. The neighborhood of a pixel are the adjacent pixels in:

  • One of these directions: in, out, left, right, up, and down

3-by-3-by-3 pixel neighborhood with 6 pixels connected to the faces of the center pixel

Current pixel is shown in gray.

18

Pixels are connected if their faces or edges touch. The neighborhood of a pixel are the adjacent pixels in:

  • One of these directions: in, out, left, right, up, and down

  • A combination of two directions, such as right-down or in-up

3-by-3-by-3 pixel neighborhood with 6 pixels connected to the faces and 12 pixels connected to the edges of the center pixel

Current pixel is center of cube.

26

Pixels are connected if their faces, edges, or corners touch. The neighborhood of a pixel are the adjacent pixels in:

  • One of these directions: in, out, left, right, up, and down

  • A combination of two directions, such as right-down or in-up

  • A combination of three directions, such as in-right-up or in-left-down

3-by-3-by-3 pixel neighborhood with 6 pixels connected to the faces, 12 pixels connected to the edges, and 8 pixels connected to the corners of the center pixel

Current pixel is center of cube.

For higher dimensions, imkeepborder uses the default value conndef(ndims(I),'maximal').

Connectivity can also be defined in a more general way for any dimension by specifying a 3-by-3-by- ... -by-3 matrix of 0s and 1s. The 1-valued elements define neighborhood locations relative to the center element of conn. Note that conn must be symmetric about its center element. See Specifying Custom Connectivities for more information.

Data Types: double | logical

Output Arguments

collapse all

Processed grayscale or binary image, returned as numeric or logical array, depending on the input image you specify.

Algorithms

imkeepborder uses morphological reconstruction where:

  • The mask image is the input image.

  • The marker image is 0 everywhere except along the border, where it equals the mask image.

References

[1] Soille, Pierre. Morphological Image Analysis: Principles and Applications Berlin ; New York: Springer, 1999, 164–165.

[2] Molnar, Ian. Uniform quartz - Silver nanoparticle injection experiment, Digital Rocks Portal (April 2016). Accessed March 10, 2023. https://www.digitalrocksportal.org/projects/44, made available under the ODC-BY 1.0 Attribution License.

Extended Capabilities

Version History

Introduced in R2023b