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

2 ビュー (過去 30 日間)
Frederick Wells
Frederick Wells 2020 年 5 月 19 日
回答済み: Image Analyst 2020 年 12 月 22 日
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 :)

回答 (1 件)

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

カテゴリ

Help Center および File ExchangeComputer Vision with Simulink についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by