How to apply two polygon on two outlines

4 次查看(过去 30 天)
I have two images. The objective is to find the area of the approximation made by polygons in the contours of the images.
In the first image i drew a polygon and it gave me a satisfactory approximation of the contour (bellow) and thus I was able to obtain its area through the 'polyarea' function.
I would like to apply the same to the second image, where there are two outlines. I would like to obtain the area of the approximation made by two polygons in these two contours.
How could I change my code to achieve this effect?
Thanks in advance.
clc
clear
close all
skip = 320;
load('Image.mat');
image = preenc;
% Identify the contours and its areas
Contours = bwconncomp(preenc, 8);
area = regionprops(Contours, 'Area');
figure, imshow(preenc);
% Speeds up the use of boundary later
Bperimeter = bwperim(preenc);
Bperimeter = imdilate(Bperimeter,strel('square',4));
% Get x,y coordinates of perimeter (column index and row index,
% respectively)
[y,x] = find(Bperimeter);
k = boundary(x,y,1); %use boundary with shrink factor of 1 to find vertices
% Back to the initial binary image, add the polygon using recently
% obtained vertices
idx = [k(1:skip:end);k(1)];
drawpolygon('Position',[x(idx) y(idx)])
% Calculate the area of the polygon
d = polyarea(x(idx),y(idx));
disp(d)

采纳的回答

Ameer Hamza
Ameer Hamza 2020-4-6
Try this
clc
clear
close all
skip = 50;
load('image2.mat');
image = preenc;
% Identify the contours and its areas
Contours = bwconncomp(preenc, 8);
area = regionprops(Contours, 'Area');
figure, imshow(preenc);
for i=1:Contours.NumObjects
image_ = image;
% hide all other patches
mask = zeros(size(image));
mask(Contours.PixelIdxList{i}) = 1;
image_(~mask) = 0;
% Speeds up the use of boundary later
Bperimeter = bwperim(image_);
Bperimeter = imdilate(Bperimeter,strel('square',4));
% Get x,y coordinates of perimeter (column index and row index,
% respectively)
[y,x] = find(Bperimeter);
k = boundary(x,y,1); %use boundary with shrink factor of 1 to find vertices
% Back to the initial binary image, add the polygon using recently
% obtained vertices
idx = [k(1:skip:end);k(1)];
drawpolygon('Position',[x(idx) y(idx)])
% Calculate the area of the polygon
d = polyarea(x(idx),y(idx));
disp(d)
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by