[37] | 1 | % EDGELINK - Link edge points in an image into lists |
---|
| 2 | % |
---|
| 3 | % Usage: [edgelist edgeim] = edgelink(im, minlength, location) |
---|
| 4 | % |
---|
| 5 | % Arguments: im - Binary edge image, it is assumed that edges |
---|
| 6 | % have been thinned. |
---|
| 7 | % minlength - Minimum edge length of interest |
---|
| 8 | % location - Optional complex valued image holding subpixel |
---|
| 9 | % locations of edge points. For any pixel the |
---|
| 10 | % real part holds the subpixel row coordinate of |
---|
| 11 | % that edge point and the imaginary part holds |
---|
| 12 | % the column coordinate. See NONMAXSUP. If |
---|
| 13 | % this argument is supplied the edgelists will |
---|
| 14 | % be formed from the subpixel coordinates, |
---|
| 15 | % otherwise the the integer pixel coordinates of |
---|
| 16 | % points in 'im' are used. |
---|
| 17 | % |
---|
| 18 | % Returns: edgelist - a cell array of edge lists in row,column coords in |
---|
| 19 | % the form |
---|
| 20 | % { [r1 c1 [r1 c1 etc } |
---|
| 21 | % r2 c2 ... |
---|
| 22 | % ... |
---|
| 23 | % rN cN] ....] |
---|
| 24 | % |
---|
| 25 | % edgeim - Image with pixels labeled with edge number. |
---|
| 26 | % |
---|
| 27 | % |
---|
| 28 | % This function links edge points together into chains. Where an edge |
---|
| 29 | % diverges at a junction the function simply tracks one of the branches. |
---|
| 30 | % The other branch is eventually processed as another edge. These `broken |
---|
| 31 | % branches' can be remerged by MERGESEG after a call to LINESEG. |
---|
| 32 | % |
---|
| 33 | % See also: DRAWEDGELIST, LINESEG, MAXLINEDEV, MERGESEG, DRAWSEG, NONMAXSUP |
---|
| 34 | |
---|
| 35 | % Acknowledgement: |
---|
| 36 | % This code is inspired by David Lowe's Link.c function from the Vista image |
---|
| 37 | % processing library developed at the University of British Columbia |
---|
| 38 | % http://www.cs.ubc.ca/nest/lci/vista/vista.html |
---|
| 39 | |
---|
| 40 | % Copyright (c) 2001-2005 Peter Kovesi |
---|
| 41 | % School of Computer Science & Software Engineering |
---|
| 42 | % The University of Western Australia |
---|
| 43 | % http://www.csse.uwa.edu.au/ |
---|
| 44 | % |
---|
| 45 | % Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 46 | % of this software and associated documentation files (the "Software"), to deal |
---|
| 47 | % in the Software without restriction, subject to the following conditions: |
---|
| 48 | % |
---|
| 49 | % The above copyright notice and this permission notice shall be included in |
---|
| 50 | % all copies or substantial portions of the Software. |
---|
| 51 | % |
---|
| 52 | % The Software is provided "as is", without warranty of any kind. |
---|
| 53 | |
---|
| 54 | % February 2001 - Original version |
---|
| 55 | % September 2004 - Revised to allow subpixel edge data to be used |
---|
| 56 | |
---|
| 57 | function [edgelist, edgeim] = edgelink(im, minlength, location) |
---|
| 58 | |
---|
| 59 | global EDGEIM; % Some global variables to avoid passing (and |
---|
| 60 | % copying) of arguments, this improves speed. |
---|
| 61 | global ROWS; |
---|
| 62 | global COLS; |
---|
| 63 | |
---|
| 64 | elist = {}; |
---|
| 65 | |
---|
| 66 | EDGEIM = im ~= 0; % make sure image is binary. |
---|
| 67 | |
---|
| 68 | % EDGEIM = bwmorph(EDGEIM,'thin',Inf); % make sure edges are thinned. |
---|
| 69 | % show(EDGEIM,1) |
---|
| 70 | |
---|
| 71 | EDGEIM = double(EDGEIM); |
---|
| 72 | [ROWS, COLS] = size(EDGEIM); |
---|
| 73 | edgeNo = 1; |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | % Perform raster scan through image looking for edge points. When a |
---|
| 77 | % point is found trackedge is called to find the rest of the edge |
---|
| 78 | % points. As it finds the points the edge image pixels are labeled |
---|
| 79 | % with the -ve of their edge No |
---|
| 80 | |
---|
| 81 | for r = 1:ROWS |
---|
| 82 | for c = 1:COLS |
---|
| 83 | if EDGEIM(r,c) == 1 |
---|
| 84 | edgepoints = trackedge(r,c, edgeNo, minlength); |
---|
| 85 | if ~isempty(edgepoints) |
---|
| 86 | edgelist{edgeNo} = edgepoints; |
---|
| 87 | edgeNo = edgeNo + 1; |
---|
| 88 | end |
---|
| 89 | end |
---|
| 90 | end |
---|
| 91 | end |
---|
| 92 | |
---|
| 93 | edgeim = -EDGEIM; % Finally negate image to make edge encodings +ve. |
---|
| 94 | |
---|
| 95 | % If subpixel edge locations are supplied upgrade the integer precision |
---|
| 96 | % edgelists that were constructed with data from 'location'. |
---|
| 97 | if nargin == 3 |
---|
| 98 | for I = 1:length(edgelist) |
---|
| 99 | ind = sub2ind(size(im),edgelist{I}(:,1),edgelist{I}(:,2)); |
---|
| 100 | edgelist{I}(:,1) = real(location(ind))'; |
---|
| 101 | edgelist{I}(:,2) = imag(location(ind))'; |
---|
| 102 | end |
---|
| 103 | end |
---|
| 104 | |
---|
| 105 | % show(edgeim,2), colormap(flag); |
---|
| 106 | |
---|
| 107 | %---------------------------------------------------------------------- |
---|
| 108 | % TRACKEDGE |
---|
| 109 | % |
---|
| 110 | % Function to track all the edge points associated with a start point. From |
---|
| 111 | % a given starting point it tracks in one direction, storing the coords of |
---|
| 112 | % the edge points in an array and labelling the pixels in the edge image |
---|
| 113 | % with the -ve of their edge number. When no more connected points are |
---|
| 114 | % found the function returns to the start point and tracks in the opposite |
---|
| 115 | % direction. Finally a check for the overall number of edge points found is |
---|
| 116 | % made and the edge is ignored if it is too short. |
---|
| 117 | % |
---|
| 118 | % Note that when a junction is encountered along an edge the function |
---|
| 119 | % simply tracks one of the branches. The other branch is eventually |
---|
| 120 | % processed as another edge. These `broken branches' can be remerged by |
---|
| 121 | % MERGESEG. |
---|
| 122 | % |
---|
| 123 | % Usage: edgepoints = trackedge(rstart, cstart, edgeNo, minlength) |
---|
| 124 | % |
---|
| 125 | % Arguments: rstart, cstart - row and column No of starting point |
---|
| 126 | % edgeNo - the current edge number |
---|
| 127 | % minlength - minimum length of edge to accept |
---|
| 128 | % |
---|
| 129 | % Returns: edgepoints - Nx2 array of row and col values for |
---|
| 130 | % each edge point. |
---|
| 131 | % An empty array is returned if the edge |
---|
| 132 | % is less than minlength long. |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | function edgepoints = trackedge(rstart, cstart, edgeNo, minlength) |
---|
| 136 | |
---|
| 137 | global EDGEIM; |
---|
| 138 | global ROWS; |
---|
| 139 | global COLS; |
---|
| 140 | |
---|
| 141 | edgepoints = [rstart cstart]; % Start a new list for this edge. |
---|
| 142 | EDGEIM(rstart,cstart) = -edgeNo; % Edge points in the image are |
---|
| 143 | % encoded by -ve of their edgeNo. |
---|
| 144 | |
---|
| 145 | [thereIsAPoint, r, c] = nextpoint(rstart,cstart); % Find next connected |
---|
| 146 | % edge point. |
---|
| 147 | |
---|
| 148 | while thereIsAPoint |
---|
| 149 | edgepoints = [edgepoints % Add point to point list |
---|
| 150 | r c ]; |
---|
| 151 | EDGEIM(r,c) = -edgeNo; % Update edge image |
---|
| 152 | [thereIsAPoint, r, c] = nextpoint(r,c); |
---|
| 153 | end |
---|
| 154 | |
---|
| 155 | edgepoints = flipud(edgepoints); % Reverse order of points |
---|
| 156 | |
---|
| 157 | % Now track from original point in the opposite direction |
---|
| 158 | |
---|
| 159 | [thereIsAPoint, r, c] = nextpoint(rstart,cstart); |
---|
| 160 | |
---|
| 161 | while thereIsAPoint |
---|
| 162 | edgepoints = [edgepoints |
---|
| 163 | r c ]; |
---|
| 164 | EDGEIM(r,c) = -edgeNo; |
---|
| 165 | [thereIsAPoint, r, c] = nextpoint(r,c); |
---|
| 166 | end |
---|
| 167 | |
---|
| 168 | % Reject short edges |
---|
| 169 | Npoints = size(edgepoints,1); |
---|
| 170 | if Npoints < minlength |
---|
| 171 | for i = 1:Npoints % Clear pixels in the edge image |
---|
| 172 | EDGEIM(edgepoints(i,1), edgepoints(i,2)) = 0; |
---|
| 173 | end |
---|
| 174 | edgepoints = []; % Return empty array |
---|
| 175 | end |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | %---------------------------------------------------------------------- |
---|
| 179 | % |
---|
| 180 | % NEXTPOINT |
---|
| 181 | % |
---|
| 182 | % Function finds a point that is 8 connected to an existing edge point |
---|
| 183 | % |
---|
| 184 | |
---|
| 185 | function [thereIsAPoint, nextr, nextc] = nextpoint(rp,cp); |
---|
| 186 | |
---|
| 187 | global EDGEIM; |
---|
| 188 | global ROWS; |
---|
| 189 | global COLS; |
---|
| 190 | |
---|
| 191 | % row and column offsets for the eight neighbours of a point |
---|
| 192 | % starting with those that are 4-connected. |
---|
| 193 | roff = [1 0 -1 0 1 1 -1 -1]; |
---|
| 194 | coff = [0 1 0 -1 1 -1 -1 1]; |
---|
| 195 | |
---|
| 196 | r = rp+roff; |
---|
| 197 | c = cp+coff; |
---|
| 198 | |
---|
| 199 | % Search through neighbours and return first connected edge point |
---|
| 200 | for i = 1:8 |
---|
| 201 | if r(i) >= 1 & r(i) <= ROWS & c(i) >= 1 & c(i) <= COLS |
---|
| 202 | if EDGEIM(r(i),c(i)) == 1 |
---|
| 203 | nextr = r(i); |
---|
| 204 | nextc = c(i); |
---|
| 205 | thereIsAPoint = 1; |
---|
| 206 | return; % break out and return with the data |
---|
| 207 | end |
---|
| 208 | end |
---|
| 209 | end |
---|
| 210 | |
---|
| 211 | % If we get here there was no connecting next point. |
---|
| 212 | nextr = 0; |
---|
| 213 | nextc = 0; |
---|
| 214 | thereIsAPoint = 0; |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | |
---|