Friday, April 12, 2024
HomeMatlabFiguring out Border-Touching Objects Utilizing imclearborder or regionprops » Steve on Picture...

# Figuring out Border-Touching Objects Utilizing imclearborder or regionprops » Steve on Picture Processing with MATLAB

I’ve seen some requests and questions associated to figuring out objects in a binary picture which can be touching the picture border. Typically the query pertains to the usage of imclearborder, and generally the query is about regionprops. Right now, I am going to present you methods to sort out the issue each methods.

## Utilizing imclearborder

I will be utilizing this binary model of the rice.png pattern picture from the Picture Processing Toolbox.

url = “https://blogs.mathworks.com/steve/recordsdata/rice-bw-1.png”;

imshow(A)

The perform imclearborder removes all objects touching the border.

imshow(B)

That appears to be the alternative of what we would like. We will, nonetheless, convert this into the specified end result by utilizing the MATLAB element-wise logical operators, equivalent to & (and), | (or), and ~ (not). In phrases, we would like the foreground pixels which can be in A and should not in B. As a MATLAB expression, it appears to be like like this:

imshow(C)

## Utilizing regionprops

The perform regionprops can compute all types of properties of binary picture objects. Right here is an easy instance that computes the realm and centroid of every object in our pattern picture. I am utilizing the type of regionprops that returns a desk.

props = regionprops(“desk”,A,[“Area” “Centroid”])

My approach for locating border-touching objects with regionprops makes use of the BoundingBox property, so embody that property together with another properties that you simply need to measure.

props = regionprops(“desk”,A,[“Area” “Centroid” “BoundingBox”])

For any specific object, BoundingBox is a four-element vector containing the left, prime, width, and top of the bounding field. For instance, right here is the bounding field of the twentieth object:

props.BoundingBox(20,:)

By evaluating these values to the dimensions of the picture, we will determine which objects contact the picture border.

Begin by figuring out the objects that contact particular borders.

left_coordinate = props.BoundingBox(:,1);

props.TouchesLeftBorder = (left_coordinate == 0.5);

top_coordinate = props.BoundingBox(:,2);

props.TouchesTopBorder = (top_coordinate == 0.5);

right_coordinate = left_coordinate + props.BoundingBox(:,3);

bottom_coordinate = top_coordinate + props.BoundingBox(:,4);

props.TouchesRightBorder = (right_coordinate == (N+0.5));

props.TouchesBottomBorder = (bottom_coordinate == (M+0.5))

Lastly, compute whether or not objects contact any border utilizing |, the element-wise OR operator.

props.TouchesAnyBorder = props.TouchesLeftBorder |

props.TouchesTopBorder |

props.TouchesRightBorder |

props.TouchesBottomBorder

I am going to end with a fast visible sanity examine of the outcomes.

L_touches_border = ismember(L,discover(props.TouchesAnyBorder));

imshowpair(A,L_touches_border)

RELATED ARTICLES