Mercurial > forge
changeset 11606:e6d0a45f9c65 octave-forge
geometry: removing \r character from text
author | jpicarbajal |
---|---|
date | Sun, 07 Apr 2013 13:11:47 +0000 |
parents | 3f0e50d95c8f |
children | 5f92eaa842cb |
files | main/geometry/inst/polygons2d/polygons2d.m main/geometry/inst/polynomialCurves2d/polynomialCurveSetFit.m |
diffstat | 2 files changed, 198 insertions(+), 176 deletions(-) [+] |
line wrap: on
line diff
--- a/main/geometry/inst/polygons2d/polygons2d.m Fri Apr 05 19:24:09 2013 +0000 +++ b/main/geometry/inst/polygons2d/polygons2d.m Sun Apr 07 13:11:47 2013 +0000 @@ -2,16 +2,16 @@ ## Copyright (C) 2004-2011 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas) ## Copyright (C) 2012 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch> ## All rights reserved. -## +## ## Redistribution and use in source and binary forms, with or without ## modification, are permitted provided that the following conditions are met: -## +## ## 1 Redistributions of source code must retain the above copyright notice, ## this list of conditions and the following disclaimer. ## 2 Redistributions in binary form must reproduce the above copyright ## notice, this list of conditions and the following disclaimer in the ## documentation and/or other materials provided with the distribution. -## +## ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -22,151 +22,151 @@ ## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -## -*- texinfo -*- -## @deftypefn {Function File} {} polygons2d () -## Description of functions operating on 2D polygons -## -## The 'polygons' module contains functions operating on shapes composed -## of a vertex list, like polygons or polylines. -## -## We call 'polyline' the curve defined by a series of vertices. -## A polyline can be either closed or open, depending on whether the last -## vertex is connected to the first one or not. This can be given as an -## option is some functions in the module. -## A 'polygon' is the planar domain delimited by a closed polyline. We -## sometimes want to consider 'complex polygons', whose boundary is -## composed of several disjoint domains. The domain defined by a single -## closed polyline is called 'simple polygon'. -## We call 'curve' a polyline with many vertices, such that the polyline -## can be considered as a discrete approximation of a "real" curve. -## -## A simple polygon or polyline is represented by a N-by-2 array, each row -## of the array representing the coordinates of a vertex. -## Simple polygons are assumed to be closed, so there is no need to repeat -## the first vertex at the end. -## As both polygons and polylines can be represented by a list of vertex -## coordinates, some functions also consider the vertex list itself. Such -## functions are prefixed by 'pointSet'. Also, many functions prefixed by -## 'polygon' or 'polyline' works also on the other type of shape. -## -## For multiple-connected polygons, the different connected boundaries are -## separated by a row [NaN NaN]. -## -## For some functions, the orientation of the polygon can be relevant: CCW -## stands for 'Conter-Clockwise' (positive orientation), CW stands for -## 'Clockwise'. -## -## Polylines are parametrized in the following way: -## * the i-th vertex is located at position i-1 -## * points of the i-th edge have positions ranging linearly from i-1 to i -## The parametrization domain for an open polyline is from 0 to Nv-1, and -## from 0 to Nv for a closed polyline (positions 0 and Nv correspond to -## the same point). -## -## Example: -## # Simple polygon: -## P1 = [1 1;2 1;2 2;1 2]; -## drawPolygon(P1); -## axis([0 5 0 5]); -## # Multiple polygon: -## P2 = [10 10;40 10;40 40;10 40;NaN NaN;20 20;20 30;30 30;30 20]; -## figure;drawPolygon(P2); axis([0 50 0 50]); -## -## -## Point Sets -## pointSetBounds - Bounding box of a set of points -## pointSetsAverage - Compute the average of several point sets -## minimumCaliperDiameter - Minimum caliper diameter of a set of points -## findPoint - Find index of a point in an set from its coordinates -## -## Polylines -## polylinePoint - Extract a point from a polyline -## polylineLength - Return length of a polyline given as a list of points -## polylineCentroid - Compute centroid of a curve defined by a series of points -## polylineSubcurve - Extract a portion of a polyline -## reversePolyline - Reverse a polyline, by iterating vertices from the end -## isPointOnPolyline - Test if a point belongs to a polyline -## projPointOnPolyline - Compute position of a point projected on a polyline -## distancePointPolyline - Compute shortest distance between a point and a polyline -## distancePolylines - Compute the shortest distance between 2 polylines -## intersectPolylines - Find the common points between 2 polylines -## polylineSelfIntersections - Find self-intersections points of a polyline -## -## Curves (polylines with lot of vertices) -## parametrize - Parametrization of a curve, based on edges length -## curvature - Estimate curvature of a polyline defined by points -## cart2geod - Convert cartesian coordinates to geodesic coord. -## geod2cart - Convert geodesic coordinates to cartesian coord. -## curveMoment - Compute inertia moment of a 2D curve -## curveCMoment - Compute centered inertia moment of a 2D curve -## curveCSMoment - Compute centered scaled moment of a 2D curve -## -## Polygons -## polygonPoint - Extract a point from a polygon -## polygonSubcurve - Extract a portion of a polygon -## reversePolygon - Reverse a polygon, by iterating vertices from the end -## projPointOnPolygon - Compute position of a point projected on a polygon -## splitPolygons - Convert a NaN separated polygon list to a cell array of polygons -## clipPolygon - Clip a polygon with a rectangular box -## clipPolygonHP - Clip a polygon with a Half-plane defined by a directed line -## intersectLinePolygon - Intersection points between a line and a polygon -## intersectRayPolygon - Intersection points between a ray and a polygon -## polygonSelfIntersections - Find-self intersection points of a polygon -## convexHull - Convex hull of a set of points -## polygonLoops - Divide a possibly self-intersecting polygon into a set of simple loops -## expandPolygon - Expand a polygon by a given (signed) distance -## medialAxisConvex - Compute medial axis of a convex polygon -## -## Measures on Polygons -## isPointInPolygon - Test if a point is located inside a polygon -## polygonContains - Test if a point is contained in a multiply connected polygon -## polygonCentroid - Compute the centroid (center of mass) of a polygon -## polygonArea - Compute the signed area of a polygon -## polygonLength - Perimeter of a polygon -## polygonNormalAngle - Compute the normal angle at a vertex of the polygon -## polygonBounds - Compute the bounding box of a polygon -## distancePointPolygon - Compute shortest distance between a point and a polygon -## distancePolygons - Compute the shortest distance between 2 polygons -## -## Triangles -## isPointInTriangle - Test if a point is located inside a triangle -## triangleArea - Area of a triangle -## -## Functions from stochastic geometry -## steinerPoint - Compute steiner point (weighted centroid) of a polygon -## steinerPolygon - Create a Steiner polygon from a set of vectors -## supportFunction - Compute support function of a polygon -## convexification - Compute the convexification of a polygon -## -## Input, Output and conversions -## readPolygon - Read a polygon stored in a file -## polygonToRow - Convert polygon coordinates to a row vector -## rowToPolygon - Create a polygon from a row vector -## rectAsPolygon - Convert a (centered) rectangle into a series of points -## -## Drawing functions -## drawPolyline - Draw a polyline specified by a list of points -## drawPolygon - Draw a polygon specified by a list of points -## fillPolygon - Fill a polygon specified by a list of points -## -## -## Credits: -## * function intersectPolylines uses the 'interX' contribution from "NS" -## (file exchange 22441, called 'curve-intersections') -## -## ----- -## Author: David Legland -## e-mail: david.legland@@grignon.inra.fr -## created the 07/11/2005. -## Homepage: @url{http://matgeom.sourceforge.net/} -## @url{http://www.pfl-cepia.inra.fr/index.php?page=geom2d} -## Copyright INRA - Cepia Software Platform. -## -## @end deftypefn - -function polygons2d () - -help('polygons2d'); - -endfunction + +## -*- texinfo -*- +## @deftypefn {Function File} {} polygons2d () +## Description of functions operating on 2D polygons +## +## The 'polygons' module contains functions operating on shapes composed +## of a vertex list, like polygons or polylines. +## +## We call 'polyline' the curve defined by a series of vertices. +## A polyline can be either closed or open, depending on whether the last +## vertex is connected to the first one or not. This can be given as an +## option is some functions in the module. +## A 'polygon' is the planar domain delimited by a closed polyline. We +## sometimes want to consider 'complex polygons', whose boundary is +## composed of several disjoint domains. The domain defined by a single +## closed polyline is called 'simple polygon'. +## We call 'curve' a polyline with many vertices, such that the polyline +## can be considered as a discrete approximation of a "real" curve. +## +## A simple polygon or polyline is represented by a N-by-2 array, each row +## of the array representing the coordinates of a vertex. +## Simple polygons are assumed to be closed, so there is no need to repeat +## the first vertex at the end. +## As both polygons and polylines can be represented by a list of vertex +## coordinates, some functions also consider the vertex list itself. Such +## functions are prefixed by 'pointSet'. Also, many functions prefixed by +## 'polygon' or 'polyline' works also on the other type of shape. +## +## For multiple-connected polygons, the different connected boundaries are +## separated by a row [NaN NaN]. +## +## For some functions, the orientation of the polygon can be relevant: CCW +## stands for 'Conter-Clockwise' (positive orientation), CW stands for +## 'Clockwise'. +## +## Polylines are parametrized in the following way: +## * the i-th vertex is located at position i-1 +## * points of the i-th edge have positions ranging linearly from i-1 to i +## The parametrization domain for an open polyline is from 0 to Nv-1, and +## from 0 to Nv for a closed polyline (positions 0 and Nv correspond to +## the same point). +## +## Example: +## # Simple polygon: +## P1 = [1 1;2 1;2 2;1 2]; +## drawPolygon(P1); +## axis([0 5 0 5]); +## # Multiple polygon: +## P2 = [10 10;40 10;40 40;10 40;NaN NaN;20 20;20 30;30 30;30 20]; +## figure;drawPolygon(P2); axis([0 50 0 50]); +## +## +## Point Sets +## pointSetBounds - Bounding box of a set of points +## pointSetsAverage - Compute the average of several point sets +## minimumCaliperDiameter - Minimum caliper diameter of a set of points +## findPoint - Find index of a point in an set from its coordinates +## +## Polylines +## polylinePoint - Extract a point from a polyline +## polylineLength - Return length of a polyline given as a list of points +## polylineCentroid - Compute centroid of a curve defined by a series of points +## polylineSubcurve - Extract a portion of a polyline +## reversePolyline - Reverse a polyline, by iterating vertices from the end +## isPointOnPolyline - Test if a point belongs to a polyline +## projPointOnPolyline - Compute position of a point projected on a polyline +## distancePointPolyline - Compute shortest distance between a point and a polyline +## distancePolylines - Compute the shortest distance between 2 polylines +## intersectPolylines - Find the common points between 2 polylines +## polylineSelfIntersections - Find self-intersections points of a polyline +## +## Curves (polylines with lot of vertices) +## parametrize - Parametrization of a curve, based on edges length +## curvature - Estimate curvature of a polyline defined by points +## cart2geod - Convert cartesian coordinates to geodesic coord. +## geod2cart - Convert geodesic coordinates to cartesian coord. +## curveMoment - Compute inertia moment of a 2D curve +## curveCMoment - Compute centered inertia moment of a 2D curve +## curveCSMoment - Compute centered scaled moment of a 2D curve +## +## Polygons +## polygonPoint - Extract a point from a polygon +## polygonSubcurve - Extract a portion of a polygon +## reversePolygon - Reverse a polygon, by iterating vertices from the end +## projPointOnPolygon - Compute position of a point projected on a polygon +## splitPolygons - Convert a NaN separated polygon list to a cell array of polygons +## clipPolygon - Clip a polygon with a rectangular box +## clipPolygonHP - Clip a polygon with a Half-plane defined by a directed line +## intersectLinePolygon - Intersection points between a line and a polygon +## intersectRayPolygon - Intersection points between a ray and a polygon +## polygonSelfIntersections - Find-self intersection points of a polygon +## convexHull - Convex hull of a set of points +## polygonLoops - Divide a possibly self-intersecting polygon into a set of simple loops +## expandPolygon - Expand a polygon by a given (signed) distance +## medialAxisConvex - Compute medial axis of a convex polygon +## +## Measures on Polygons +## isPointInPolygon - Test if a point is located inside a polygon +## polygonContains - Test if a point is contained in a multiply connected polygon +## polygonCentroid - Compute the centroid (center of mass) of a polygon +## polygonArea - Compute the signed area of a polygon +## polygonLength - Perimeter of a polygon +## polygonNormalAngle - Compute the normal angle at a vertex of the polygon +## polygonBounds - Compute the bounding box of a polygon +## distancePointPolygon - Compute shortest distance between a point and a polygon +## distancePolygons - Compute the shortest distance between 2 polygons +## +## Triangles +## isPointInTriangle - Test if a point is located inside a triangle +## triangleArea - Area of a triangle +## +## Functions from stochastic geometry +## steinerPoint - Compute steiner point (weighted centroid) of a polygon +## steinerPolygon - Create a Steiner polygon from a set of vectors +## supportFunction - Compute support function of a polygon +## convexification - Compute the convexification of a polygon +## +## Input, Output and conversions +## readPolygon - Read a polygon stored in a file +## polygonToRow - Convert polygon coordinates to a row vector +## rowToPolygon - Create a polygon from a row vector +## rectAsPolygon - Convert a (centered) rectangle into a series of points +## +## Drawing functions +## drawPolyline - Draw a polyline specified by a list of points +## drawPolygon - Draw a polygon specified by a list of points +## fillPolygon - Fill a polygon specified by a list of points +## +## +## Credits: +## * function intersectPolylines uses the 'interX' contribution from "NS" +## (file exchange 22441, called 'curve-intersections') +## +## ----- +## Author: David Legland +## e-mail: david.legland@@grignon.inra.fr +## created the 07/11/2005. +## Homepage: @url{http://matgeom.sourceforge.net/} +## @url{http://www.pfl-cepia.inra.fr/index.php?page=geom2d} +## Copyright INRA - Cepia Software Platform. +## +## @end deftypefn + +function polygons2d () + +help('polygons2d'); + +endfunction
--- a/main/geometry/inst/polynomialCurves2d/polynomialCurveSetFit.m Fri Apr 05 19:24:09 2013 +0000 +++ b/main/geometry/inst/polynomialCurves2d/polynomialCurveSetFit.m Sun Apr 07 13:11:47 2013 +0000 @@ -2,16 +2,16 @@ ## Copyright (C) 2004-2011 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas) ## Copyright (C) 2012 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch> ## All rights reserved. -## +## ## Redistribution and use in source and binary forms, with or without ## modification, are permitted provided that the following conditions are met: -## +## ## 1 Redistributions of source code must retain the above copyright notice, ## this list of conditions and the following disclaimer. ## 2 Redistributions in binary form must reproduce the above copyright ## notice, this list of conditions and the following disclaimer in the ## documentation and/or other materials provided with the distribution. -## +## ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -24,13 +24,13 @@ ## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ## -*- texinfo -*- -## @deftypefn {Function File} {[@var{coefs} @var{bnds}]=} polynomialCurveSetFit (@var{img}) +## @deftypefn {Function File} {@var{coefs}=} polynomialCurveSetFit (@var{img}) ## @deftypefnx {Function File} {@dots{} =} polynomalCurveSetFit (@var{img}, @var{deg}) ## @deftypefnx {Function File} {[@dots{} @var{lbl}] =} polynomalCurveSetFit (@dots{}) ## Fit a set of polynomial curves to a segmented image ## ## Result is a cell array of matrices. Each matrix is @var{deg}+1-by-2, and -## contains coefficients of polynomial curve for each coordinate. +## contains coefficients of polynomial curve for each coordinate. ## @var{bnds} contains the boundary of the parametrizations. ## @var{img} is first binarised, then skeletonized. ## @@ -44,14 +44,13 @@ ## @seealso{polynomialCurves2d, polynomialCurveFit} ## @end deftypefn -function [coefs bnds lblBranches] = polynomialCurveSetFit(seg, varargin) +function [coefs lblBranches] = polynomialCurveSetFit(seg, varargin) # default degree for curves deg = 2; if ~isempty(varargin) deg = varargin{1}; end - # ajoute un contour seg([1 end], :) = 1; seg(:, [1 end]) = 1; @@ -63,18 +62,28 @@ imgNodes = imfilter(double(seg), ones([3 3])) .* seg > 3; # compute coordinate of nodes, as c entroids of the multiple points - lblNodes = bwlabel(imgNodes, 4); + lblNodes = bwlabel(imgNodes, 8); struct = regionprops(lblNodes, 'Centroid'); nodes = zeros(length(struct), 2); for i=1:length(struct) - nodes(i, 1:2) = struct(i).Centroid; + nodes(i, [2 1]) = struct(i).Centroid; end + figure(1) +# subplot(1,2,1) +# imshow(seg); +# hold on +# plot(nodes(:,1),nodes(:,2),'og') +# subplot(1,2,2) + imshow(imgNodes); + hold on + plot(nodes(:,1),nodes(:,2),'og') + keyboard # enleve les bords de l'image seg([1 end], :) = 0; seg(:, [1 end]) = 0; - # Isoles les branches + # Isolate the branches imgBranches = seg & ~imgNodes; lblBranches = bwlabel(imgBranches, 8); @@ -95,23 +104,23 @@ # extract points corresponding to current curve imgBranch = lblBranches == i; points = chainPixels (imgBranch); - + # check number of points is sufficient if size(points, 1) < max(deg+1-2, 2) # find labels of nodes inds = unique(lblNodes(imdilate(imgBranch, true (3,3)))); inds = inds(inds ~= 0); - + if length(inds) < 2 warning ("geometry:poylnomialCurveSetFit", ... ['Could not find extremities of branch number ' num2str(i)]); continue; end - + # consider extremity nodes node0 = nodes(inds(1), :); node1 = nodes(inds(2), :); - + # use only a linear approximation xc = zeros(1, deg+1); yc = zeros(1, deg+1); @@ -119,10 +128,9 @@ yc(1) = node0(2); xc(2) = node1(1)-node0(1); yc(2) = node1(2)-node0(2); - + # assigne au tableau de courbes coefs{i} = [xc;yc]; - bnds{i} = [0 1]; # next branch continue; end @@ -130,23 +138,37 @@ # find nodes closest to first and last points of the current curve [dist, ind0] = minDistancePoints(points(1, :), nodes); ##ok<*ASGLU> [dist, ind1] = minDistancePoints(points(end, :), nodes); - + # add nodes to the curve. points = [nodes(ind0,:); points; nodes(ind1,:)]; ##ok<AGROW> - + # parametrization of the polyline t = parametrize(points); t = t / max(t); - + # fit a polynomial curve to the set of points [xc yc] = polynomialCurveFit(... t, points, deg, ... 0, {points(1,1), points(1,2)},... 1, {points(end,1), points(end,2)}); - + + + plot(points(:,1),points(:,2),'or') + hold on + drawPolynomialCurve ([0 1], xc,yc); + axis tight + v = axis(); + hold off + imshow (~imgBranch) + hold on + plot(points(:,1),points(:,2),'or') + drawPolynomialCurve ([0 1], xc,yc); + axis xy + axis (v); + pause + # stores result coefs{i} = [xc;yc]; - bnds{i} = t([1 end]); end endfunction @@ -191,7 +213,7 @@ for i = 1:size(points, 1) # avoid multiple neighbors (can happen in loops) ind = ind(1); - + # add current point to chained curve points(i,:) = [x(ind) y(ind)]; @@ -202,12 +224,12 @@ # find next candidate ind = find(abs(x-points(i,1))<=1 & abs(y-points(i,2))<=1); end - + else for i = 1:size(points, 1) # avoid multiple neighbors (can happen in loops) ind = ind(1); - + # add current point to chained curve points(i,:) = [x(ind) y(ind)]; @@ -235,7 +257,7 @@ %! hold on %! for i=1:numel(c) %! if !isempty (c{i}) -%! drawPolynomialCurve (t{i}, c{i}(1,:),c{i}(2,:)); +%! drawPolynomialCurve ([0 1], c{i}); %! endif %! endfor %!