Ignore:
Timestamp:
Jun 11, 2016, 9:32:29 PM (8 years ago)
Author:
sommeria
Message:

various updates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/transform_field/ima_levels.m

    r924 r950  
     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
    111%=======================================================================
    212% Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
     
    1727%=======================================================================
    1828
    19 function DataOut=im_levels(DataIn)
    20 %% set GUI config: no action defined
    21 DataOut=[];  %default  output field
    22 if strcmp(DataIn,'*')
     29function DataOut=ima_remove_background_blocks(DataIn,Param)
     30%------------------------------------------------------------------------
     31%% request input parameters
     32if 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});
    2342    return
    2443end
    25 %-----------------------------------------------
    26 %parameters
    27 np=30
     44if ~isfield(DataIn,'A')
     45    DataOut.Txt='remove_particles only valid for input images';
     46    return
     47end
     48if ~exist('imerode','file');
     49        DataOut.Txt='the function imerode from the image processing toolbox is needed';
     50    return
     51end
     52
    2853%---------------------------------------------------------
    2954DataOut=DataIn;%default
     55nblock_y=2*Param.TransformInput.BlockSize;
     56nblock_x=2*Param.TransformInput.BlockSize;
     57[npy,npx]=size(DataIn.A);
     58[X,Y]=meshgrid(1:npx,1:npy);
    3059
    31 B=double(DataIn.A(:,:,1));
    32 windowsize=round(min(size(B,1),size(B,2))/20);
    33 windowsize=floor(windowsize/2)*2+1;
    34 ix=[1/2-windowsize/2:-1/2+windowsize/2];%
    35 %del=np/3;
    36 %fct=exp(-(ix/del).^2);
    37 fct2=cos(ix/((np-1)/2)*pi/2);
    38 %Mfiltre=(ones(5,5)/5^2);
    39 %Mfiltre=fct2';
    40 Mfiltre=fct2'*fct2;
    41 Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
    42 
    43 C=filter2(Mfiltre,B);
    44 C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
    45 C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
    46 C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
    47 C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
    48 C=tanh(B./(2*C));
    49 [n,c]=hist(reshape(C,1,[]),100);
    50 % figure;plot(c,n);
    51 
    52 [m,i]=max(n);
    53 c_max=c(i);
    54 [dummy,index]=sort(abs(c-c(i)));
    55 n=n(index);
    56 c=c(index);
    57 i_select = find(cumsum(n)<0.95*sum(n));
    58 if isempty(i_select)
    59     i_select = 1:length(c);
    60 end
    61 c_select=c(i_select);
    62 n_select=n(i_select);
    63 cmin=min(c_select);
    64 cmax=max(c_select);
    65 C=(C-cmin)/(cmax-cmin)*256;
    66 DataOut.AA=uint8(C);
     60%BACKGROUND LEVEL
     61Atype=class(DataIn.A);
     62A=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
     67fctblock= inline('median(x(:))');
     68Backg=blkproc(A,[nblock_y nblock_x],fctblock);% take the median in  blocks
     69fctblock= inline('mean(x(:))');
     70B=imresize(Backg,size(A),'bilinear');% interpolate to the initial size image
     71A=(A-B);%substract background
     72AMean=blkproc(A,[nblock_y nblock_x],fctblock);% take the mean in  blocks
     73fctblock= inline('var(x(:))');
     74AVar=blkproc(A,[nblock_y nblock_x],fctblock);% take the mean in  blocks
     75Avalue=AVar./AMean% typical value of particle luminosity
     76Avalue=imresize(Avalue,size(A),'bilinear');% interpolate to the initial size image
     77DataOut.A=uint16(1000*tanh(A./(2*Avalue)));
     78%Bmin=blkproc(Aflagmin,[nblock_y nblock_x],sumblock);% find the number of minima in blocks
     79%Backg=Backg./Bmin; % find the average of minima in blocks
    6780
    6881
    69 
    70 DataOut.A=uint8(C);
Note: See TracChangeset for help on using the changeset viewer.