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
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 个评论
Image Analyst
2020-4-6
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!