PREPROCESS segments and preprocesses a given image. Inputs: img = image array to be processed imgFile = mat file containing image to be processed psfPath = path to psf to use downsample = [1xn] or single number defining the amount to downsample the image before preprocessing. adherent = boolean flag defining if the cell is adherent(forces largest slice to bottom) top_thresh = the fraction of fluorescence you wish to consider as the top slice bot_thresh = the fraction of fluorescence you wish to consider as the bottom slice display = boolean flag of whether to display progress on screen cellmask = binary valued image array masking the inside of the cell Outputs: segimg = segmented image top_slice = integer value of the top slice where signal lies bot_slice = integer value of the top slice where signal lies sumslices = the cumsum of each slice zflip = boolean flag indicating if preprocess thinks the cell is upsidedown
0001 function [segimg, top_slice, bot_slice, sumslices,zflip] = preprocess(img, imgFile, psfPath, downsample, adherent, top_thresh, bot_thresh, display, cellmask) 0002 %PREPROCESS segments and preprocesses a given image. 0003 % 0004 %Inputs: 0005 % img = image array to be processed 0006 % imgFile = mat file containing image to be processed 0007 % psfPath = path to psf to use 0008 % downsample = [1xn] or single number defining the amount to downsample the image before preprocessing. 0009 % adherent = boolean flag defining if the cell is adherent(forces largest slice to bottom) 0010 % top_thresh = the fraction of fluorescence you wish to consider as the top slice 0011 % bot_thresh = the fraction of fluorescence you wish to consider as the bottom slice 0012 % display = boolean flag of whether to display progress on screen 0013 % cellmask = binary valued image array masking the inside of the cell 0014 % 0015 %Outputs: 0016 % segimg = segmented image 0017 % top_slice = integer value of the top slice where signal lies 0018 % bot_slice = integer value of the top slice where signal lies 0019 % sumslices = the cumsum of each slice 0020 % zflip = boolean flag indicating if preprocess thinks the cell is upsidedown 0021 0022 % Copyright (C) 2006 Murphy Lab 0023 % Carnegie Mellon University 0024 % 0025 % This program is free software; you can redistribute it and/or modify 0026 % it under the terms of the GNU General Public License as published 0027 % by the Free Software Foundation; either version 2 of the License, 0028 % or (at your option) any later version. 0029 % 0030 % This program is distributed in the hope that it will be useful, but 0031 % WITHOUT ANY WARRANTY; without even the implied warranty of 0032 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0033 % General Public License for more details. 0034 % 0035 % You should have received a copy of the GNU General Public License 0036 % along with this program; if not, write to the Free Software 0037 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0038 % 02110-1301, USA. 0039 % 0040 % For additional information visit http://murphylab.web.cmu.edu or 0041 % send email to murphy@cmu.edu 0042 0043 %Created by Devin Sullivan 3/21/13 moved from being a subfunction of 0044 %seg_cell_and_nucleus.m 0045 % 0046 %G. Johnson 8/22/13 - support for 2D masks, even if img is 3D 0047 %G. Johnson 8/26/13 - Re-mask the output of region_seg incase the snake moves 0048 % out of bounds 0049 0050 if ~exist('cellmask', 'var') | isempty(cellmask) 0051 cellmask = ones(size(img)); 0052 end 0053 0054 imsize = size(img); 0055 masksize = size(cellmask); 0056 0057 % Load PSFs 0058 infPSF = imfinfo( psfPath); 0059 0060 psf = zeros( infPSF(1).Height, infPSF(1).Width, length(infPSF)); 0061 0062 for I=1:length(infPSF) 0063 psf(:,:,I)=imread(psfPath,I); 0064 end 0065 0066 0067 psf = psf.^2; % Approximate confocal PSF 0068 0069 %D. Sullivan 3/21/13 initialize the z flip to 0 0070 zflip = 0; 0071 0072 % downsample PDFs and images as specified by user 0073 if exist(imgFile,'file') 0074 load(imgFile) 0075 else 0076 fprintf(1,'%s\n','Downsampling PSF and image'); tic; 0077 psf = ml_downsize(psf,downsample,'average'); 0078 resizeimg = ml_downsize(img,downsample,'average'); toc 0079 0080 if length(size(cellmask)) == 3 & masksize(3) ~= imsize(3) 0081 [~,ind] = max(squeeze(sum(sum(cellmask,1),2))); 0082 cellmask = cellmask(:,:,ind); 0083 end 0084 0085 if length(size(cellmask)) == 3 0086 resizecellmask = ml_downsize(cellmask,downsample) > 0; 0087 else 0088 resizecellmask = ml_downsize(cellmask, downsample(1:2)) > 0; 0089 resizecellmask = repmat(resizecellmask, [1,1, size(resizeimg,3)]); 0090 end 0091 0092 clear dnaim3 0093 fprintf(1,'%s\n','Deconvolving image'); tic; 0094 [image,psf2] = deconvblind(resizeimg,psf); toc 0095 % save(dnafile,'dna_image','dnaPSF2'); 0096 end 0097 0098 0099 fprintf(1,'%s\n','Segmenting image'); tic; 0100 sumslices = cumsum(squeeze(sum(sum(image)))); 0101 sumslices = sumslices/sumslices(end); 0102 bot_slice=find(sumslices> bot_thresh); %0.05, 0.02 0103 bot_slice=bot_slice(1); 0104 top_slice=find(sumslices< top_thresh); %0.95, 0.98 0105 top_slice=top_slice(end); 0106 %D.Sullivan 6/6/13, these two lines literally do nothing 0107 % bot_slice=max(bot_slice,bot_slice); 0108 % top_slice=min(top_slice,top_slice); 0109 0110 fprintf(1,'Total slices=%i, Bottom slice=%i, Top slice=%i\n',length(sumslices),bot_slice,top_slice); 0111 0112 mask = image > ml_rcthreshold(image(:)); 0113 for i = 1:size(mask,3) 0114 mask(:,:,i) = bwfill(mask(:,:,i), 'holes'); 0115 end 0116 mask = repmat(sum(mask,3), [1,1,size(mask,3)]) > 0; 0117 0118 mask = tz_maskimg_3d( mask, resizecellmask ); 0119 0120 segimg = region_seg(image, mask, 1000, 0.7, 0, 0.00001); 0121 0122 segimg = ml_findmainobj(segimg); 0123 0124 segimg = tz_maskimg_3d(segimg, resizecellmask); 0125 0126 0127 inds = find(sum(sum(segimg,1),2)); 0128 bot_seg = inds(1); 0129 top_seg = inds(end); 0130 0131 bot_slice=max([bot_slice,bot_seg]); 0132 top_slice=min([top_slice,top_seg]); 0133 0134 0135 if bot_slice > bot_seg 0136 0137 0138 % segimg = active3Dsegment(image, bot_slice, top_slice,display,[], 1); 0139 0140 if adherent %false, <param> 0141 %D. Sullivan 3/21/13 check if image is right side up. 0142 %since we expect the cell to generally get larger at the bottom for 0143 %adherent cell lines, if the slope of the areas is positive we should 0144 %flip the order 0145 0146 %get total cell area per slice 0147 areas = squeeze(sum(sum(segimg))); 0148 %eliminate ones that are not in the cell 0149 cellareas = areas(areas~=0); 0150 %P = coefficients P(1)*X+P(2) 0151 P = polyfit([1:length(cellareas)],cellareas',1); 0152 if P(1)>0 0153 %set zflip flag 0154 zflip = 1; 0155 segimg = flipdim(segimg,3); 0156 end 0157 [bot_slice,top_slice]=fixadherent(segimg,bot_slice,top_slice); 0158 end 0159 clear image 0160 toc 0161 0162 0163 end