1 | % 'ima_remove_background': removes backgound from an image (using 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=remove_background(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 | %BACKGROUND LEVEL
|
---|
27 | Atype=class(DataIn.A);
|
---|
28 | A=double(DataIn.A);
|
---|
29 | Backg=zeros(size(A));
|
---|
30 | Aflagmin=sparse(imregionalmin(A));%Amin=1 for local image minima
|
---|
31 | Amin=A.*Aflagmin;%values of A at local minima
|
---|
32 | % local background: find all the local minima in image subblocks
|
---|
33 | sumblock= inline('sum(sum(x(:)))');
|
---|
34 | Backg=blkproc(Amin,[nblock_y nblock_x],sumblock);% take the sum in blocks
|
---|
35 | Bmin=blkproc(Aflagmin,[nblock_y nblock_x],sumblock);% find the number of minima in blocks
|
---|
36 | Backg=Backg./Bmin; % find the average of minima in blocks
|
---|
37 | B=imresize(Backg,size(A),'bilinear');% interpolate to the initial size image
|
---|
38 | ImPart=(A-B);
|
---|
39 | DataOut.A=ImPart.*(ImPart>threshold);
|
---|
40 | DataOut.A=feval(Atype,DataOut.A); |
---|