How to apply two polygon on two outlines
4 views (last 30 days)
Show older comments
Jórdan Venâncio Leite
on 5 Apr 2020
Commented: Rena Berman
on 12 Oct 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)
Accepted Answer
Ameer Hamza
on 6 Apr 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
2 Comments
Image Analyst
on 6 Apr 2020
image is a built-in function so you shouldn't use image as the name of a variable.
Also, see these links, some of which might be interesting:
and see attached article.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!