shade segmentation, opening by reconstruction, boundary tracing, polyshapes
Earlier this summer time, Cleve despatched me this image of a puzzle.
He requested for tips about turning the person “cheese slices” into patch objects for plotting and manipulation, maybe one thing like this:
In my submit right this moment, I am going to elaborate on this step.
rgb = imread(“Cheese_puzzle.png”);
% Threshold values chosen with the assistance of colorThresholder.
masks = ((30 <= L) & (L <= 98)) & …
((-20 <= a) & (a <= 16)) & …
imshow(masks)
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
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.