Smallest mask enclosing a polygon
이전 댓글 표시
I want to find the smallest mask that contains a given polygon (for example the shape of a country).
X = longitude;
Y = latitude;
[Y,X] = meshgrid(Y,X);
shape = some_polygon;
mask = zeros(size(X));
for i =1:size(X,1)
for j = 1:size(X,2)
if inpolygon(X(i,j),Y(i,j),shape(1,:),shape(2,:))
mask(i,j)=1;
end
end
end
X and Y are lon/lat grids.
This would only give me the coordinates that are in or on the polygon, so if I were to plot the given mask and the polygon overlapped, I may have areas inside the polygon that are not filled.
Example of what that would give me :

I'd like to have the (smallest) mask that would completly enclose the polygon and thus not have any white space.
Is there a function for that
댓글 수: 2
Dyuman Joshi
2023년 8월 29일
이동: Dyuman Joshi
2023년 8월 30일
"Is there a function for that "
Philippe EAR
2023년 8월 29일
이동: Dyuman Joshi
2023년 8월 30일
채택된 답변
추가 답변 (3개)
Image Analyst
2023년 8월 30일
1 개 추천
Perhaps you'd be interested in the attached paper on "Minimum Perimeter Polygon"

KSSV
2023년 8월 29일
You need not to use a loop. If you want fine mask, increase the resolution.
X = longitude;
Y = latitude;
[Y,X] = meshgrid(Y,X);
shape = some_polygon;
% mask = zeros(size(X));
[in,on] = inpolygon(X,Y,shape(1,:),shape(2,:)) ;
mask(i,j)=in|on;
Image Analyst
2023년 8월 29일
0 개 추천
Not sure what you're calling mask and what you're calling polygon, but the smallest shape that would enclose the blue shape without any white space would be the blue shape itself.
If you want to smooth boundaries, there is a variety of ways to do that, like with activecontour or by simply blurring the shape and thresholding, or by using imclose
댓글 수: 5
Philippe EAR
2023년 8월 29일
Image Analyst
2023년 8월 29일
편집: Image Analyst
2023년 8월 29일
OK, so the polygon is the perimeter of the blue shape, not the black line.
So the smallest shape is the blue shape itself. That is the shape that will have the smallest area. To have the shape with the smallest perimeter, use convhull.
Is your data a matrix? And do you want the perimeter of the blue shape? If so you can treat the blue France as an image and use bwboundaries to get a list of (x,y) coordinates of the perimeter of the blue, or use bwperim to get the perimeter as an image where it's 1 along the perimeter and zero everywhere else, including inside.
Attach your data in a .mat file if you can.
Dyuman Joshi
2023년 8월 29일
You said this in the problem statement -
"I'd like to have the (smallest) mask that would completly enclose the polygon and thus not have any white space."
And you said this above -
"What I would like is the smallest shape made of square (corresponding to my lon/lat resolution) that completely include the outline of France."
We can't help you if you keep changing what you want to obtain.
What is it that you want? Clarify it properly once.
Also, it will be better to show a rough image of what you want to achieve.
Philippe EAR
2023년 8월 29일
There are 3 shapes separated by a NaN in the list.
This is what I have so far but I don't think we have enough resolution on the grid.
s = load('example.mat')
x = s.example.X;
y = s.example.Y;
shape = s.example.shape;
xs = shape(:, 1);
ys = shape(:, 2);
subplot(1, 2, 1);
plot(xs, ys, 'r-', 'LineWidth', 2)
grid on;
axis equal;
legend
% Shift so that min ia 1 so we can make an image.
xs = xs - min(xs) + 1;
ys = ys - min(ys) + 1;
% Get a digital image
[rows, columns] = size(x)
binaryImage = false(rows, columns);
[rowss, columnss] = size(xs)
% xs and ys are broken into groups separated by NaN. So find each groups.
nanLocations = [1; find(isnan(xs)); rowss]
for k = 1 : length(nanLocations) - 1
index1 = nanLocations(k) + 1;
index2 = nanLocations(k+1) - 1;
fprintf('Getting boundary #%d between %d and %d\n', k, index1, index2);
thisx = xs(index1 : index2);
thisy = ys(index1 : index2);
binaryImage = binaryImage | poly2mask(thisx, thisy, rows, columns);
subplot(1, 2, 2);
imshow(binaryImage)
axis('on', 'xy')
drawnow
end
카테고리
도움말 센터 및 File Exchange에서 Sparse Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






