Changeset 950 for trunk/src/transform_field/ima_levels.m
- Timestamp:
- Jun 11, 2016, 9:32:29 PM (8 years ago)
- 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 1 11 %======================================================================= 2 12 % Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France … … 17 27 %======================================================================= 18 28 19 function DataOut=im_levels(DataIn) 20 %% set GUI config: no action defined 21 DataOut=[]; %default output field 22 if strcmp(DataIn,'*') 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}); 23 42 return 24 43 end 25 %----------------------------------------------- 26 %parameters 27 np=30 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 28 53 %--------------------------------------------------------- 29 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); 30 59 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 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 A=(A-B);%substract background 72 AMean=blkproc(A,[nblock_y nblock_x],fctblock);% take the mean in blocks 73 fctblock= inline('var(x(:))'); 74 AVar=blkproc(A,[nblock_y nblock_x],fctblock);% take the mean in blocks 75 Avalue=AVar./AMean% typical value of particle luminosity 76 Avalue=imresize(Avalue,size(A),'bilinear');% interpolate to the initial size image 77 DataOut.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 67 80 68 81 69 70 DataOut.A=uint8(C);
Note: See TracChangeset
for help on using the changeset viewer.