CONTOURMATRIXTOPOLYLINES Converts a contour matrix array into a polyline set. POLYS = contourMatrixToPolylines(C) Converts the contour matrix array, as given as the result of the contourc function, into a set of polylines. Example img = imread('circles.png'); C = contourc(img, 1); polys = contourMatrixToPolylines(C); imshow(img); hold on; drawPolyline(polys, 'Color', 'r', 'LineWidth', 2); See also polygons2d, contour, contourc
0001 function polys = contourMatrixToPolylines(C) 0002 %CONTOURMATRIXTOPOLYLINES Converts a contour matrix array into a polyline set. 0003 % 0004 % POLYS = contourMatrixToPolylines(C) 0005 % Converts the contour matrix array, as given as the result of the 0006 % contourc function, into a set of polylines. 0007 % 0008 % Example 0009 % img = imread('circles.png'); 0010 % C = contourc(img, 1); 0011 % polys = contourMatrixToPolylines(C); 0012 % imshow(img); hold on; 0013 % drawPolyline(polys, 'Color', 'r', 'LineWidth', 2); 0014 % 0015 % See also 0016 % polygons2d, contour, contourc 0017 0018 % ------ 0019 % Author: David Legland 0020 % e-mail: david.legland@grignon.inra.fr 0021 % Created: 2013-08-22, using Matlab 7.9.0.529 (R2009b) 0022 % Copyright 2013 INRA - Cepia Software Platform. 0023 0024 % size of the contour matrix array 0025 nCoords = size(C, 2); 0026 0027 % first, compute the number of contours 0028 nContours = 0; 0029 offset = 1; 0030 while offset < nCoords 0031 nContours = nContours + 1; 0032 offset = offset + C(2, offset) + 1; 0033 end 0034 0035 % extract each contour as a polygon or polyline 0036 polys = cell(nContours, 1); 0037 offset = 1; 0038 for iContour = 1:nContours 0039 nv = C(2, offset); 0040 polys{iContour} = C(:, offset + (1:nv))'; 0041 offset = offset + nv + 1; 0042 end