How to apply two polygon on two outlines

1 回表示 (過去 30 日間)
Jórdan Venâncio Leite
Jórdan Venâncio Leite 2020 年 4 月 5 日
コメント済み: Rena Berman 2020 年 10 月 12 日
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)
  1 件のコメント
Rena Berman
Rena Berman 2020 年 10 月 12 日
(Answers Dev) Restored edit

サインインしてコメントする。

採用された回答

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
  2 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 4 月 6 日
Mark, thanks for pointing to useful information.

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by