[171] | 1 | %'ima_levels': rescale the image intensity to reduce strong luminosity peaks
|
---|
| 2 | %------------------------------------------------------------------------
|
---|
| 3 | % function GUI_input=ima_levels(num_i1,num_i2,num_j1,num_j2,Series)
|
---|
| 4 | %
|
---|
| 5 | %OUTPUT
|
---|
| 6 | % GUI_input=list of options in the GUI series.fig needed for the function
|
---|
| 7 | %
|
---|
| 8 | %INPUT:
|
---|
| 9 | %num_i1: series of first indices i (given from the series interface as first_i:incr_i:last_i, mode and list_pair_civ)
|
---|
| 10 | %num_i2: series of second indices i (given from the series interface as first_i:incr_i:last_i, mode and list_pair_civ)
|
---|
| 11 | %num_j1: series of first indices j (given from the series interface as first_j:incr_j:last_j, mode and list_pair_civ )
|
---|
| 12 | %num_j2: series of second indices j (given from the series interface as first_j:incr_j:last_j, mode and list_pair_civ)
|
---|
| 13 | %Series: Matlab structure containing information set by the series interface% relabel_i_j: relabel an image series with two indices, according to the time matrix given by ImaDoc
|
---|
[64] | 14 | %----------------------------------------------------------------------
|
---|
[442] | 15 | function GUI_input=ima_levels(Param)
|
---|
[64] | 16 | %requests for the visibility of input windows in the GUI series (activated directly by the selection in the menu ACTION)
|
---|
[442] | 17 | if ~exist('Param','var')
|
---|
[64] | 18 | GUI_input={};
|
---|
| 19 | return %exit the function
|
---|
| 20 | end
|
---|
| 21 |
|
---|
[442] | 22 | %% input parameters
|
---|
| 23 | % read the xml file for batch case
|
---|
| 24 | if ischar(Param) && ~isempty(find(regexp('Param','.xml$')))
|
---|
| 25 | Param=xml2struct(Param);
|
---|
| 26 | checkrun=0;
|
---|
| 27 | else % RUN case: parameters introduced as the input structure Param
|
---|
| 28 | hseries=guidata(Param.hseries);%handles of the GUI series
|
---|
| 29 | WaitbarPos=get(hseries.waitbar_frame,'Position');
|
---|
| 30 | checkrun=1;
|
---|
| 31 | end
|
---|
| 32 | %filebase=fullfile(Param.InputTable{1,1},Param.InputTable{1,3});
|
---|
| 33 | RootPath=Param.InputTable{1,1};
|
---|
| 34 | Subdir=Param.InputTable{1,2};
|
---|
| 35 | RootFile=Param.InputTable{1,3};
|
---|
| 36 | NomType=Param.InputTable{1,4};
|
---|
| 37 | FileExt=Param.InputTable{1,5};
|
---|
| 38 | [filecell,i1_series,i2_series,j1_series]=get_file_series(Param);% list of input files and indices
|
---|
| 39 | if size(filecell,1)>1
|
---|
| 40 | msgbox_uvmat('WARNING','This function uses only the first input image series')
|
---|
| 41 | return
|
---|
| 42 | end
|
---|
[64] | 43 |
|
---|
[442] | 44 | %% determine input image type
|
---|
| 45 | [FileType,FileInfo,MovieObject]=get_file_type(filecell{1,1});
|
---|
| 46 | ListTypes={'image','multimage','mmreader','video'};
|
---|
[64] | 47 |
|
---|
[442] | 48 | if isempty(strcmp(FileType,ListTypes))% if the detected FileType is not in the list for images
|
---|
| 49 | msgbox_uvmat('ERROR',['invalid file extension ' FileExt ': this function only accepts image or movie input'])
|
---|
| 50 | return
|
---|
| 51 | end
|
---|
| 52 |
|
---|
| 53 | %% create dir of the new images
|
---|
| 54 | SubdirResult=[Param.InputTable{1,2} '.lev'];% add the suffix '.lev' to the name of the image folder
|
---|
| 55 | try
|
---|
| 56 | mkdir(fullfile(Param.InputTable{1,1},SubdirResult));
|
---|
| 57 | catch ME
|
---|
| 58 | msgbox_uvmat('ERROR',['error in creating result directory: ' ME.message]);%display error msg for directory creation if fails
|
---|
| 59 | return
|
---|
| 60 | end
|
---|
| 61 | [xx,msg2] = fileattrib(fullfile(Param.InputTable{1,1},SubdirResult),'+w','g'); %yield writing access (+w) to user group (g)
|
---|
[128] | 62 | if ~strcmp(msg2,'')
|
---|
[442] | 63 | msgbox_uvmat('ERROR',['pb of permission for ' fullfile(Param.InputTable{1,1},SubdirResult) ': ' msg2])%error message for directory creation
|
---|
[128] | 64 | return
|
---|
| 65 | end
|
---|
[160] | 66 | msgbox_uvmat('CONFIRMATION','apply image rescaling function levels.m ');
|
---|
[64] | 67 |
|
---|
| 68 | %copy the xml file
|
---|
[442] | 69 | % if exist([basename '.xml'],'file')
|
---|
| 70 | % copyfile([basename '.xml'],[basename_new '.xml']);% copy the .civ file
|
---|
| 71 | % end
|
---|
[64] | 72 |
|
---|
[442] | 73 | %% main loop
|
---|
| 74 | nbfield=size(i1_series{1},2);
|
---|
| 75 | nbfield2=size(i1_series{1},1);
|
---|
[64] | 76 | for ifile=1:nbfield
|
---|
[442] | 77 | if checkrun
|
---|
| 78 | % update_waitbar(hseries.waitbar,WaitbarPos,ifile/nbfield)
|
---|
| 79 | update_waitbar(hseries.waitbar_frame,WaitbarPos,ifile/nbfield)
|
---|
| 80 | stopstate=get(hseries.RUN,'BusyAction');
|
---|
| 81 | else
|
---|
| 82 | stopstate='queue';
|
---|
| 83 | end
|
---|
[64] | 84 | if isequal(stopstate,'queue') % enable STOP command
|
---|
| 85 | for jfile=1:nbfield2
|
---|
[442] | 86 | %filename=name_generator(basename,num_i1(jfile,ifile),num_j1(jfile,ifile),Series.FileExt,Series.NomType);
|
---|
| 87 | %filename_new=name_generator(basename_new,num_i1(jfile,ifile),num_j1(jfile,ifile),'.png',Series.NomType);
|
---|
| 88 | filename=fullfile_uvmat(RootPath,Subdir,RootFile,FileExt,NomType,i1_series{1}(jfile,ifile),[],j1_series{1}(jfile,ifile));
|
---|
| 89 | switch FileType
|
---|
| 90 | case {'video','mmreader'}
|
---|
| 91 | A=read(MovieObject,i1_series{1}(jfile,ifile));
|
---|
| 92 | case {'vol','image'}
|
---|
| 93 | A=imread(filename);
|
---|
| 94 | case 'multimage'
|
---|
| 95 | A=imread(filename,i1_series{1}(jfile,ifile));
|
---|
| 96 | end
|
---|
[64] | 97 | C=levels(A);
|
---|
[442] | 98 | filename_new=fullfile_uvmat(RootPath,SubdirResult,RootFile,'.png',NomType,i1_series{1}(jfile,ifile),[],j1_series{1}(jfile,ifile));
|
---|
[64] | 99 | imwrite(C,filename_new)
|
---|
[442] | 100 | display([filename_new ' written'])
|
---|
[64] | 101 | end
|
---|
| 102 | end
|
---|
| 103 | end
|
---|
[442] | 104 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
[64] | 105 |
|
---|
| 106 | function C=levels(A)
|
---|
| 107 | %whos A;
|
---|
| 108 | B=double(A(:,:,1));
|
---|
| 109 | windowsize=round(min(size(B,1),size(B,2))/20);
|
---|
| 110 | windowsize=floor(windowsize/2)*2+1;
|
---|
| 111 | ix=[1/2-windowsize/2:-1/2+windowsize/2];%
|
---|
| 112 | %del=np/3;
|
---|
| 113 | %fct=exp(-(ix/del).^2);
|
---|
| 114 | fct2=cos(ix/(windowsize-1)/2*pi/2);
|
---|
| 115 | %Mfiltre=(ones(5,5)/5^2);
|
---|
| 116 | %Mfiltre=fct2';
|
---|
| 117 | Mfiltre=fct2'*fct2;
|
---|
| 118 | Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
|
---|
| 119 |
|
---|
[71] | 120 | C=filter2(Mfiltre,B);
|
---|
[64] | 121 | C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
|
---|
| 122 | C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
|
---|
| 123 | C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
|
---|
| 124 | C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
|
---|
| 125 | C=tanh(B./(2*C));
|
---|
| 126 | [n,c]=hist(reshape(C,1,[]),100);
|
---|
| 127 | % figure;plot(c,n);
|
---|
| 128 |
|
---|
| 129 | [m,i]=max(n);
|
---|
| 130 | c_max=c(i);
|
---|
| 131 | [dummy,index]=sort(abs(c-c(i)));
|
---|
| 132 | n=n(index);
|
---|
| 133 | c=c(index);
|
---|
| 134 | i_select = find(cumsum(n)<0.95*sum(n));
|
---|
| 135 | if isempty(i_select)
|
---|
| 136 | i_select = 1:length(c);
|
---|
| 137 | end
|
---|
| 138 | c_select=c(i_select);
|
---|
| 139 | n_select=n(i_select);
|
---|
| 140 | cmin=min(c_select);
|
---|
| 141 | cmax=max(c_select);
|
---|
| 142 | C=(C-cmin)/(cmax-cmin)*256;
|
---|
| 143 | C=uint8(C);
|
---|