├── EnforceMirrorBoundary.m ├── GVF3D.m └── LICENSE /EnforceMirrorBoundary.m: -------------------------------------------------------------------------------- 1 | function [f] = EnforceMirrorBoundary(f) 2 | % This function enforces the mirror boundary conditions 3 | % on the 3D input image f. The values of all voxels at 4 | % the boundary is set to the values of the voxels 2 steps 5 | % inward 6 | [N M O] = size(f); 7 | 8 | xi = 2:M-1; 9 | yi = 2:N-1; 10 | zi = 2:O-1; 11 | 12 | % Coners 13 | f([1 N], [1 M], [1 O]) = f([3 N-2], [3 M-2], [3 O-2]); 14 | 15 | % Edges 16 | f([1 N], [1 M], zi) = f([3 N-2], [3 M-2], zi); 17 | f(yi, [1 M], [1 O]) = f(yi, [3 M-2], [3 O-2]); 18 | f([1 N], xi, [1 O]) = f([3 N-2], xi, [3 O-2]); 19 | 20 | % Faces 21 | f([1 N], xi, zi) = f([3 N-2], xi, zi); 22 | f(yi, [1 M], zi) = f(yi, [3 M-2], zi); 23 | f(yi, xi, [1 O]) = f(yi, xi, [3 O-2]); 24 | end 25 | -------------------------------------------------------------------------------- /GVF3D.m: -------------------------------------------------------------------------------- 1 | function [u,v,w] = GVF3D(f, mu, iterations) 2 | % This function calculates the gradient vector flow (GVF) 3 | % of a 3D image f. 4 | % 5 | % inputs: 6 | % f : The 3D image 7 | % mu : The regularization parameter. Adjust it to the amount 8 | % of noise in the image. More noise higher mu 9 | % iterations: The number of iterations. 10 | % sqrt(nr of voxels) is a good choice 11 | % 12 | % outputs: 13 | % u,v,w : The GVF 14 | % 15 | % Function is written by Erik Smistad, Norwegian University 16 | % of Science and Technology (June 2011) based on the original 17 | % 2D implementation by Xu and Prince 18 | 19 | % Normalize 3D image to be between 0 and 1 20 | f = (f-min(f(:)))/(max(f(:))-min(f(:))); 21 | 22 | % Enforce the mirror conditions on the boundary 23 | f = EnforceMirrorBoundary(f); 24 | 25 | % Calculate the gradient of the image f 26 | [Fx, Fy, Fz] = gradient(f); 27 | magSquared = Fx.*Fx + Fy.*Fy + Fz.*Fz; 28 | 29 | % Set up the initial vector field 30 | u = Fx; 31 | v = Fy; 32 | w = Fz; 33 | 34 | for i = 1:iterations 35 | fprintf(1, '%d\n', i); 36 | 37 | % Enforce the mirror conditions on the boundary 38 | u = EnforceMirrorBoundary(u); 39 | v = EnforceMirrorBoundary(v); 40 | w = EnforceMirrorBoundary(w); 41 | 42 | % Update the vector field 43 | u = u + mu*6*del2(u) - (u-Fx).*magSquared; 44 | v = v + mu*6*del2(v) - (v-Fy).*magSquared; 45 | w = w + mu*6*del2(w) - (w-Fz).*magSquared; 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2011 Erik Smistad. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without modification, are 4 | permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of 7 | conditions and the following disclaimer. 8 | 9 | 2. Redistributions in binary form must reproduce the above copyright notice, this list 10 | of conditions and the following disclaimer in the documentation and/or other materials 11 | provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY Erik Smistad ''AS IS'' AND ANY EXPRESS OR IMPLIED 14 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 15 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OR 16 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 18 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 19 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 20 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 21 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 | 23 | The views and conclusions contained in the software and documentation are those of the 24 | authors and should not be interpreted as representing official policies, either expressed 25 | or implied, of Erik Smistad. 26 | --------------------------------------------------------------------------------