defining a polygon area within an array

조회 수: 8 (최근 30일)
Jonathan
Jonathan 2020년 3월 26일
답변: Akira Agata 2020년 3월 26일
I have a 2D array of numbers.
I have a polygon with defined x and y coordinates.
Could someone please help me set the area within the array that the polygon overlaps to 0 please.

답변 (2개)

Akira Agata
Akira Agata 2020년 3월 26일
How about the following solution?
% 100-by-100 sample matrix
Zin = peaks(100);
Zin = Zin - min(Zin(:));
% Example of polygon
pgon = polyshape([10 10 80 30], [10 30 30 10]);
% Create logical index which indicates inside/outsize the polygon
[colGrid,rowGrid] = meshgrid(1:size(Zin,2),1:size(Zin,1));
idx = isinterior(pgon,[colGrid(:),rowGrid(:)]);
idx = reshape(idx,size(Zin));
% Set elements inside the polygon to 0
Zout = Zin;
Zout(idx) = 0;
% Show the result
figure
subplot(2,2,1)
contourf(Zin)
title('Original','FontSize',14)
colorbar
subplot(2,2,2)
plot(pgon)
xlim([1 100])
ylim([1 100])
box on
title('Polygon','FontSize',14)
subplot(2,2,3)
contourf(Zout)
title('Result','FontSize',18)
colorbar

Walter Roberson
Walter Roberson 2020년 3월 26일
If you have the Computer Vision Toolbox, then you can use https://www.mathworks.com/help/vision/ref/insertshape.html#btppvxj-1-shape insertShape with 'filledpolygon' and 'Color', 'k' . This will write 0s into the part of the rectangle indicated by the coordinates. The resulting array will have been converted to RGB, but you can just take the first pane of it.
  댓글 수: 1
Jonathan
Jonathan 2020년 3월 26일
Hi there is valuable information in the resulting array containing numbers that are essential for analysis. Is there anyway of using a function (maybe without the computer vision toolbox) that can preserve the resulting array?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by