Thursday, March 28, 2024
HomeMatlabPuzzle Items and Polyshapes » Steve on Picture Processing with MATLAB

Puzzle Items and Polyshapes » Steve on Picture Processing with MATLAB


shade segmentation, opening by reconstruction, boundary tracing, polyshapes

Earlier this summer time, Cleve despatched me this image of a puzzle.

Cheese_puzzle.png

He requested for tips about turning the person “cheese slices” into patch objects for plotting and manipulation, maybe one thing like this:

colored-puzzle-pieces.png

In my submit right this moment, I am going to elaborate on this step.

The very first thing that occurred to me was to make use of shade to phase the puzzle items from the remainder of the picture. I used the Colour Thresholder app to find out some threshold values in CIELAB house.
color-thresholder-screenshot.png

rgb = imread(“Cheese_puzzle.png”);

% Threshold values chosen with the assistance of colorThresholder.

masks = ((30 <= L) & (L <= 98)) &

((-20 <= a) & (a <= 16)) &

imshow(masks)

Subsequent, we have to do away with the additional undesirable foreground pixels. I am going to use a way that the mathematical morphology people name opening by reconstruction. Step one is to erode the picture in a method that eliminates all of the undesirable pixels, whereas sustaining a minimum of a portion of all of the objects we wish to preserve. An erosion by a vertical line will do away with the skinny horizontal strains, and an erosion by a horizontal line will do away with the skinny vertical strains.

mask2 = imerode(masks,ones(21,1));

imshow(mask2)

mask3 = imerode(mask2,ones(1,21));

imshow(mask3)

Within the picture above, the extraneous foreground pixels are gone, however the puzzle items have been shrunk. I am going to use morphological reconstruction to get well the complete puzzle items.

mask4 = imreconstruct(mask3,masks);

imshow(mask4)

Nice. Now, how can we flip these puzzle items into some type of simply plottable representations? Cleve had steered patch objects, however it’s sophisticated to create patch objects containing holes. I steered utilizing polyshape as a substitute. A polyshape is an object that may signify shapes which can be composed of polygons. You possibly can create a polyshape from a group of polygons that sure particular person areas and holes, and the Picture Processing Toolbox operate bwboundaries can produce simply such a listing of polygons.

b = bwboundaries(mask4)

The polyshape operate can take a group of bounding polygons and routinely determine which polygons sure areas and which sure holes, however the type of the polyshape enter arguments is a bit completely different from what bwboundaries produces. Right here is a few code to transform the bwboundaries output into one thing that polyshape can deal with.

ps = polyshape(X,Y)

Nicely, that is a bit messy. In my picture processing work, I typically see this warning message from polyshape. Normally it’s as a result of I am passing a bunch of colinear vertices to polyshape. They’re colinear as a result of the vertices are on the picture pixel grid. I typically ignore the warning.

There’s one other challenge, although. Why does the output of polyshape say it has 6 areas as a substitute of 5? I needed to search a bit for the rationale. We are able to see it by zooming into one of many corners of the decrease proper puzzle piece and superimposing the boundary traced by bwboundaries.

plot(b{5}(:,2),b{5}(:,1),‘b’)

ylim([990 1010])

There is a spot the place the traced boundary is self-intersecting, and that causes polyshape to deal with that tiny triangle on the backside as a separate area. We are able to do some area-based processing of the polyshape to do away with that triangle.

Cut up the polyshape into separate areas:

ps_regions = areas(ps)

Discover the realm of every area:

region_areas = space(ps_regions)

And do away with the tiny area:

ps_regions(region_areas < 1) = []

Now we are able to plot our puzzle items. For those who move an array of polyshapes to plot, it can shade every one individually.

axis equal

I really like polyshapes. They arrive with a wealthy assortment of helpful features (see “Object Capabilities” on the polyshape reference web page.) You possibly can modify them, take them aside, be part of them utilizing Boolean operations, measure them, question level places, and many others.

For instance, the polybuffer operate can develop (or shrink) a polyshape primarily based on the thought of making a “buffer zone” across the form. As an example, let’s take one of many puzzle items and provides it a 50-pixel buffer zone.

ps5_50 = polybuffer(ps5,50);

maintain off

Are polyshapes helpful in your work? I might like to listen to about it. Please depart a remark.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments