Contours to polygons

  • Follow


How can I quickly convert contours to polygons? i.e. How can I quickly extract all polygons (x and y) from C, where [C,g] = contour(Z); ?

The structure of C looks complicated -- anyone have something handy?
0
Reply Sujit 3/24/2010 7:11:05 PM

"Sujit " <kirpekar@gmail.com> wrote in message <hodo49$t4u$1@fred.mathworks.com>...
> How can I quickly convert contours to polygons? i.e. How can I quickly extract all polygons (x and y) from C, where [C,g] = contour(Z); ?
> 
> The structure of C looks complicated -- anyone have something handy?

A = peaks(40);
v=[-10:10]

c = contourc(A, v);
struct = contour2poly(c)

%%
function cstruct = contour2poly(c)

idxlabel = 1;
l = 1;
cstruct = struct('level', {}, 'x', {}, 'y', {});
while idxlabel <= size(c,2)
    n = c(2,idxlabel);
    cstruct(l).x = c(1,idxlabel+(1:n));
    cstruct(l).y = c(2,idxlabel+(1:n));
    cstruct(l).level = c(1,idxlabel);
    l = l+1;
    idxlabel = idxlabel+n+1;
end

end % contour2poly

% Bruno
0
Reply Bruno 3/24/2010 7:57:20 PM


Sujit wrote:
> How can I quickly convert contours to polygons? i.e. How can I quickly 
> extract all polygons (x and y) from C, where [C,g] = contour(Z); ?
> 
> The structure of C looks complicated -- anyone have something handy?

Take the other approach -- go into the handle that is returned!

polycells = arrayfun( @(ch) horzcat( get(ch,'XData'), get(ch,'YData')), 
get(g,'Children'),'Uniform',0);  %g is the handle from contour()

will return a cell array, each of which contains an array of coordinate pairs, 
X in column 1 and Y in column 2. If that's not exactly how you you would like 
the output, you can adjust to suit your needs. Note that the fetched XData and 
YData will be column vectors.
0
Reply Walter 3/24/2010 8:09:43 PM

Bruno Luong wrote:
> "Sujit " <kirpekar@gmail.com> wrote in message 
> <hodo49$t4u$1@fred.mathworks.com>...
>> How can I quickly convert contours to polygons?

> function cstruct = contour2poly(c)

> while idxlabel <= size(c,2)


Ah, but can you code it without an explicit loop? ;-)

(I have a mental outline for a recursive approach, and a hack in mind that 
would use arrayfun...)
0
Reply Walter 3/24/2010 8:27:46 PM

Walter Roberson <roberson@hushmail.com> wrote in message 
> Ah, but can you code it without an explicit loop? ;-)
> 
> (I have a mental outline for a recursive approach, and a hack in mind that 
> would use arrayfun...)

Of course you are right Walter, it is straightforward to convert while-loop to equivalent recursive call:

function cstruct = contour2poly(c)

if isempty(c)
    cstruct = struct('level', {}, 'x', {}, 'y', {});
else
    n = c(2,1);
    idx = 2:n+1;
    cstruct = [struct('level', c(1,1), 'x', c(1,idx), 'y', c(2,idx)) ...
        contour2poly(c(:,n+2:end))];
end

end % contour2poly

% Bruno
1
Reply Bruno 3/24/2010 11:03:07 PM

"Sujit " <kirpekar@gmail.com> wrote in message 
news:hodo49$t4u$1@fred.mathworks.com...
> How can I quickly convert contours to polygons? i.e. How can I quickly 
> extract all polygons (x and y) from C, where [C,g] = contour(Z); ?
>
> The structure of C looks complicated -- anyone have something handy?

The structure of the contour matrix isn't that complicated.  It's described 
in the documentation for CONTOURC:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/contourc.html

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ 


0
Reply Steven 3/25/2010 2:34:29 AM

5 Replies
351 Views

(page loaded in 0.213 seconds)

Similiar Articles:













7/23/2012 5:04:17 AM


Reply: