Files
hartman-server/qhtcp-workflow/apps/matlab/easy/NIscanIntensBGpar4GblFnc.m

1225 lines
56 KiB
Matlab
Executable File

%% CALLED BY par4GblFnc8c.m %%
function [Tmpsbdg2, scanIntens, F_spots, bmtp, optomizedPos, TmpexpScanIntens2, TmpFexpScanSpots2, TmpFexpScanBMtp2, TmpanlZoneRefs2,areaOfIntensAboveBG]= ...
NIscanIntensBGpar4GblFnc(Fflg, tifFileLst, ImParMat,PTmapPos,optCirMask,diaExt,doCircle,cirPixA,numRows,numCols,ImHeigth,ImWidth,cirMask, ...
tptLength,selScan,Empsc,~, ~, ~, ~,easyResultsDir, Tmpsbdg1)
global matFile
global CSrchRng
global figsResultsDir
global fotosResultsDir
searchIntens=[];intensMax=[];detMaxPos=[];scIntens=[];areaOfIntensAboveBG=[];BkgrdMat=[];
% TODO needs explanation
if Fflg==1
% Preallocation
tPtLength=length(tifFileLst);
optomizedPos=cell(24,16,tPtLength);
F_spots=cell(24,16,tPtLength); % Foto spot coordinates Preallocation
bmtp=zeros(24,16,tPtLength); % Added/used for FOTOS Preallocation
MPnum=ImParMat(1);
destPerMP=ImParMat(2);
spotThres=ImParMat(4)/100;
% width=ImParMat(5);
CSrchRng=ImParMat(12);
width=24; % absolute fixed after Sept 2014
ditherF=ImParMat(6);
ditherF=1; % absolute fixed after Sept 2014
% Get PTmap data (detPos) and create PTimage with cirMask 2019_0904
PTmapOnesDbl=zeros(2075,1400);
for r=1:24
for c=1:16
[PTrefPt]=PTmapPos{r,c};
PTrefPtR=PTrefPt(1);
PTrefPtC=PTrefPt(2);
% doCircle
PTmapOnesDbl(PTrefPtR:(PTrefPtR+(diaExt-1)),PTrefPtC:(PTrefPtC+(diaExt-1)))=optCirMask;
end
end
end
% Zeroth Initial Search Parameters
rRangeUpper=-12;
rRangeLower=12;
cRangeLower=-12;
cRangeUpper=12;
ditherI=width/3;
searchRange=floor(width*0.75); %generally -/+18
%searchRange=CSrchRng
rCRangeUpper=-searchRange; % -12 % -8; %Ranges should be multiples of dither
rCRangeLower=searchRange; %12; %Ranges should be multiples of dither
cCRangeLower=-searchRange; %-12 %-8; %-12; %Ranges should be multiples of dither
cCRangeUpper=searchRange; %12; %8; %Ranges should be multiples of dither
ditherC=floor((2*searchRange)/9); %gnerally /6 0r /9 -> dither=6 or 4 pixels
if ditherC==0,ditherC=1;end
rFRangeUpper=-floor(1/2*width); %(searchRange*2/3) %-12 % -8; %Ranges should be multiples of dither
rFRangeLower=floor(1/2*width); %(searchRange*2/3); %12; %Ranges should be multiples of dither
cFRangeLower=-floor(1/2*width); %(searchRange*2/3); %-12 %-8; %-12; %Ranges should be multiples of dither
cFRangeUpper=floor(1/2*width); %(searchRange*2/3) %12; %8; %Ranges should be multiples of dither
ditherF=1;
% The purpose is to reduce incidence of 'chasing' scratches and abrasions to
% the edge of a frame and also to limit late timept finds of overgrown
% spot(that are yet small). The idea is to make the threshold high enough to
% prevent the tracking of scratches and small contaminants(overgrowth) while
% not limiting detection of small slow grow cultures. Recommend value
% slightly above the expected background level.
noSpotThres=60; % If PixelIntensity for area < area*this, use the Orig.search Refpt
if doCircle==1
noSpot=cirPixA*noSpotThres;
else
noSpot=(width^2)*noSpotThres; %Area * (a selected background pixel level i.e., 70)
end
% Begin Time series loop
% Preallocation
stopSearch=zeros(numRows,numCols); %set all spots 'stopSearch' to false
intens=zeros(24,16);
intensPrev=zeros(24,16);
cent=cell(24,16);
BGTav=zeros(numRows,numCols);
% 7 0;before incr.to95 to accommodate dark media %50forEpson;%Initialize for first tPt run
BGthres(1:numRows,1:numCols)=95;
if length(tifFileLst)>2
% Determine a good Plate Image from all the timepoints to use for Registration
% Plate Intensity curve over time points
% clear plateImage
plateImage={};
%{
% Version compatability fixes
v11a='7.12.0.635 (R2011a)';
v14a='8.3.0.532 (R2014a)';
%v14b='8.4.0.150421 (R2014b)';
v18a='9.4.0.813654 (R2018a)';
v18b='9.5.0.944444 (R2018b)';
pool=2;
if isequal(v11a,version)|| isequal(v14a,version)
if matlabpool('size')==0
matlabpool(pool)
end
else
if parpool('local')==0
parpool('local', pool)
end
end
%}
% parfor iii=1:length(tifFileLst)
for iii=1:length(tifFileLst)
tifFile=char(tifFileLst(iii));
fullplt=imread(tifFile); % Ncode actually reading a .bmp file for Ncode
fullpl=255 - fullplt(:,:,1);
%fullpl=(fullplt(1:ImHeigth,1:ImWidth));
pltInts(iii)=sum(sum(fullpl(:,:)))/(ImHeigth*ImWidth);
plateImage{iii}=fullpl; % 190919 to use instead of re-reading in NIscanIntensBG.m
end
guessOffsetAdj=0.5; %Half(0.5) is the midpoint intensity
guessOffset=(max(pltInts)- min(pltInts)) * guessOffsetAdj; %Could plug-in a curvefit routine to determine an 'L' value
guessVal=max(pltInts)- guessOffset;
guesTpt=find(pltInts > guessVal,1,'first');
if isempty(guesTpt)
guesTpt=1;
end
PtptIm=tifFileLst(guesTpt);
PtptImg=char(tifFileLst(guesTpt));
ImageRd=imread(PtptImg); %Ncode actually reading a .bmp file for Ncode
PltImg=(ImageRd(1:ImHeigth,1:ImWidth));
PltImg=255 - PltImg;
% Convert to image to BW then AND with PTmapOnesDbl
GraythrShift=1;
BWimage=im2bw(PltImg, GraythrShift*graythresh(PltImg));
matchAnded=BWimage & PTmapOnesDbl;
initPTANDed=matchAnded;
ANDValInitPT=sum(sum(initPTANDed(:,:)));
% Begin Image Registration for Pintool
MaxANDVal=0; % sum(sum(BWimage(:,:))); %seed MaxANDVal from the BWimage
regDith=4;
TestmapOnesDbl=zeros(2075,1400);
% Create a Search Range centered on C==1 and R==1 of the PinTool map
RtopBndry=96; RbotBndry=176;
ClfBndry=96; CrtBndry=154;
[PTrefPt]=PTmapPos{1,1};
PTrefPtR=PTrefPt(1);
PTrefPtC=PTrefPt(2);
rngUpper=RtopBndry - PTrefPtR;
quotT=round(abs(rngUpper)/regDith);
topRang=regDith * quotT;
if rngUpper<0, topRang=-1*topRang; end
rngLower=RbotBndry - PTrefPtR;
quotB=round(abs(rngLower)/regDith);
botRang=regDith * quotB;
if rngLower<0, botRang=-1*botRang; end
rngLeft=ClfBndry - PTrefPtC;
quotLf=round(abs(rngLeft)/regDith);
leftRang=regDith * quotLf;
if rngLeft<0, leftRang=-1*leftRang; end
rngRight=CrtBndry - PTrefPtC;
quotRt=round(abs(rngRight)/regDith);
rightRang=regDith * quotRt;
if rngRight<0, rightRang=-1*rightRang; end
% Orthagonal Registration
testPos=cell(24,16);
for shiftPosR=topRang:regDith:botRang
for shiftPosC=leftRang:regDith:rightRang
TestmapOnesDbl=zeros(2075,1400);
for r=1:24
for c=1:16
[PTrefPt]=PTmapPos{r,c};
testPosR=PTrefPt(1) + shiftPosR;
testPosC=PTrefPt(2) + shiftPosC;
a=[testPosR,testPosC]; %testPos(r,c)={testPosR,testPosC};
testPos(r,c)={a};
doCircle;
TestmapOnesDbl(testPosR:(testPosR+(diaExt-1)),testPosC:(testPosC+(diaExt-1)))=cirMask; %optCirMask;
end
end
GraythrShift=1.0;
BWtestimage=im2bw(TestmapOnesDbl, GraythrShift*graythresh(TestmapOnesDbl));
matchAnded=BWimage & BWtestimage;
ANDVal=sum(sum(matchAnded(:,:)));
if ANDVal> MaxANDVal
MaxANDVal=ANDVal;
MaxPos=testPos;
MaxPosDbl=TestmapOnesDbl;
end
end
end
if MaxANDVal>ANDValInitPT
MaxPosDbl=MaxPosDbl;
MaxPos=MaxPos;
else
MaxPosDbl=PTmapOnesDbl;
MaxPos=PTmapPos;
end
% SKEW Registration
testSkPos=MaxPos;
MaxANDValSk=0; %MaxANDVal;
skQty=0.05;
regDith=4;
for shiftPos=-regDith/2:regDith/2
for k=-5:5
sk=k* skQty;
TestmapSkDbl=zeros(2075,1400);
for r=1:24
for c=1:16
[skrefPt]=testSkPos{r,c};
testSkPosR=round(skrefPt(1) + (c*sk + shiftPos));
testSkPosC=round(skrefPt(2) + (-r*sk + shiftPos));
a=[testSkPosR,testSkPosC]; % testPos(r,c)={testPosR,testPosC};
testSkPos(r,c)={a};
% doCircle
TestmapSkDbl(testSkPosR:(testSkPosR+(diaExt-1)),testSkPosC:(testSkPosC+(diaExt-1)))=cirMask; % optCirMask;
end
end
GraythrShift=1;
BWtestSkimage=im2bw(TestmapSkDbl, GraythrShift*graythresh(TestmapSkDbl));
matchAndedSk=BWimage & BWtestSkimage;
ANDValSk=sum(sum(matchAndedSk(:,:)));
if ANDValSk> MaxANDValSk
MaxANDValSk=ANDValSk;
MaxPosSk=testSkPos;
MaxPosSkDbl=TestmapSkDbl;
end
end
end
if MaxANDValSk> MaxANDVal
MaxANDVal=MaxANDValSk;
MaxPosDbl=MaxPosSkDbl;
MaxPos=MaxPosSk;
end
% Center of Area for Final Registration
% Note GraythrShift in NImapPT can be used to a lower or higher 'decider'
% and thus effect the centroid(center of area) that this module determines.
% GraythrShift=1.0 normally allowing the Matlab graythresh to calc. the BW decider
range=0;
lastDetMaxCentDbl=zeros(2075,1400);
lastDetMaxPos=cell(24,16);
% CIRCLE related
for r=1:24
for c=1:16
[refPt]=MaxPos{r,c};
refPtR=refPt(1);
refPtC=refPt(2);
% Calc. Centroid Position of spot
detPrLo=(MaxPos{r,c}(1)) -(range); detPrHi=(MaxPos{r,c}(1)) + (diaExt+range);
detPcLo=(MaxPos{r,c}(2)) -(range); detPcHi=(MaxPos{r,c}(2)) + (diaExt+range);
rsum=(sum(BWimage(detPrLo:detPrHi,detPcLo:detPcHi),2));
csum=(sum(BWimage(detPrLo:detPrHi,detPcLo:detPcHi),1));
if sum(rsum)>1 && sum(csum)>1
Rvec=detPrLo:detPrHi;
Cvec=detPcLo:detPcHi;
centR=round((sum(Rvec .*rsum')) /sum(rsum));
centC=round((sum(Cvec .*csum)) /sum(csum));
aa=[(centR-diaExt/2) (centC-diaExt/2)];
else
rsum=(sum(MaxPosDbl(detPrLo:detPrHi,detPcLo:detPcHi),2));
csum=(sum(MaxPosDbl(detPrLo:detPrHi,detPcLo:detPcHi),1));
Rvec=detPrLo:detPrHi;
Cvec=detPcLo:detPcHi;
centR=round((sum(Rvec .*rsum')) /sum(rsum));
centC=round((sum(Cvec .*csum)) /sum(csum));
end
aa=[(centR-diaExt/2) (centC-diaExt/2)];
lastDetMaxPos(r,c)={aa};
% 2nd Recenter Based on Previous
detPrLo=lastDetMaxPos{r,c}(1) -(range); detPrHi=lastDetMaxPos{r,c}(1) + (diaExt+range);
detPcLo=lastDetMaxPos{r,c}(2) -(range); detPcHi=lastDetMaxPos{r,c}(2) + (diaExt+range);
rsum=(sum(BWimage(detPrLo:detPrHi,detPcLo:detPcHi),2));
csum=(sum(BWimage(detPrLo:detPrHi,detPcLo:detPcHi),1));
if sum(rsum)>1 && sum(csum)>1
Rvec=detPrLo:detPrHi;
Cvec=detPcLo:detPcHi;
centR=round((sum(Rvec .*rsum')) /sum(rsum));
centC=round((sum(Cvec .*csum)) /sum(csum));
aa=[(centR-diaExt/2) (centC-diaExt/2)];
else
rsum=(sum(MaxPosDbl(detPrLo:detPrHi,detPcLo:detPcHi),2));
csum=(sum(MaxPosDbl(detPrLo:detPrHi,detPcLo:detPcHi),1));
Rvec=detPrLo:detPrHi;
Cvec=detPcLo:detPcHi;
centR=round((sum(Rvec .*rsum')) /sum(rsum));
centC=round((sum(Cvec .*csum)) /sum(csum));
end
aa=[(centR-diaExt/2) (centC-diaExt/2)];
lastDetMaxPos(r,c)={aa};
[CentPt]=lastDetMaxPos{r,c};
CentPtR=CentPt(1);
CentPtC=CentPt(2);
% doCircle
lastDetMaxCentDbl(CentPtR:(CentPtR+(diaExt-1)),CentPtC:(CentPtC+(diaExt-1)))=cirMask; % optCirMask;
end
end
BWtestCentimage=im2bw(lastDetMaxCentDbl, GraythrShift*graythresh(lastDetMaxCentDbl));
matchAndedCent=BWimage & BWtestCentimage;
ANDValCent=sum(sum(matchAndedCent(:,:)));
if ANDValCent>=MaxANDVal
MaxPosDbl=lastDetMaxCentDbl;
MaxPos=lastDetMaxPos;
% detPos=lastDetMaxPos;
end
% detPos=lastDetMaxPos;
detPos=MaxPos;
end
% Preallocation for speed on 2019_1121 ***
scIntens=zeros(24,16,length(tifFileLst));
BkgrdMat=zeros(24,16,length(tifFileLst));
% areaOfIntensAboveBG=zeros(24,16,length(tifFileLst));
lastDetMaxPos=cell(numRows, numCols);
for tPt=1:length(tifFileLst)
tPt;
tifFile=char(tifFileLst(tPt));
fullsc=plateImage{tPt};
fIntsc=fullsc;
% Bad Image Filter Zone
% NORMALLY turned off; This is Hand HARD CODE SPECIFIC to particular ExpJOB
specificHardFilterCode=0;
if specificHardFilterCode==1 && tPt==1
% Plate scratch filter prototype could be added here
fIntsc(:,1105:1190)=uint8(67); %Specific blocking area set to background level here
% fIntsc(:,:)=uint8(67); % set entire image to background level if 1.bmp is
% bad. This allows acquisiton of the initial "Print Time" from .bmp time stamp.
end
tp=90; bt=1935; lf=90; rt=1318; blkVal=10; %tp=90;
fIntsc(1:tp,:)=uint8(blkVal); %top of blocking frame
fIntsc(bt:ImHeigth,:)=uint8(blkVal); %bottom of blocking frame
fIntsc(:,1:lf)=uint8(blkVal); %left side of blocking frame
fIntsc(:,rt:ImWidth)=uint8(blkVal); %right side of blocking frame
bwIntsc=im2bw(fIntsc,80/255);
% Collect Intensities (Absorbances)
% end for search for maximum intensity spot detection alignment
if doCircle==1
earlySrc1Thres=.31*cirPixA*255; % Initial search spot det. threshold
earlySrc2Thres=spotThres*cirPixA*255; % Course search spot det. threshold
definedSpot=(spotThres+.03)*cirPixA*255; % typically .34 ; 20000; %Intensity at which a spot is well defined for a width=24 {formerly20} % 0.5*((width^2 * (255- backgrdPixel))
else
earlySrc1Thres=.31*(width^2)*255; % Initial search spot det. threshold
earlySrc2Thres=spotThres*(width^2)*255; % Course search spot det. threshold
definedSpot=(spotThres+.03)*(width^2)*255; % typically .34 ; 20000; %Intensity at which a spot is well defined for a width=24 {formerly20} % 0.5*((width^2 * (255- backgrdPixel))
end
detMaxPos=cell(numRows, numCols);
bcent=cell(numRows, numCols);
for r=1:numRows
for c=1:numCols
[refPt]=detPos{r,c};
refPtR=refPt(1); InitR=refPt(1);
refPtC=refPt(2); InitC=refPt(2);
% ZeroSearchIntens
% clear ('searchIntens')
searchIntens=[];
if doCircle==1
% cutout=fIntsc(refPtR+1:(refPtR+(diaExt)),(refPtC+1:(refPtC+diaExt)));
cutout=fIntsc(refPtR:(refPtR+(diaExt-1)),refPtC:(refPtC+(diaExt-1)));
Imcir=cutout .* uint8(cirMask);
zeroSearchIntens=sum(sum(Imcir(1:(diaExt-1),1:(diaExt-1))));
else % standard square analysis area
refPtRExt=refPtR+widthEx;
if (refPtRExt)> size(fIntsc,1), refPtRExt=size(fIntsc,1); end
refPtCExt=refPtC+widthEx;
if (refPtCExt)> size(fIntsc,2), refPtCExt=size(fIntsc,2); end
zeroSearchIntens=sum(sum(fIntsc(refPtR:(refPtRExt),refPtC:(refPtCExt))));
end
% clear ('searchIntens')
searchIntens=[];
intensMax=-1;
src1cnt=0; % zero count for oneshot calc of centroid after initial location of spot during Src1
if stopSearch(r,c)==false
% Set Range and Dither********
if intensPrev(r,c)< earlySrc1Thres %First Search parameters insert
rRangeUpper=rCRangeUpper;
rRangeLower=rCRangeLower;
cRangeLower=cCRangeLower;
cRangeUpper=cCRangeUpper;
dither=ditherI;
[searchStartRef]=detPos{r,c};
elseif intensPrev(r,c)> earlySrc1Thres &&intensPrev(r,c)<= earlySrc2Thres %Second Search parameters insert
rRangeUpper=-CSrchRng; % rCRangeUpper;
rRangeLower=CSrchRng; % rCRangeLower;
cRangeLower=-CSrchRng; % cCRangeLower;
cRangeUpper=CSrchRng; % cCRangeUpper;
dither=ditherC;
% src1cnt=src1cnt+1;
% After spot defined above selected threshold, start search from
% center of the calc'd spot area
% Calc. Center of Area (black and white (1's and 0's)
% detPrLo=(detPos{r,c}(1)) -18; detPrHi=(detPos{r,c}(1)) +width+18;
% detPcLo=(detPos{r,c}(2)) -18; detPcHi=(detPos{r,c}(2)) +width+18;
detPrLo=(detPos{r,c}(1)) -CSrchRng; detPrHi=(detPos{r,c}(1)) +width+CSrchRng;
detPcLo=(detPos{r,c}(2)) -CSrchRng; detPcHi=(detPos{r,c}(2)) +width+CSrchRng;
brsum=(sum(bwIntsc(detPrLo:detPrHi,detPcLo:detPcHi),2));
bcsum=(sum(bwIntsc(detPrLo:detPrHi,detPcLo:detPcHi),1));
bRvec=detPrLo:detPrHi;
bCvec=detPcLo:detPcHi;
bcentR=round((sum(bRvec .*brsum')) /sum(brsum));
bcentC=round((sum(bCvec .*bcsum)) /sum(bcsum));
bcent{r,c}=[bcentR, bcentC];
cornerOfset=ceil((0.5*width)+CSrchRng); %ceil(0.5*(width+(2*CSrchRng)));
refCorner{r,c}=[(bcentR-cornerOfset),(bcentC-cornerOfset)];
%NAN blowup preventive
if sum(brsum)>5 && sum(bcsum)>5
% [searchStartRef]=cell2mat(bcent(r,c));
[searchStartRef]=cell2mat(refCorner(r,c));
else
[searchStartRef]=detPos{r,c};
end
elseif intensPrev(r,c) >earlySrc2Thres && intensPrev(r,c)<= definedSpot
rRangeUpper=rFRangeUpper; %Fine Search parameters insert
rRangeLower=rFRangeLower;
cRangeLower=cFRangeLower;
cRangeUpper=cFRangeUpper;
dither=ditherF;
zz=lastDetMaxPos(r,c);
[searchStartRef]=zz{1,1};
end
% Fixed to Roam Switch
%{
if intensPrev(r,c)<=earlySrc1Thres
% [searchRef]=detPos{r,c}+ [rOff,cOff];
[searchStartRef]=detPos{r,c};
end
if intensPrev(r,c)> earlySrc1Thres && intensMax<earlySrc2Thres
% [searchRef]=detPos{r,c}+ [rOff,cOff];
[searchStartRef]=cell2mat(cent(r,c));
end
if intensPrev(r,c) >=earlySrc2Thres, %Let Roam
zz=lastDetMaxPos(r,c);
[searchStartRef]=zz{1,1};
end
%}
extent=CSrchRng;
Rmin=InitR-extent; Rmax=InitR+extent;
Cmin=InitC-extent; Cmax=InitC+extent;
% clear cutout %just found 20_0327
cutout=[];
for rOff=rRangeUpper:dither:rRangeLower
for cOff=cRangeLower:dither:cRangeUpper
[searchRef]=searchStartRef + [rOff,cOff];
refPtR=searchRef(1);
refPtC=searchRef(2);
if refPtR<Rmin,refPtR=Rmin;end
if refPtR>Rmax,refPtR=Rmax;end
if refPtC<Cmin,refPtC=Cmin;end
if refPtC>Cmax,refPtC=Cmax;end
% Do Circle analysis
if doCircle==1
% cutout=fIntsc(refPtR+1:(refPtR+(diaExt)),(refPtC+1:(refPtC+diaExt)));
try
cutout=fIntsc(refPtR:(refPtR+(diaExt-1)),refPtC:(refPtC+(diaExt-1)));
catch
tPt;
r;
c;
end
Imcir=cutout .* uint8(cirMask);
searchIntens=sum(sum(Imcir(1:(diaExt-1),1:(diaExt-1))));
else %standard square analysis area
refPtRExt=refPtR+widthEx;
if (refPtRExt)> size(fIntsc,1), refPtRExt=size(fIntsc,1); end
refPtCExt=refPtC+widthEx;
if (refPtCExt)> size(fIntsc,2), refPtCExt=size(fIntsc,2); end
searchIntens=sum(sum(fIntsc(refPtR:(refPtRExt),refPtC:(refPtCExt))));
end
if searchIntens > intensMax
intensMax=searchIntens;
intens(r,c)=searchIntens;
intensPrev(r,c)=searchIntens;
a=[refPtR,refPtC];
detMaxPos(r,c)={a};
lastDetMaxPos(r,c)=detMaxPos(r,c);
end
if intensMax>definedSpot || intensMax<2
stopSearch(r,c)=true;
end
end
end
if intensMax< noSpot
intensMax=zeroSearchIntens;
lastDetMaxPos(r,c)={refPt};
end
else % elseif Spot is well defined STOPSearch strong
[refPt]=lastDetMaxPos{r,c};
refPtR=refPt(1);
refPtC=refPt(2);
if doCircle==1
cutout=fIntsc(refPtR:(refPtR+(diaExt-1)),refPtC:(refPtC+(diaExt-1)));
Imcir=cutout .* uint8(cirMask);
intens(r,c)=sum(sum(Imcir(1:(diaExt-1),1:(diaExt-1))));
else %standard square analysis area
intens(r,c)=sum(sum(fIntsc(refPtR:(refPtR+widthEx),refPtC:(refPtC+widthEx))));
end
end
%{
if intensMax> earlySrc1Thres && intensMax< earlySrc2Thres && src1cnt==0,
%{
src1cnt=src1cnt+1;
detPrLo=(detPos{r,c}(1)) -12; detPrHi=(detPos{r,c}(1)) +width+12;
detPcLo=(detPos{r,c}(2)) -12; detPcHi=(detPos{r,c}(2)) +width+12;
rsum=(sum(fIntsc(detPrLo:detPrHi,detPcLo:detPcHi),2));
csum=(sum(fIntsc(detPrLo:detPrHi,detPcLo:detPcHi),1));
Rvec=detPrLo:detPrHi;
Cvec=detPcLo:detPcHi;
centR=round((sum(Rvec .*rsum')) /sum(rsum));
centC=round((sum(Cvec .*csum)) /sum(csum));
cent{r,c}=[centR, centC];
%}
end
%}
end
end
% [F_spots,bmtp, BGthres,bmm,rwm,lstTpt,BGsc,totBkgrd]=...
% NIgenBkGrdDataPar4Fnc(ImParMat, tifFileLst, lastDetMaxPos, numRows, numCols, fullsc, tPt,BGthres, doCircle, cirPixA,diaExt, cirMask, BGTav);
% NIgenBkGrdDataPar4Fnc
% function [F_spots,bmtp, BGthres,bmm,rwm,lstTpt,BGsc,totBkgrd ]=...
% NIgenBkGrdDataPar4Fnc(ImParMat, tifFileLst, lastDetMaxPos, numRows, numCols, fullsc, tPt, BGthres, doCircle, cirPixA, diaExt, cirMask, BGTav)
% Called by NIscanIntensBG.m
MPnum=ImParMat(1);
destPerMP=ImParMat(2);
spotThres=ImParMat(4)/100; %selScan=ImParMat(4);
width=ImParMat(5);
dither=ImParMat(6);
BGthresInput=ImParMat(3); % a value of percent to be added (20=>1.20*BGTav)
lstTpt=length(tifFileLst);
LfOffset=65; %lf; %Ncode
TopOffset=65; %tp;
rtLim=1350; %1400; %1350;
rtNotchOffset=10;
cushion=20;
% close2edgeTol=90;
botLim=2003;
botNotchOffset=60;
% Initialize
% clear('totBkgrd')
% clear('pixsAboveBG')
% clear('lineV','pixCnt','lineSum,meanBkgrd')
% clear('rw','rwm')
totBkgrd=0;
pixsAboveBG=0;
lineV=0; pixCnt=0; lineSum=0; meanBkgrd=85;
rw=0; rwm=0;
% Start Generation of BackGround data after the 2nd Row (&& 2nd Col)
% Optimized spot positions determined
%if r>=2 && c>=2,
BGsc=zeros(2075,1400); %Empsc;
widthEx=width-1; %width extention from reference point
for r=1:numRows
for c=1:numCols
refP=lastDetMaxPos{r,c};
if r<numRows,refNxR=lastDetMaxPos{r+1,c}; end
if c<numCols,refNxC=lastDetMaxPos{r,c+1}; end
if r>1, refPrR=lastDetMaxPos{r-1,c}; end
if c>1, refPrC=lastDetMaxPos{r,c-1}; end
% At least NINE conditions which must be treated differently
% Condition 0: most spots fall in this central category
if r>1 && r<numRows && c>1 && c<numCols
if rem(r,numRows)==1
refBG(1)=refP(1)-floor(0.5*(plateBotSpace));
else
refBG(1)=refP(1)-floor(0.5*(refP(1)-(refPrR(1)+widthEx)));
end
if rem(c,numCols)==1
refBG(2)=refP(2)-floor(0.5*(plateEdgeSpace));
else
refBG(2)=refP(2)-floor(0.5*(refP(2)-(refPrC(2)+widthEx)));
end
if rem(r,numRows)==0
refBG(3)=refP(1)+widthEx+ floor(0.5*(plateBotSpace));
else
refBG(3)=refP(1)+widthEx+ floor(0.5*(refNxR(1)-(refP(1)+widthEx)));
end
if rem(c,numCols)==0
refBG(4)=refP(2)+widthEx+ floor(0.5*(plateEdgeSpace));
else
refBG(4)=refP(2)+widthEx+ floor(0.5*(refNxC(2)-(refP(2)+widthEx)));
end
%refBG(4)=refP(2)+widthEx+ floor(0.5*(refNxC(2)-refP(2)+widthEx));
detBG(r,c)={uint8(refBG)}; %Store Background Reference pt data in a Cell
% clear('lineV','pixCnt','lineSum')
lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
if tPt==1 || tPt==lstTpt,rw=lineV';end
pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV')
lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); % mean down lf
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV')
lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); % mean down rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV')
lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); % mean across bot rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
if doCircle==1
totBkgrd(r,c)=meanBkgrd* cirPixA;
else
totBkgrd(r,c)=meanBkgrd* width^2;
end
%{
lineSum(1)=sum((fullsc(refBG(1),refBG(2):refBG(4)-1))); %mean across top rt
lineSum(2)=sum((fullsc(refBG(1):refBG(3)-1,refBG(2)))); %mean down lf
lineSum(3)=sum((fullsc(refBG(1):refBG(3)-1,refBG(4)))); %mean down rt
lineSum(4)=sum((fullsc(refBG(3),refBG(2):refBG(4)-1))); %mean across bot rt
meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
totBkgrd(r,c)=meanBkgrd* width^2;
%}
end
% Condition 1
if r==1 && c==1
% if (size(refP(1)-4)>0
refBG(1)=refP(1)- floor(0.5*(refP(1) - TopOffset));
refBG(2)=refP(2)- floor(0.5*(refP(2) - LfOffset)); % Icode -0
refBG(3)=refP(1)+widthEx+ floor(0.5*((refNxR(1)-(refP(1)+widthEx))));
refBG(4)=refP(2)+widthEx+ floor(0.5*((refNxC(2)-(refP(2)+widthEx))));
detBG(r,c)={uint8(refBG)}; % Store Background Reference pt data in a Cell
% clear('lineV','pixCnt','lineSum')
lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); %mean across top rt
if tPt==1 || tPt==lstTpt,rw=lineV';end
pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV')
lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); %mean down lf
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV')
lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); %mean down rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV')
lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); %mean across bot rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
% meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
if doCircle==1
totBkgrd(r,c)=meanBkgrd* cirPixA;
else
totBkgrd(r,c)=meanBkgrd* width^2;
end
%{
lineSum(1)=sum((fullsc(refBG(1),refBG(2):refBG(4)-1))); %mean across top rt
lineSum(2)=sum((fullsc(refBG(1):refBG(3)-1,refBG(2)))); %mean down lf
lineSum(3)=sum((fullsc(refBG(1):refBG(3)-1,refBG(4)))); %mean down rt
lineSum(4)=sum((fullsc(refBG(3),refBG(2):refBG(4)-1))); %mean across bot rt
meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
totBkgrd(r,c)=round(meanBkgrd* width^2);
%}
end
% Condition 1a
if r==1 && c>1 && c<numCols
refBG=zeros(1,4);
refBG(1)=refP(1)-floor(0.5*(refP(1)-TopOffset));
if rem(c,numCols)==1
refBG(2)=refP(2)-floor(0.5*(plateEdgeSpace));
else
refBG(2)=refP(2)-floor(0.5*(refP(2)-(refPrC(2)+widthEx)));
end
refBG(3)=refP(1)+widthEx+ floor(0.5*(refNxR(1)-(refP(1)+widthEx)));
if rem(c,numCols)==0
refBG(4)=refP(2)+widthEx+ floor(0.5*(plateEdgeSpace));
else
refBG(4)=refP(2)+widthEx+ floor(0.5*(refNxC(2)-(refP(2)+widthEx)));
end
detBG(r,c)={uint8(refBG)}; %Store Background Reference pt data in a Cell
% clear('lineV','pixCnt','lineSum')
lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
if tPt==1 || tPt==lstTpt,rw=lineV';end
pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV')
lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); %mean down lf
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV')
lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); %mean down rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV')
lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); %mean across bot rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
if doCircle==1
totBkgrd(r,c)=meanBkgrd* cirPixA;
else
totBkgrd(r,c)=meanBkgrd* width^2;
end
%{
lineSum(1)=sum((fullsc(refBG(1),refBG(2):refBG(4)-1))); %mean across top rt
lineSum(2)=sum((fullsc(refBG(1):refBG(3)-1,refBG(2)))); %mean down lf
lineSum(3)=sum((fullsc(refBG(1):refBG(3)-1,refBG(4)))); %mean down rt
lineSum(4)=sum((fullsc(refBG(3),refBG(2):refBG(4)-1))); %mean across bot rt
meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
totBkgrd(r,c)=meanBkgrd* width^2;
%}
end
% Condition 2
if r==1 && c==numCols
% close2edgeTol=14;
refBGcExt=refP(2)+ widthEx;
refBG(1)=refP(1)-floor(0.5*(refP(1)-TopOffset)); %vertical (row)
refBG(2)=refP(2)-floor(0.5*(refP(2)-(refPrC(2)+widthEx))); %horizontal(col)
refBG(3)=refP(1)+widthEx+ floor(0.5*((refNxR(1)-(refP(1)+widthEx))));
% if refBGcExt>(size(fullsc,2)-close2edgeTol)%if intens area too close to edge
if refBGcExt>(rtLim-cushion)%if intens area too close to edge
refBG(4)=rtLim-rtNotchOffset;
else
refBG(4)=refP(2)+widthEx+ floor(0.5*(rtLim-refBGcExt));
end
detBG(r,c)={uint8(refBG)}; %Store Background Reference pt data in a Cell
% clear('lineV','pixCnt','lineSum')
lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
if tPt==1 || tPt==lstTpt,rw=lineV';end
pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV')
lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); %mean down lf
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV')
lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); %mean across bot rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% if refBGcExt>(size(fullsc,2)-close2edgeTol) %if intens area too close to edge
if refBGcExt>(rtLim-cushion) %if intens area too close to edge
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
% meanBkgrd=(lineSum(1)+lineSum(2)+lineSum(4))./ ...
% ((2*(refBG(4)-refBG(2))+(refBG(3)-refBG(1)))); %divby 2*across+1*down
if doCircle==1
totBkgrd(r,c)=meanBkgrd* cirPixA;
else
totBkgrd(r,c)=meanBkgrd* width^2;
end
else
% clear('lineV') %3
lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); %mean down rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
% meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
if doCircle==1
totBkgrd(r,c)=meanBkgrd* cirPixA;
else
totBkgrd(r,c)=meanBkgrd* width^2;
end
end
if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
end
% Condition 2a: down rows 2->47 farRight col edge
if r>1 && r<numRows && c==numCols
% close2edgeTol=14; %Icode=4
refBGcExt=refP(2)+widthEx;
if rem(r,numRows)==1
refBG(1)=refP(1)-floor(0.5*(plateBotSpace));
else
refBG(1)=refP(1)-floor(0.5*(refP(1)-(refPrR(1)+widthEx)));
end
refBG(2)=refP(2)-floor(0.5*(refP(2)-(refPrC(2)+widthEx)));
if rem(r,numRows)==0
refBG(3)=refP(1)+widthEx+ floor(0.5*(plateBotSpace));
else
refBG(3)=refP(1)+widthEx+ floor(0.5*(refNxR(1)-(refP(1)+widthEx)));
end
if refBGcExt<(rtLim-cushion) %if intens area too close to edge
refBG(4)=refP(2)+widthEx+ floor(0.5*(rtLim-refBGcExt));
else
refBG(4)=rtLim-cushion;
end
detBG(r,c)={uint8(refBG)}; % store Background Reference pt data in a Cell
% clear('lineV','pixCnt','lineSum') %1
lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
if tPt==1 || tPt==lstTpt,rw=lineV';end
pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV') %2
lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); %mean down lf
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV') %4
lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); % mean across bot rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
%if refBGcExt<(size(fullsc,2)-close2edgeTol) % if intens area too close to edge
if refBGcExt<(rtLim-cushion) % if intens area too close to edge
% clear('lineV') %3
lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); % mean down rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
% meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
if doCircle==1
totBkgrd(r,c)=meanBkgrd* cirPixA;
else
totBkgrd(r,c)=meanBkgrd* width^2;
end
else
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
% meanBkgrd=(lineSum(1)+lineSum(2)+lineSum(4))./ ...
% ((2*(refBG(4)-refBG(2))+(refBG(3)-refBG(1)))); %divby 2*across+1*down
if doCircle==1
totBkgrd(r,c)=meanBkgrd* cirPixA
else
totBkgrd(r,c)=meanBkgrd* width^2
end
end
if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
end
% Condition 3: Lower right corner
if r==numRows && c==numCols
%close2edgeTol=14;
refBGrExt=refP(1)+ widthEx;
refBGcExt=refP(2)+ widthEx;
refBG(1)=refP(1)-floor(0.5*(refP(1)-(refPrR(1)+widthEx))); %vertical (row)
refBG(2)=refP(2)-floor(0.5*(refP(2)-(refPrC(2)+widthEx))); %horizontal(col)
if refBGrExt>(botLim-botNotchOffset)%if intens area not too close to bot edge else
refBG(3)=botLim + cushion;
else
refBG(3)=refP(1)+widthEx+ floor(0.5*(botLim-refBGrExt));
end
if refBGcExt>(rtLim- cushion)%if intens area not too close to edge else
refBG(4)=rtLim -cushion;
else
refBG(4)=refP(2)+widthEx+ floor(0.5*(rtLim-refBGcExt));
end
detBG(r,c)={uint8(refBG)}; %Store Background Reference pt data in a Cell
% clear('lineV','pixCnt','lineSum') %1
lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
if tPt==1 || tPt==lstTpt,rw=lineV';end
pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV') %2
lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); % mean down lf
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% if refBGcExt>(size(fullsc,2)-close2edgeTol)&&...
if refBGcExt>(rtLim-cushion)&&...
refBGrExt<(botLim-botNotchOffset) % if intens area too close to right edge only
% clear('lineV') %4
lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); % mean across bot rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
% meanBkgrd=(lineSum(1)+lineSum(2)+lineSum(4))./ ...
% ((2*(refBG(4)-refBG(2))+(refBG(3)-refBG(1)))); % divby 2*across+1*down
if doCircle==1
totBkgrd(r,c)=meanBkgrd* cirPixA;
else
totBkgrd(r,c)=meanBkgrd* width^2;
end
elseif refBGrExt>(botLim-botNotchOffset) && ...
refBGcExt>(rtLim-cushion)%if intens area too close to bottom edge only
% clear('lineV') %3
lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); % mean down rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end % meanBkgrd=(lineSum(1)+lineSum(2)+lineSum(3))./ ...
%((refBG(4)-refBG(2)+(2*(refBG(3)-refBG(1))))); % divby 1*across+2*down
if doCircle==1, totBkgrd(r,c)=meanBkgrd* cirPixA; else totBkgrd(r,c)=meanBkgrd* width^2; end
% elseif refBGcExt>(size(fullsc,2)-close2edgeTol)&& ...
elseif refBGcExt>(rtLim-cushion)&& ...
refBGrExt>(botLim-botNotchOffset) % if intens area too close to both right edge and bottom edge
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
% meanBkgrd=(lineSum(1)+lineSum(2))./ ...
% ((refBG(4)-refBG(2)+(refBG(3)-refBG(1)))); %divby 1across+1*down
if doCircle==1, totBkgrd(r,c)=meanBkgrd* cirPixA; else totBkgrd(r,c)=meanBkgrd* width^2; end
else
% clear('lineV') %3
lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); %mean down rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV') %4
lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); %mean across bot rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
% meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
if doCircle==1
totBkgrd(r,c)=meanBkgrd* cirPixA;
else
totBkgrd(r,c)=meanBkgrd* width^2;
end
end
if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
end
% Condition 3a: Bottom across row48 col2-79
if r==numRows && c>1 && c<numCols
refBG=zeros(1,4);
% close2edgeTol=14; % Icode=4
refBGrExt=refP(1)+ widthEx;
refBG(1)=refP(1)-floor(0.5*(refP(1)-(refPrR(1)+widthEx))); % vertical (row)
if rem(c,numCols)==1
refBG(2)=refP(2)-floor(0.5*(plateEdgeSpace)); % horizontal(col)
else
refBG(2)=refP(2)-floor(0.5*(refP(2)-(refPrC(2)+widthEx))); % horizontal(col)
end
if refBGrExt>(botLim-botNotchOffset) % if intens area not too close to bot edge else
refBG(3)=botLim + cushion;
else
refBG(3)=refP(1)+widthEx+ floor(0.5*(botLim-refBGrExt));
end
if rem(c,numCols)==0
refBG(4)=refP(2)+widthEx+ floor(0.5*(plateEdgeSpace));
else
refBG(4)=refP(2)+widthEx+ floor(0.5*(refNxC(2)-(refP(2)+widthEx)));
%refBG(4)=refP(2)+widthEx+ floor(0.5*(size(fullsc,2)-refBGcExt));
end
detBG(r,c)={uint8(refBG)}; % Store Background Reference pt data in a Cell
% clear('lineV','pixCnt','lineSum') %1
lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
if tPt==1 || tPt==lstTpt,rw=lineV';end
pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV') %2
lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); % mean down lf
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV') %3
lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); % mean down rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
if refBGrExt>(botLim-botNotchOffset) % if intens area too close to bottom edge
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
% meanBkgrd=(lineSum(1)+lineSum(2)+lineSum(4))./ ...
% ((refBG(4)-refBG(2)+(2*(refBG(3)-refBG(1))))); %divby 1*across+2*down
if doCircle==1, totBkgrd(r,c)=meanBkgrd* cirPixA; else totBkgrd(r,c)=meanBkgrd* width^2; end
else
% clear('lineV') %4
lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); % mean across bot rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
% meanBkgrd=(sum(lineSum(1:4)))./ ((2*(refBG(4)-refBG(2))+ 2*(refBG(3)-refBG(1))));
if doCircle==1, totBkgrd(r,c)=meanBkgrd* cirPixA; else totBkgrd(r,c)=meanBkgrd* width^2; end
end
if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
end
% Condition 4: Bottom Left Corner row48 col1
if r==numRows && c==1
%close2edgeTol=14; %Icode=4
refBGrExt=refP(1)+ widthEx;
refBG(1)=refP(1)-floor(0.5*(refP(1)-(refPrR(1)+widthEx))); % vertical (row)
refBG(2)=refP(2)- floor(0.5*(refP(2) - LfOffset)); % horizontal(col) Icode -0 instead of LfOffset
if refBGrExt>(botLim-botNotchOffset) % if intens area not too close to bot edge else
refBG(3)=botLim + cushion;
else
refBG(3)=refP(1)+widthEx+ floor(0.5*(botLim-refBGrExt));
end
refBG(4)=refP(2)+widthEx+ floor(0.5*(refNxC(2)-(refP(2)+widthEx)));
detBG(r,c)={uint8(refBG)}; % Store Background Reference pt data in a Cell
% clear('lineV','pixCnt','lineSum') %1
lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
if tPt==1 || tPt==lstTpt,rw=lineV';end
pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV') %2
lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); % mean down lf
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV') %3
lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); % mean down rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
if refBGrExt>(botLim-botNotchOffset) % if intens area too close to bottom edge
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
% meanBkgrd=(lineSum(1)+lineSum(2)+lineSum(3))./ ...
% ((refBG(4)-refBG(2)+(2*(refBG(3)-refBG(1))))); % divby 1*across+2*down
if doCircle==1, totBkgrd(r,c)=meanBkgrd* cirPixA; else totBkgrd(r,c)=meanBkgrd* width^2; end
else
% clear('lineV') %4
lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); %mean across bot rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
% meanBkgrd=(sum(lineSum(1:4)))./ ((2*(refBG(4)-refBG(2))+ 2*(refBG(3)-refBG(1))));
if doCircle==1
totBkgrd(r,c)=meanBkgrd* cirPixA;
else
totBkgrd(r,c)=meanBkgrd* width^2;
end
end
if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
end
% Condition 4a: Left edge center area rows2-47 col1
if r>1 && r<numRows && c==1
% close2edgeTol=14; %Icode=4
refBGrExt=refP(1)+ widthEx;
if rem(r,numRows)==1
refBG(1)=refP(1)-floor(0.5*(plateBotSpace)); % vertical (row)
else
refBG(1)=refP(1)-floor(0.5*(refP(1)-(refPrR(1)+widthEx))); % vertical (row)
end
refBG(2)=refP(2)- floor(0.5*(refP(2) - LfOffset)); % horizontal(col) %Icode=0
if rem(r,numRows)==0
refBG(3)=refP(1)+widthEx+ floor(0.5*(plateBotSpace));
else
refBG(3)=refP(1)+widthEx+ floor(0.5*(refNxR(1)-(refP(1)+widthEx)));
end
refBG(4)=refP(2)+widthEx+ floor(0.5*(refNxC(2)-(refP(2)+widthEx)));
detBG(r,c)={uint8(refBG)}; % Store Background Reference pt data in a Cell
% clear('lineV','pixCnt','lineSum') %1
lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
if tPt==1 || tPt==lstTpt,rw=lineV';end
pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV') %2
lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); %mean down lf
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV') %3
lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); %mean down rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
% clear('lineV') %4
lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); %mean across bot rt
if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
if doCircle==1
totBkgrd(r,c)=meanBkgrd* cirPixA;
else
totBkgrd(r,c)=meanBkgrd* width^2;
end
end
% Determine the actual number of pixels(area) of growth above the background
% Intensity
BG=meanBkgrd;
[refPtPxAbove]=lastDetMaxPos{r,c};
refPtRx=refPtPxAbove(1);
refPtCx=refPtPxAbove(2);
if doCircle==1
cutout=(fullsc(refPtRx:(refPtRx+ (diaExt-1)),refPtCx:(refPtCx+ (diaExt-1))));
Imcir=cutout .* uint8(cirMask);
pixsAboveBG(r,c)=nnz(Imcir>BG); %calc non-background within circular analysis area
% pixsAboveBG(r,c)=nnz(fullsc(refPtRx:(refPtRx+ (diaExt-1)),refPtCx:(refPtCx+ (diaExt-1)))>BG); %BG);
else % standard square analysis area
pixsAboveBG(r,c)=nnz(fullsc(refPtRx:(refPtRx+widthEx),refPtCx:(refPtCx+widthEx))>BG); % calc non-background within square analysis area
end
% TimePT average
% meanBGTsum(r,c)=meanBGTsum(r,c)+meanBkgrd;
% meanBGTav(r,c)=meanBGTsum(r,c) /tPt;
bmm(r,c)=uint8(floor(meanBkgrd));
% For FOTOS only
F_spots(r,c,tPt)={refBG}; % Foto spot coordinates
bmtp(r,c,tPt)=bmm(r,c); % Added/used for FOTOS 11_0830
% TimePT Average BG intensity calc method
BGTav(r,c)=uint8(round((BGTav(r,c)*(tPt-1))+meanBkgrd)/tPt);
% BGTav(r,c)=((BGTav(r,c)*(tPt-1))+meanBkgrd)/tPt;
% sliding BG average
% if r==1 && c==1, slavg(1:5)=BG;end % slavg(1:5)=50; %120620 Epsonvalue is 33; end
% slavg(5)=slavg(4);
% slavg(4)=slavg(3);
% slavg(3)=slavg(2);
% slavg(2)=slavg(1);
% slavg(1)=BG;
% slidingAvg=mean(slavg);
% For Visualization Purposes only
%1->2 across rt 1, 2-4
BGsc(refBG(1),refBG(2):refBG(4))=1;
%1->4 down lf 1-3, 2
BGsc(refBG(1):refBG(3),refBG(2))=1;
%2->3 down rt 1-3, 4
BGsc(refBG(1):refBG(3),refBG(4))=1;
%4->3 Bot Across rt 1, 2-4
BGsc(refBG(3),refBG(2):refBG(4))=1;
% BGsc(refBG(1):refBG(3),refBG(2):refBG(4)) %just displays matrices
end
end
BGthres=uint8(double(BGTav) * (1+(BGthresInput/100)));
optomizedPos(:,:,tPt)=lastDetMaxPos;
% if(isequal(SWsingleSc,1))|| ((~isequal(SWsingleSc,1))&&tPt==tptLength) %190731 change to capture last timePt PTmap if(isequal(SWsingleSc,1))
% if(isequal(SWsingleSc,1))&& (tPt==1 || tPt==guesTpt || tPt==tptLength)|| ((~isequal(SWsingleSc,1))&&tPt==tptLength) %190919 Option for observation
% if (tPt==1 || tPt==guesTpt || tPt==tptLength) %190919 Option for observation
if (tPt==tptLength) % 190919 change to capture ONLY last timePt PTmap
% clear OptmapOnes;
OptmapOnes=[];
% OptmapOnes=Empsc+ 1.4;
OptmapOnesDbl=double(Empsc)+ .8;
for r=1:numRows
for c=1:numCols
[refPt]=lastDetMaxPos{r,c};
refPtR=refPt(1);
refPtC=refPt(2);
if doCircle==1
% OptmapOnesDbl(refPtR:(refPtR+diaExt),refPtC:(refPtC+diaExt))=cirMask;
OptmapOnesDbl(refPtR:(refPtR+(diaExt-1)),refPtC:(refPtC+(diaExt-1)))=optCirMask;
else
refPtRExt=refPtR+widthEx;
if (refPtRExt)> size(fullsc,1), refPtRExt=size(fullsc,1); end
refPtCExt=refPtC+widthEx;
if (refPtCExt)> size(fullsc,2), refPtCExt=size(fullsc,2); end
OptmapOnesDbl(refPtR:(refPtRExt),refPtC:(refPtCExt))=1;%255;
end
end
end
% Cross-optimized Map Matrix onto tifFile Matrix
% For visualization purposes only
if tPt==1,figure;end
resIm=uint8((double(BGsc) + OptmapOnesDbl) .* double(fullsc));
% clear('OptmapOnesDbl')
OptmapOnesDbl=[];
% hres=imagesc(resIm);
hfscanIm=imagesc(resIm);
hfIm=gcf;
haxis=gca;
title(strcat('Scan',num2str(selScan),'timePt-',num2str(tPt)));
clf(hfscanIm,'reset');
set(hfIm,'NumberTitle','off')
set(hfIm,'Name', char(matFile))
if Fflg==1
fullfile(fotosResultsDir,strcat('Scan',num2str(selScan),'_timePt-',num2str(tPt)))
hgsave(fullfile(fotosResultsDir,strcat('Scan',num2str(selScan),'_timePt-',num2str(tPt)))); %F 14_0626
else
hgsave(fullfile(figsResultsDir,strcat('Scan',num2str(selScan),'_timePt-',num2str(tPt))));
end
else
selScan;
% tifFile
end
BkgrdMat=totBkgrd; % BkgrdMat(:,:,tPt)=totBkgrd; %debug parfor effort
areaOfIntensAboveBG(:,:,tPt)=pixsAboveBG; % actual area of growth for each timepoint (used for printout of grArea ?not used in previous analysis)
scanIntens(:,:,tPt)=intens-BkgrdMat;
% Capture first and last background values
if tPt==1
bdg(:,:,1)=bmm;
bdg(:,:,3)=rwm;
Tmpsbdg1={bdg};
end
if tPt==lstTpt
bdg(:,:,2)=bmm;
bdg(:,:,4)=rwm;
Tmpsbdg1={bdg};
end
TmpexpScanIntens1={scanIntens}; % par4Gbl 20_0122
TmpFexpScanSpots1={F_spots};
TmpFexpScanBMtp1={bmtp};
TmpanlZoneRefs1={optomizedPos}; % added to store tPt by tPt positions
TmpexpScanIntens2=TmpexpScanIntens1;
TmpFexpScanSpots2=TmpFexpScanSpots1;
TmpFexpScanBMtp2=TmpFexpScanBMtp1;
TmpanlZoneRefs2=TmpanlZoneRefs1;
Tmpsbdg2=Tmpsbdg1;
end
end