| 1 | % 'ima_remove_particles': removes particles from an image (keeping the local minimum)
|
---|
| 2 | % requires the Matlab image processing toolbox
|
---|
| 3 |
|
---|
| 4 | %------------------------------------------------------------------------
|
---|
| 5 | %%%% Use the general syntax for transform fields with a single input %%%%
|
---|
| 6 | % OUTPUT:
|
---|
| 7 | % DataOut: output field structure
|
---|
| 8 |
|
---|
| 9 | %INPUT:
|
---|
| 10 | % DataIn: first input field structure
|
---|
| 11 | %------------------------------------------------------------------------
|
---|
| 12 | function DataOut=ima_remove_particles(DataIn)
|
---|
| 13 | %------------------------------------------------------------------------
|
---|
| 14 | DataOut=[]; %default output field
|
---|
| 15 | if strcmp(DataIn,'*')
|
---|
| 16 | return
|
---|
| 17 | end
|
---|
| 18 |
|
---|
| 19 | %parameters
|
---|
| 20 | threshold=200;
|
---|
| 21 | nblock_x=30;%size of image subblocks for analysis
|
---|
| 22 | nblock_y=30;
|
---|
| 23 | %---------------------------------------------------------
|
---|
| 24 | DataOut=DataIn;%default
|
---|
| 25 |
|
---|
| 26 | if ~isfield(DataIn,'A')
|
---|
| 27 | DataOut.Txt='remove_particles only valid for input images';
|
---|
| 28 | return
|
---|
| 29 | end
|
---|
| 30 |
|
---|
| 31 | %BACKGROUND LEVEL
|
---|
| 32 | Atype=class(DataIn.A);
|
---|
| 33 | A=double(DataIn.A);
|
---|
| 34 | Backg=zeros(size(A));
|
---|
| 35 | Aflagmin=sparse(imregionalmin(A));%Amin=1 for local image minima
|
---|
| 36 | Amin=A.*Aflagmin;%values of A at local minima
|
---|
| 37 | % local background: find all the local minima in image subblocks
|
---|
| 38 | sumblock= inline('sum(sum(x(:)))');
|
---|
| 39 | Backg=blkproc(Amin,[nblock_y nblock_x],sumblock);% take the sum in blocks
|
---|
| 40 | Bmin=blkproc(Aflagmin,[nblock_y nblock_x],sumblock);% find the number of minima in blocks
|
---|
| 41 | Backg=Backg./Bmin; % find the average of minima in blocks
|
---|
| 42 | B=imresize(Backg,size(A),'bilinear');% interpolate to the initial size image
|
---|
| 43 | ImPart=(A-B);
|
---|
| 44 | ImPart=ImPart.*(ImPart>threshold);
|
---|
| 45 | DataOut.A=A-ImPart;%
|
---|
| 46 | DataOut.A=feval(Atype,DataOut.A);
|
---|
| 47 |
|
---|