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