function [cm,controlim] = controlroi(imsegm,labelim,nuclei,rim,prm) msg = ['This is ' upper(mfilename) ' finding control regions']; disp(msg); tic % find possible did cells (NB lower threshold than earlier) bw = imsegm > prm.detect.level*0.70; % remove noise objects bw = bwareaopen(bw,10); % label and find CM [faser,L] = bwlabeln(bw); L cm = zeros(L,2); for i = 1 : L % this region reg = faser == i; clear c; [c(:,1),c(:,2)] = find(reg); % center of mass cm(i,:) = mean(c,1); end; cm = round(cm); toc; msg = ['Finding ' int2str(L) ' potential DiD objects']; disp(msg); % distance around cells dist = prm.detect.smallestdistcells; % label image around the DiD cells not to take these regions dim = size(labelim); controlim = ones(dim(1),dim(2),'uint8'); for i = 1 : L x = max(1,cm(i,1) - dist):min(dim(1),cm(i,1)+dist); y = max(1,cm(i,2) - dist):min(dim(2),cm(i,2)+dist); controlim(x,y) = 0; end; close all % showall(imsegm,controlim) % all possible positions to use as control image clear c; [c(:,1),c(:,2)] = find(controlim); % order them randonly n = size(c,1); v = randperm(n); c = c(v,:); % rim coordinates [rim.c(:,1),rim.c(:,2)] = find(rim.bw); msg = ['Finding ' int2str(prm.detect.ncontrol) ' objects']; disp(msg); ncontrol = 0; i = 0; cmcontrol = zeros(prm.detect.ncontrol,2); while ncontrol < prm.detect.ncontrol i = i + 1; cmhere = c(i,:); x = max(1,cmhere(1)-dist):min(dim(1),cmhere(1)+dist); y = max(1,cmhere(2)-dist):min(dim(2),cmhere(2)+dist); reg = controlim(x,y); showall( % is there an overlap to a DiD cell image? if ~isempty(find(reg,1)) msg = ['Overlap to DID or earlier object']; disp(msg); continue; end; % is it dense enough of cells? imhere = nuclei.bw(x,y); [a,n] = bwlabeln(imhere); if n < prm.detect.minnumneigh msg = ['Too sparse cells']; disp(msg); continue; end; % is it far enough from rim? d = rim.c - repmat(cmhere.*prm.lowres.h,rim.nc,1); d = d.^2; d = sum(d,2); d = sqrt(d); if d < prm.detect.smallestdistrim msg = ['Too close too rim']; disp(msg); continue; end; % label this region as "taken" controlim(x,y) = 2; % and store the coordinates ncontrol = ncontrol + 1; cmcontrol(ncontrol,:) = cmhere; msg = ['Finding control ROI ' int2str(ncontrol)]; disp(msg); end; showall(bw,controlim) controlim = controlim == 2;