Main Content

imclearborder

Suppress light structures connected to image border

Description

J = imclearborder(I) suppresses structures in the image I that are lighter than their surroundings and connected to the image border. Use this function to clear the image border or select image borders. For grayscale images, imclearborder tends to reduce the overall intensity level in addition to suppressing border structures. The output image J is grayscale or binary, depending on the input.

example

J = imclearborder(I,conn) specifies the pixel connectivity, conn.

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

example

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)

Figure contains an axes object. The hidden axes object contains an object of type image.

Clear all light objects in the image that are connected to the image border, and display the adjusted image.

BWclearB = imclearborder(originalBW);
imshow(BWclearB)

Figure contains an axes object. The hidden axes object contains an object of type image.

Read a grayscale image containing an object with a dark border into the workspace, and display it.

I = imread("logo.png");
imshow(I)

Figure contains an axes object. The hidden axes object contains an object of type image.

Complement the image and remove the border. Display the image.

J = imcomplement(I);
JNoBorder = imclearborder(J);
imshow(JNoBorder)

Figure contains an axes object. The hidden axes object contains an object of type image.

Complement the image again to return it to the original input image contrast.

INoBorder = imcomplement(JNoBorder);
imshow(INoBorder)

Figure contains an axes object. The hidden axes object contains an object of type image.

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)

Figure contains an axes object. The hidden axes object contains an object of type image.

Remove only the objects which are connected to the top or bottom border of the image.

BWclear2B = imclearborder(originalBW, Borders=["top" "bottom"]);
imshow(BWclear2B)

Figure contains an axes object. The hidden axes object contains an object of type image.

Create a simple binary image.

BW = [0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0
      1     0     0     1     1     1     0     0     0
      0     1     0     1     1     1     0     0     0
      0     0     0     1     1     1     0     0     0
      0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0
      0     0     0     0     0     0     0     0     0];

Clear pixels on the border of the image using 4-connectivity. Note that imclearborder does not clear the pixel at (5,2) because, with 4-connectivity, it is not considered connected to the border pixel at (4,1).

BWc1 = imclearborder(BW,Connectivity=4)
BWc1 = 9×9

     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0
     0     0     0     1     1     1     0     0     0
     0     1     0     1     1     1     0     0     0
     0     0     0     1     1     1     0     0     0
     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0

Now clear pixels on the border of the image using 8-connectivity. imclearborder clears the pixel at (5,2) because, with 8-connectivity, it is considered connected to the border pixel (4,1).

BWc2 = imclearborder(BW,Connectivity=8)
BWc2 = 9×9

     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0
     0     0     0     1     1     1     0     0     0
     0     0     0     1     1     1     0     0     0
     0     0     0     1     1     1     0     0     0
     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0

Input Arguments

collapse all

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

Example: I = imread('pout.tif');

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

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, imclearborder 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.

Note

A pixel on the edge of the input image might not be considered to be a border pixel if you specify a nondefault connectivity. For example, if conn = [0 0 0; 1 1 1; 0 0 0], elements on the first and last row are not considered to be border pixels because, according to that connectivity definition, they are not connected to the region outside the image.

Note

If you specify both the conn argument and the Connectivity name-value argument, then imclearborder sets the connectivity according to Connectivity and ignores the value of conn.

Data Types: double | 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: imclearborder(I,Borders=["left" "right"]) removes light structures touching the left or right border of an image.

Since R2023b

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

  • Vector of strings — Specifies which borders of a 2-D image to remove structures from 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 which borders of an N-dimensional image to remove structures from, 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 remove structures touching all borders of the image.

Since R2023b

Pixel connectivity, specified as 4, 8, 6, 18, 26, or a 3-by-3-by- ... -by-3 matrix of 0s and 1s. For more information, see conn.

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

imclearborder 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 for documentation use under the ODC-BY 1.0 Attribution License.

Extended Capabilities

Version History

Introduced before R2006a

expand all