Boundaries of an image (cell array) BACK to bw image

2 views (last 30 days)
Hi,
I take boundaries from an image using bwboundaries, which gives a cell array. I then convert the largest boundary to a matrix of outline points, and perform a refining process to get a 'better' boundary (using convhull, to get something more accurate to the real object I'm filming).
How do I then turn the new boundary BACK into a binary image?
The reason I want to do that is so that I can then perform an imerode for a further refined outline. This seems easy for a binary image, difficult for an array of outline points.
temp = regionprops(BW3,'centroid', 'BoundingBox', 'Area','Eccentricity'); % finds the centre, bounding box and the total area of the object in the image
B = bwboundaries(BW3); % finds the outline points of the object in the image
[~,I] = max([temp.Area]); % finds the index of the row with the largest area in the image
outline = B{I,1}; % converts the boundary to a numerical array
figure(1)
clf('reset')
imshow(BW3) %show binary image
hold on % keeps the current image on screen
plot(outline(:,2),outline(:,1),'r.-','LineWidth',1) % plots the outline of the object on top of the image
K = convhull(outline(:,2),outline(:,1)); % finds a convex hull K, to avoid long straight sections on the outline and false indents due to reflection
plot(outline(K,2),outline(K,1),'b.-','LineWidth',1) % plots the convex hull outline of the object on top of the image
Full code and test image attached, which ends up with a single-entry cell array waiting to become a new image :)

Answers (1)

Image Analyst
Image Analyst on 22 Dec 2020
Try this:
[rows, columns, numColors] = size(BW3);
mask = poly2mask(outline(:,2), outline(:,1), rows, columns);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!