Home > detect > WBCparameterTuning.m

WBCparameterTuning

PURPOSE ^

% Simpe GUI to tune detection parameter

SYNOPSIS ^

function WBCparameterTuning(dataFolder)

DESCRIPTION ^

% Simpe GUI to tune detection parameter

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function WBCparameterTuning(dataFolder)
0002 %% Simpe GUI to tune detection parameter
0003 
0004 iPath = 'C:\Research\WBC\Data\';
0005 
0006 fr = 2;
0007 iFolder = strcat(iPath,dataFolder);
0008 cd(strcat(iFolder,'/MN'));
0009 iFiles = dir('*.mn.png');
0010 
0011 % create background
0012 try
0013     bg = imread('../BG.png');
0014 catch
0015     bg = WBCbg(iFiles);
0016 end
0017 
0018 % load mask file
0019 try
0020     amask = imread('../amask.png');
0021 catch
0022     error('Amask file is not ready. Consider create one.');
0023 end
0024 
0025 % create blood flow direction
0026 try
0027     load('../_bloodFlowDir.mat');
0028 catch
0029     f1 = figure,imshow('../amask.png');
0030     [x0,y0] = ginput(1);
0031     [x1,y1] = ginput(1);
0032     bloodFlowDir = [round(x1-x0), round(y1-y0)];
0033     save('../_bloodFlowDir.mat','bloodFlowDir');
0034     close(f1);
0035 end
0036 
0037 
0038 f = figure('Visible','off','Position',[360,500,600,700]);
0039 % Appearance threshold
0040 k = 21;
0041 thresh_temp_diff = 25;
0042 thresh_rm = .365;
0043 kh = floor(k/2);
0044 k2 = 2*k-1;
0045 kh2= floor(k2/2);
0046 rm_layer = 2; % number layer of radial kernel
0047 
0048 % create filter kernels
0049 [rm_k] = create_radial_mean_kernel(k,rm_layer);
0050 reg1 = rm_k == 0;
0051 reg2 = (rm_k == 1);
0052 reg3 = (rm_k == 2);
0053 sum_pix1 = sum(reg1(:));
0054 sum_pix2 = sum(reg2(:));
0055 sum_pix3 = sum(reg3(:));
0056 
0057 
0058 appreanceTXT = uicontrol(f,'Style','text','String','Appreance Threshold',...
0059      'Position',[50,175,150,15]);
0060 appreanceEDT = uicontrol(f,'Style','edit','String',num2str(thresh_rm),...
0061      'Position',[200,175,50,15]);
0062 appreanceTHR = uicontrol(f,'Style','slider',...
0063     'Max', .4,...
0064     'Min', .3,...
0065     'Value', thresh_rm,...
0066     'SliderStep',[.2 .1],...
0067     'Position',[50 150 300 20],...
0068     'Callback',@(src,event)appreanceTHR_cb(src,event));
0069 
0070 motionTXT = uicontrol(f,'Style','text','String','Motion Threshold',...
0071     'Position',[50,125,150,15]);
0072 motionEDT = uicontrol(f,'Style','edit','String',num2str(thresh_temp_diff),...
0073     'Position',[200,125,50,15]);
0074 motionTHR = uicontrol(f,'Style','slider',...
0075     'Max', 100,...
0076     'Min', 10,...
0077     'Value', thresh_temp_diff,...
0078     'SliderStep',[.2 .1],...
0079     'Position',[50 100 300 20],...
0080     'Callback',@(src,event)motionTHR_cb(src,event));
0081 
0082 preview = uicontrol(f,'Style','pushbutton','String','Preview',...
0083     'Position',[50,50,75,25],...
0084     'Callback',{@preview_cb});
0085 
0086 nextImage = uicontrol(f,'Style','pushbutton','String','Next Image',...
0087     'Position',[50,50,75,25],...
0088     'Callback',{@loadImage_cb});
0089 
0090 randImage = uicontrol(f,'Style','pushbutton','String','Random Image',...
0091     'Position',[150,50,125,25],...
0092     'Callback',{@loadRandImage_cb});
0093 
0094 savePars = uicontrol(f,'Style','pushbutton','String','Save Parameters',...
0095     'Position',[300,50,100,25],...
0096     'Callback',{@save_cb});
0097 
0098 
0099 frEDT = uicontrol(f,'Style','edit','String',num2str(fr),...
0100     'Position',[520,135,30,15]);
0101 
0102 
0103 ha = axes('Units','Pixels','Position',[50,200,500,500]);
0104 
0105 im = imread(iFiles(fr).name);
0106 [R, C] = size ( im );
0107 rad_im = zeros(size(im));
0108 im_subtract = zeros(size(im));
0109 imshow(zeros(size(im)));
0110 % Assign the GUI a name to appear in the window title.
0111 set(f,'Name','Detection Parameter Tuning');
0112 % Move the GUI to the center of the screen.
0113 movegui(f,'center');
0114 % Make the GUI visible.
0115 set(f,'Visible','on');
0116 
0117 
0118 %% HELPER FUNCTIONS
0119     function appreanceTHR_cb(src,event)
0120         thresh_rm = get(src,'Value');
0121         set(appreanceEDT,'String',num2str(thresh_rm));
0122         changeThresh(im_subtract);
0123     end
0124 
0125     function motionTHR_cb(src,event)
0126         thresh_temp_diff = get(src,'Value');
0127         set(motionEDT,'String',num2str(thresh_temp_diff));
0128         changeThresh(im_subtract);
0129     end
0130 
0131     function loadImage_cb(src,event)
0132         fr = fr + 1;
0133         im = imread(iFiles(fr).name);
0134         im_subtract = imsubtract(im, bg);
0135         im_subtract = medfilt2(im_subtract); 
0136         set(frEDT,'String',num2str(fr));
0137         detect(im_subtract);
0138     end
0139 
0140     function loadRandImage_cb(src,event)
0141         fr = round(length(iFiles)*rand(1));
0142         im = imread(iFiles(fr).name);
0143         im_subtract = imsubtract(im, bg);
0144         im_subtract = medfilt2(im_subtract); 
0145         set(frEDT,'String',num2str(fr));
0146         detect(im_subtract);
0147     end
0148 
0149     function detect(im_subtract)
0150         imshow(im);
0151         text(100,100,'Detecting cells...','color','w','FontSize',15);
0152         pause(.001);
0153         im = medfilt2(im, [5 5]);               
0154         for r = kh2+1:(R-kh2-1)
0155             % for each col
0156             for c = kh2+1:(C-kh2-1)
0157                 if (amask(r,c)==0)
0158                     r0 = max ( 1, r-kh );
0159                     c0 = max ( 1, c-kh );
0160                     r1 = min ( R, r+kh );
0161                     c1 = min ( C, c+kh );
0162 
0163                     im_sub = im ( r0:r1, c0:c1 );
0164 
0165                     % compute radial mean
0166                     radial_mean1 = sum(im_sub(reg1(:))) / sum_pix1;
0167                     radial_mean2 = sum(im_sub(reg2(:))) / sum_pix2;
0168                     radial_mean3 = sum(im_sub(reg3(:))) / sum_pix3;
0169                     %normalized
0170                     radial_mean = radial_mean1 / ...
0171                         (radial_mean1 + radial_mean2+ radial_mean3);
0172 
0173                     rad_im(r,c) = radial_mean;
0174                 end
0175             end
0176         end
0177         changeThresh(im_subtract);
0178         
0179     end
0180 
0181   
0182 
0183     function save_cb(src,event)
0184         save('../_detectionParameters.mat','thresh_rm');
0185         msgbox('Parameters saved.');
0186         close(f);
0187     end
0188 
0189 
0190     function changeThresh(im_subtract)
0191         % Thresholding
0192         imRGB = gray_to_rgb(im);        
0193         for r = kh2+1:(R-kh2-1)
0194             % for each col
0195             for c = kh2+1:(C-kh2-1)
0196                 if (amask(r,c)==0)
0197                     if (rad_im(r,c) > thresh_rm)
0198                         imRGB(r,c,:)= [255 0 0];
0199                     elseif (im_subtract(r,c) > thresh_temp_diff)
0200                         imRGB(r,c,:)= [0 255 0];
0201                     end
0202                 end
0203             end
0204         end
0205         imshow(imRGB);
0206 
0207     end
0208 end

Generated on Thu 17-Mar-2011 14:45:51 by m2html © 2005