How to apply two polygon on two outlines

1 visualización (últimos 30 días)
Jórdan Venâncio Leite
Jórdan Venâncio Leite el 5 de Abr. de 2020
Comentada: Rena Berman el 12 de Oct. de 2020
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)

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 6 de Abr. de 2020
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

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by