Friday, April 26, 2024
HomeMatlabObjects and Polygonal Boundaries » Steve on Picture Processing with MATLAB

Objects and Polygonal Boundaries » Steve on Picture Processing with MATLAB


In a few of my current perusal of picture processing questions on MATLAB Solutions, I’ve come throughout a number of questions involving binary picture objects and polygonal boundaries, like this.

binary-objects-polygonal-boundaries.png

The questions differ however are comparable:

  • How can I decide which objects contact the polygon boundary?
  • How can I do away with objects outdoors the boundary?
  • How can I do away with objects outdoors or touching the boundary?
  • How can I do away with objects inside a sure distance of the boundary?

As we speak I need to present you methods to reply all these questions and extra moreover, utilizing these elementary operations:

(Or, as an alternative choice to drawpolygon, pictures.roi.Polygon, and createMask, you should use roipoly and poly2mask.)

Right here is the picture I will be utilizing at the moment:

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

imshow(A)

And right here is my polygon.

I will be drawing it utilizing a helper operate, showpoly, which is on the finish of this put up. The helper operate makes use of drawpolygon, like this:

p = drawpolygon(Place = xy, FaceAlpha = 0.5)

Be aware that drawpolygon creates an pictures.roi.Polygon (or simply Polygon). The Polygon object is certainly one of a wide range of region-of-interest (ROI) objects within the Picture Processing Toolbox. You may customise many points of its look and conduct, and it’s helpful for implementing apps that work with ROIs.

Of specific curiosity for at the moment’s matter is the Polygon operate referred to as createMask. This operate returns a binary picture with white pixels contained in the polygon.

imshow(masks)

Objects utterly or partially contained in the polygon

Now we are able to begin answering the object-polygon questions. First up: which objects are contained in the polygon, both utterly or partially?

One of many issues that makes MATLAB enjoyable for picture processing is utilizing logical operators with binary pictures. Under, I compute the logical AND of the unique binary picture with the polygon masks picture.

showpoly

Subsequent, I apply morphological reconstruction utilizing imreconstruct. This operate takes a marker picture and expands its objects outward, whereas constraining the growth to incorporate solely object pixels within the second enter. This can be a handy strategy to convert partial objects again into their full, authentic shapes. The outcome, C, is a picture containing objects which might be utterly or partially throughout the polygon.

showpoly

Objects touching the perimeter of the polygon

Subsequent query: which objects contact the sting, or perimeter, of the polygon? I am going to begin with bwperim on the polygon masks to compute a fringe masks.

perimeter_mask = bwperim(masks);

imshow(perimeter_mask)

Going again to the logical & operator provides us the perimeter pixels which might be a part of any of the objects.

imshow(D)

Morphological reconstruct is helpful once more right here, to “reconstruct” the complete shapes of objects from the article pixels that lie alongside the polygon perimeter.

showpoly();

Objects outdoors the polygon

Third query: which objects lie utterly outdoors the polygon? Recall that picture C, computed above, is the objects which might be utterly or partially contained in the polygon. The objects that we wish, then, are the objects which might be in A however not in C. You may compute this utilizing A & ~C or setdiff(A,C).

objects_outside = A & ~C;

showpoly();

What concerning the object close to the higher proper polygon vertex? Is it actually utterly outdoors the polygon?

axis([215 230 45 60])

It seems like a sliver of white is contained in the polygon. Why is that?

I am going to clarify by exhibiting the polygon masks, the surface objects, the polygon itself, and the pixel grid. I am going to use imshowpair to each the polygon masks and the outside-objects masks. (The operate pixelgrid is on the File Change.)

imshowpair(objects_outside,masks)

pixelgrid

When a polygon solely partially covers a pixel, the createMask has sure tie-breaking guidelines to find out whether or not a pixel is inside or outdoors. You may see the tie-breaking guidelines play out in a number of pixels above.

Objects inside and at the very least 20 pixels away the polygon boundary

The fourth query is which objects will not be solely contained in the polygon, however at the very least 20 pixels away from the polygon boundary? To reply this query, I am going to usher in one other software: the distance remodel, as computed by bwdist.

First, although, let’s begin by making use of the logical NOT (or complement) operator, ~. Making use of it to the polygon masks picture provides us a masks exhibiting the pixels which might be outdoors the polygon. I am going to flip the axis field on so you’ll be able to see the extent of the outside masks.

axis on

The gap remodel computes, for each picture pixel, the space between that pixel and the closest foreground pixel. If a pixel is itself a foreground pixel, then the space remodel at that pixel is 0. By computing the space remodel of the outside masks, we are able to learn how far each pixel is from the closest pixel within the polygon exterior.

G = bwdist(exterior_mask);

(Various strategy: contemplate the buffer technique of polyshape object.)

When displaying a distance remodel as a picture, the autoranging syntax of imshow is helpful. Simply specify [] because the second argument when displaying depth values, and the minimal worth will get robotically scaled to black, and the utmost depth worth will get robotically scaled to white.

imshow(G,[])

Now let’s compute a masks of pixels at the very least 20 pixel items away from the outside masks by utilizing a relational operator, >=.

interior_mask_with_buffer = (G >= 20);

imshow(interior_mask_with_buffer)

showpoly();

Utilizing the methods described above, compute a picture containing the objects which might be utterly outdoors the inside buffered masks.

H = imreconstruct(A & ~interior_mask_with_buffer, A);

imshow(H)

Subsequent, discover the objects which might be in A however not in H.

imshowpair(I,interior_mask_with_buffer)

showpoly();

Objects not inside 20 pixels of the polygon border

The ultimate query for at the moment is which objects will not be inside 20 pixels of the polygon border. Begin by computing the space remodel of the perimeter masks.

J = bwdist(perimeter_mask);

imshow(J,[])

Use a relational operator to compute a buffered perimeter masks.

perimeter_mask_with_buffer = (J <= 20);

imshow(perimeter_mask_with_buffer)

Use the methods described above to search out all the objects which might be competely or partially throughout the buffered perimeter masks area.

Ok = A & perimeter_mask_with_buffer;

imshow(L)

The ultimate reply is the objects which might be within the authentic picture, A, however not in L.

imshowpair(M,perimeter_mask_with_buffer)

showpoly();

This put up demonstrated plenty of MATLAB and toolbox capabilities and operators that work nicely collectively:

  • createMask (or poly2mask) to create a binary “masks” picture from polygons
  • Logical operators on binary pictures to compute issues like “on this picture or masks however not on this different one”
  • imreconstruct to reconstruct authentic binary picture shapes from partial ones
  • bwdist to compute the space remodel, which tells you ways far-off each picture pixel is from some binary picture masks
  • Relational operators to kind masks from distance remodel pictures

Have enjoyable with these.

And Completely happy New Yr!

drawpolygon(Place = xy, FaceAlpha = 0.5);

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments