annotate inst/imdilate.m @ 640:19907cddd83a

graythresh: fix bug on mean method (unitialized variable), use mean on otsu's methdo when multiple max values, add tests
author carandraug
date Mon, 01 Oct 2012 15:49:46 +0000
parents 22009b99ee6b
children 7b6b493d66b5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
628
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
1 ## Copyright (C) 2004 Josep Mones i Teixidor <jmones@puntbarra.com>
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
2 ## Copyright (C) 2008 Soren Hauberg <soren@hauberg.org>
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
3 ## Copyright (C) 2010 Carnë Draug <carandraug+dev@gmail.com>
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
4 ##
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
5 ## This program is free software; you can redistribute it and/or modify it under
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
6 ## the terms of the GNU General Public License as published by the Free Software
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
7 ## Foundation; either version 3 of the License, or (at your option) any later
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
8 ## version.
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
9 ##
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
10 ## This program is distributed in the hope that it will be useful, but WITHOUT
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
13 ## details.
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
14 ##
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
15 ## You should have received a copy of the GNU General Public License along with
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
16 ## this program; if not, see <http://www.gnu.org/licenses/>.
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
17
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
18 ## -*- texinfo -*-
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
19 ## @deftypefn {Function File} {@var{B} =} imdilate (@var{A}, @var{se})
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
20 ## Perform morphological dilation on a given image.
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
21 ##
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
22 ## The image @var{A} must be a grayscale or binary image, and @var{se} a
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
23 ## structuring element. Both must have the same class, e.g., if @var{A} is a
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
24 ## logical matrix, @var{se} must also be logical. Note that the erosion
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
25 ## algorithm is different for each class, being much faster for logical
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
26 ## matrices. As such, if you have a binary matrix, you should use @code{logical}
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
27 ## first. This will also reduce the memory usage of your code.
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
28 ##
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
29 ## The center of @var{SE} is calculated using floor((size(@var{SE})+1)/2).
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
30 ##
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
31 ## Pixels outside the image are considered to be 0.
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
32 ##
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
33 ## @seealso{imerode, imopen, imclose}
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
34 ## @end deftypefn
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
35
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
36 function retval = imdilate(im, se)
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
37 ## Checkinput
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
38 if (nargin != 2)
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
39 print_usage();
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
40 endif
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
41 if (!ismatrix(im) || !isreal(im))
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
42 error("imdilate: first input argument must be a real matrix");
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
43 elseif (!ismatrix(se) || !isreal(se))
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
44 error("imdilate: second input argument must be a real matrix");
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
45 elseif ( !strcmp(class(im), class(se)) )
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
46 error("imdilate: image and structuring element must have the same class");
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
47 endif
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
48
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
49 ## Perform filtering
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
50 ## Filtering must be done with the reflection of the structuring element (they
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
51 ## are not always symmetrical)
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
52 se = imrotate(se, 180);
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
53
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
54 ## If image is binary/logical, try to use filter2 (much faster)
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
55 if (islogical(im))
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
56 retval = filter2(se,im)>0;
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
57 else
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
58 retval = ordfiltn(im, sum(se(:)!=0), se, 0);
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
59 endif
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
60
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
61 endfunction
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
62
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
63 %!demo
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
64 %! imdilate(eye(5),ones(2,2))
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
65 %! % returns a thick diagonal.
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
66
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
67 %!assert(imdilate(eye(3),[1])==eye(3)); # using [1] as a mask returns the same value
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
68 %!assert(imdilate(eye(3),[1,0,0])==[0,0,0;1,0,0;0,1,0]); # check if it works with non-symmetric SE
22009b99ee6b image: fix dos line ending style
carandraug
parents: 561
diff changeset
69 %!assert(imdilate(eye(5),[1,0,0,0])==[0,0,0,0,0;1,0,0,0,0;0,1,0,0,0;0,0,1,0,0;0,0,0,1,0]); # test if center is correctly calculated on even masks