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
|
---|
7 | %
|
---|
8 | %INPUT:
|
---|
9 | % DataIn: first input field structure
|
---|
10 |
|
---|
11 | %=======================================================================
|
---|
12 | % Copyright 2008-2022, LEGI UMR 5519 / CNRS UGA 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 |
|
---|
29 | function DataOut=ima_remove_background_blocks(DataIn,Param)
|
---|
30 | %------------------------------------------------------------------------
|
---|
31 | %% request input parameters
|
---|
32 | if isfield(DataIn,'Action') && isfield(DataIn.Action,'RUN') && isequal(DataIn.Action.RUN,0)
|
---|
33 | prompt = {'block size(pixels)'};
|
---|
34 | dlg_title = 'get the block size (in pixels) used to calculate the local statistics';
|
---|
35 | num_lines= 1;
|
---|
36 | def = { '100'};
|
---|
37 | if isfield(Param,'TransformInput')&&isfield(Param.TransformInput,'BlockSize')
|
---|
38 | def={num2str(Param.TransformInput.BlockSize)};
|
---|
39 | end
|
---|
40 | answer = inputdlg(prompt,dlg_title,num_lines,def);
|
---|
41 | DataOut.TransformInput.BlockSize=str2num(answer{1});
|
---|
42 | return
|
---|
43 | end
|
---|
44 | if ~isfield(DataIn,'A')
|
---|
45 | DataOut.Txt='remove_particles only valid for input images';
|
---|
46 | return
|
---|
47 | end
|
---|
48 | if ~exist('imerode','file');
|
---|
49 | DataOut.Txt='the function imerode from the image processing toolbox is needed';
|
---|
50 | return
|
---|
51 | end
|
---|
52 |
|
---|
53 | %---------------------------------------------------------
|
---|
54 | DataOut=DataIn;%default
|
---|
55 | nblock_y=2*Param.TransformInput.BlockSize;
|
---|
56 | nblock_x=2*Param.TransformInput.BlockSize;
|
---|
57 | [npy,npx]=size(DataIn.A);
|
---|
58 | [X,Y]=meshgrid(1:npx,1:npy);
|
---|
59 |
|
---|
60 | %BACKGROUND LEVEL
|
---|
61 | Atype=class(DataIn.A);
|
---|
62 | A=double(DataIn.A);
|
---|
63 | %Backg=zeros(size(A));
|
---|
64 | %Aflagmin=sparse(imregionalmin(A));%Amin=1 for local image minima
|
---|
65 | %Amin=A.*Aflagmin;%values of A at local minima
|
---|
66 | % local background: find all the local minima in image subblocks
|
---|
67 | fctblock= inline('median(x(:))');
|
---|
68 | Backg=blkproc(A,[nblock_y nblock_x],fctblock);% take the median in blocks
|
---|
69 | fctblock= inline('mean(x(:))');
|
---|
70 | B=imresize(Backg,size(A),'bilinear');% interpolate to the initial size image
|
---|
71 | DataOut.A=B;
|
---|
72 |
|
---|
73 | % A=(A-B);%substract background
|
---|
74 | % AMean=blkproc(A,[nblock_y nblock_x],fctblock);% take the mean in blocks
|
---|
75 | % fctblock= inline('var(x(:))');
|
---|
76 | % AVar=blkproc(A,[nblock_y nblock_x],fctblock);% take the mean in blocks
|
---|
77 | % Avalue=AVar./AMean;% typical value of particle luminosity
|
---|
78 | % Avalue=imresize(Avalue,size(A),'bilinear');% interpolate to the initial size image
|
---|
79 | % DataOut.A=uint16(1000*tanh(A./(2*Avalue)));
|
---|
80 | %Bmin=blkproc(Aflagmin,[nblock_y nblock_x],sumblock);% find the number of minima in blocks
|
---|
81 | %Backg=Backg./Bmin; % find the average of minima in blocks
|
---|
82 |
|
---|
83 |
|
---|
84 |
|
---|
85 |
|
---|