[574] | 1 | % 'ima_remove_background': removes backgound from an image (using the local minimum)
|
---|
| 2 | % requires the Matlab image processing toolbox
|
---|
| 3 | %------------------------------------------------------------------------
|
---|
| 4 | %%%% Use the general syntax for transform fields with a single input %%%%
|
---|
| 5 | % OUTPUT:
|
---|
| 6 | % DataOut: output field structure
|
---|
[810] | 7 | %
|
---|
[574] | 8 | %INPUT:
|
---|
| 9 | % DataIn: first input field structure
|
---|
[810] | 10 |
|
---|
| 11 | %=======================================================================
|
---|
| 12 | % Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France
|
---|
| 13 | % http://www.legi.grenoble-inp.fr
|
---|
| 14 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
|
---|
| 15 | %
|
---|
| 16 | % This file is part of the toolbox UVMAT.
|
---|
| 17 | %
|
---|
| 18 | % UVMAT is free software; you can redistribute it and/or modify
|
---|
| 19 | % it under the terms of the GNU General Public License as published
|
---|
| 20 | % by the Free Software Foundation; either version 2 of the license,
|
---|
| 21 | % or (at your option) any later version.
|
---|
| 22 | %
|
---|
| 23 | % UVMAT is distributed in the hope that it will be useful,
|
---|
| 24 | % but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 25 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 26 | % GNU General Public License (see LICENSE.txt) for more details.
|
---|
| 27 | %=======================================================================
|
---|
| 28 |
|
---|
[574] | 29 | function DataOut=remove_background(DataIn)
|
---|
| 30 | %------------------------------------------------------------------------
|
---|
| 31 | DataOut=[]; %default output field
|
---|
| 32 | if strcmp(DataIn,'*')
|
---|
| 33 | return
|
---|
| 34 | end
|
---|
| 35 |
|
---|
| 36 | %parameters
|
---|
| 37 | threshold=200
|
---|
| 38 | nblock_x=30;%size of image subblocks for analysis
|
---|
| 39 | nblock_y=30;
|
---|
| 40 | %---------------------------------------------------------
|
---|
| 41 | DataOut=DataIn;%default
|
---|
| 42 |
|
---|
| 43 | %BACKGROUND LEVEL
|
---|
| 44 | Atype=class(DataIn.A);
|
---|
| 45 | A=double(DataIn.A);
|
---|
| 46 | Backg=zeros(size(A));
|
---|
| 47 | Aflagmin=sparse(imregionalmin(A));%Amin=1 for local image minima
|
---|
| 48 | Amin=A.*Aflagmin;%values of A at local minima
|
---|
| 49 | % local background: find all the local minima in image subblocks
|
---|
| 50 | sumblock= inline('sum(sum(x(:)))');
|
---|
| 51 | Backg=blkproc(Amin,[nblock_y nblock_x],sumblock);% take the sum in blocks
|
---|
| 52 | Bmin=blkproc(Aflagmin,[nblock_y nblock_x],sumblock);% find the number of minima in blocks
|
---|
| 53 | Backg=Backg./Bmin; % find the average of minima in blocks
|
---|
| 54 | B=imresize(Backg,size(A),'bilinear');% interpolate to the initial size image
|
---|
| 55 | ImPart=(A-B);
|
---|
| 56 | DataOut.A=ImPart.*(ImPart>threshold);
|
---|
[810] | 57 | DataOut.A=feval(Atype,DataOut.A);
|
---|