Extract outline of polygons defined by gridded mask
Mostrar comentarios más antiguos
I'm attempting to extract the polygons defined by a gridded mask, where the mask is defined pcolor-style, i.e. by the grid cell edges, and not necessarily rectilinear.
The bwboundaries function gets close to what I'm looking for, but not quite, since it does its calculations based on the center of each grid cell, rather than the edges. This causes the outline to be slightly shifted, and it leaves out one-grid-cell objects.
The following shows the discrepancy between an example mask and the outline I'm currently able to extract. Any of the image processing gurus know of a quick and easy way to extract the actual outline of all the light regions?
I = imread('rice.png');
mask = im2bw(I, graythresh(I));
mask = mask(1:100,1:100);
[ny,nx] = size(mask);
r = linspace(1,2,ny+1);
theta = linspace(3*pi/4,pi/4,nx+1);
[r,theta] = meshgrid(r, theta);
[xr, yr] = pol2cart(theta, r);
hp = pcolor(xr,yr,[mask nan(ny,1); nan(1,nx+1)]);
colormap(gray);
axis equal;
set(gca, 'clim', [-1 1]);
b1 = bwboundaries(mask, 4);
[xout, yout] = deal(cell(size(b1)));
for ib = 1:length(b1)
idx = sub2ind(size(xr), b1{ib}(:,1), b1{ib}(:,2));
xout{ib} = xr(idx);
yout{ib} = yr(idx);
end
hold on;
hl = cellfun(@(x,y) plot(x,y, 'b'), xout, yout);
set(hl, 'linewidth', 2);
Respuestas (3)
Sean de Wolski
el 30 de Abr. de 2013
0 votos
This reminded me of this blog post by Steve:
1 comentario
Kelly Kearney
el 30 de Abr. de 2013
Kelly Kearney
el 30 de Abr. de 2013
Editada: Kelly Kearney
el 30 de Abr. de 2013
1 comentario
Kelly Kearney
el 1 de Mayo de 2013
Chad Greene
el 20 de Sept. de 2017
I just ran into this same issue. My mask contains only a few grid cells and I needed an exact outline of the grid cells in my mask, preserving the 90 degree corners. Below, each black dot indicates the center of a grid cell. The true values in my mask are outlined by red circles.

In my first attempt to outline the red cells I wasn't thinking about the corners, so I tried simple contouring like this:
contour(x,y,double(mask),[0.5 0.5],'r')

But as you can see, that doesn't adequately capture the corners of each grid cell. The simplest solution I've found comes via Wolfgang Schwanghart's TopoToolbox.
% Create a grid object that links x,y locations to pixel centers of the mask:
G = GRIDobj(x,y,mask);
% Get the outline of the mask:
[~,xi,yi] = GRIDobj2polygon(G);
% Plot the outline in blue:
plot(xi,yi,'b')

1 comentario
Another option if you have a license for ArcGIS is to build a small wrapper, and then you have access to the full toolbox. The only limitation is that the geoprocessor (or arcpy) must check the license (which may require an access to a license server). If you have many operations to perform iteratively, this means that you have to extend the mechanism so you can pass a stack of operations to your wrapper.
Categorías
Más información sobre Data Type Identification en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!