NIscanIntensBGpar4GblFnc.m 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. %% CALLED BY par4GblFnc8c.m %%
  2. function [Tmpsbdg2, scanIntens, F_spots, bmtp, optomizedPos, TmpexpScanIntens2, TmpFexpScanSpots2, TmpFexpScanBMtp2, TmpanlZoneRefs2,areaOfIntensAboveBG]= ...
  3. NIscanIntensBGpar4GblFnc(Fflg, tifFileLst, ImParMat,PTmapPos,optCirMask,diaExt,doCircle,cirPixA,numRows,numCols,ImHeigth,ImWidth,cirMask, ...
  4. tptLength,selScan,Empsc,~, ~, ~, ~,easyResultsDir, Tmpsbdg1)
  5. global matFile
  6. global CSrchRng
  7. global figsResultsDir
  8. global fotosResultsDir
  9. searchIntens=[];intensMax=[];detMaxPos=[];scIntens=[];areaOfIntensAboveBG=[];BkgrdMat=[];
  10. % TODO needs explanation
  11. if Fflg==1
  12. % Preallocation
  13. tPtLength=length(tifFileLst);
  14. optomizedPos=cell(24,16,tPtLength);
  15. F_spots=cell(24,16,tPtLength); % Foto spot coordinates Preallocation
  16. bmtp=zeros(24,16,tPtLength); % Added/used for FOTOS Preallocation
  17. MPnum=ImParMat(1);
  18. destPerMP=ImParMat(2);
  19. spotThres=ImParMat(4)/100;
  20. % width=ImParMat(5);
  21. CSrchRng=ImParMat(12);
  22. width=24; % absolute fixed after Sept 2014
  23. ditherF=ImParMat(6);
  24. ditherF=1; % absolute fixed after Sept 2014
  25. % Get PTmap data (detPos) and create PTimage with cirMask 2019_0904
  26. PTmapOnesDbl=zeros(2075,1400);
  27. for r=1:24
  28. for c=1:16
  29. [PTrefPt]=PTmapPos{r,c};
  30. PTrefPtR=PTrefPt(1);
  31. PTrefPtC=PTrefPt(2);
  32. % doCircle
  33. PTmapOnesDbl(PTrefPtR:(PTrefPtR+(diaExt-1)),PTrefPtC:(PTrefPtC+(diaExt-1)))=optCirMask;
  34. end
  35. end
  36. end
  37. % Zeroth Initial Search Parameters
  38. rRangeUpper=-12;
  39. rRangeLower=12;
  40. cRangeLower=-12;
  41. cRangeUpper=12;
  42. ditherI=width/3;
  43. searchRange=floor(width*0.75); %generally -/+18
  44. %searchRange=CSrchRng
  45. rCRangeUpper=-searchRange; % -12 % -8; %Ranges should be multiples of dither
  46. rCRangeLower=searchRange; %12; %Ranges should be multiples of dither
  47. cCRangeLower=-searchRange; %-12 %-8; %-12; %Ranges should be multiples of dither
  48. cCRangeUpper=searchRange; %12; %8; %Ranges should be multiples of dither
  49. ditherC=floor((2*searchRange)/9); %gnerally /6 0r /9 -> dither=6 or 4 pixels
  50. if ditherC==0,ditherC=1;end
  51. rFRangeUpper=-floor(1/2*width); %(searchRange*2/3) %-12 % -8; %Ranges should be multiples of dither
  52. rFRangeLower=floor(1/2*width); %(searchRange*2/3); %12; %Ranges should be multiples of dither
  53. cFRangeLower=-floor(1/2*width); %(searchRange*2/3); %-12 %-8; %-12; %Ranges should be multiples of dither
  54. cFRangeUpper=floor(1/2*width); %(searchRange*2/3) %12; %8; %Ranges should be multiples of dither
  55. ditherF=1;
  56. % The purpose is to reduce incidence of 'chasing' scratches and abrasions to
  57. % the edge of a frame and also to limit late timept finds of overgrown
  58. % spot(that are yet small). The idea is to make the threshold high enough to
  59. % prevent the tracking of scratches and small contaminants(overgrowth) while
  60. % not limiting detection of small slow grow cultures. Recommend value
  61. % slightly above the expected background level.
  62. noSpotThres=60; % If PixelIntensity for area < area*this, use the Orig.search Refpt
  63. if doCircle==1
  64. noSpot=cirPixA*noSpotThres;
  65. else
  66. noSpot=(width^2)*noSpotThres; %Area * (a selected background pixel level i.e., 70)
  67. end
  68. % Begin Time series loop
  69. % Preallocation
  70. stopSearch=zeros(numRows,numCols); %set all spots 'stopSearch' to false
  71. intens=zeros(24,16);
  72. intensPrev=zeros(24,16);
  73. cent=cell(24,16);
  74. BGTav=zeros(numRows,numCols);
  75. % 7 0;before incr.to95 to accommodate dark media %50forEpson;%Initialize for first tPt run
  76. BGthres(1:numRows,1:numCols)=95;
  77. if length(tifFileLst)>2
  78. % Determine a good Plate Image from all the timepoints to use for Registration
  79. % Plate Intensity curve over time points
  80. % clear plateImage
  81. plateImage={};
  82. %{
  83. % Version compatability fixes
  84. v11a='7.12.0.635 (R2011a)';
  85. v14a='8.3.0.532 (R2014a)';
  86. %v14b='8.4.0.150421 (R2014b)';
  87. v18a='9.4.0.813654 (R2018a)';
  88. v18b='9.5.0.944444 (R2018b)';
  89. pool=2;
  90. if isequal(v11a,version)|| isequal(v14a,version)
  91. if matlabpool('size')==0
  92. matlabpool(pool)
  93. end
  94. else
  95. if parpool('local')==0
  96. parpool('local', pool)
  97. end
  98. end
  99. %}
  100. % parfor iii=1:length(tifFileLst)
  101. for iii=1:length(tifFileLst)
  102. tifFile=char(tifFileLst(iii));
  103. fullplt=imread(tifFile); % Ncode actually reading a .bmp file for Ncode
  104. fullpl=255 - fullplt(:,:,1);
  105. %fullpl=(fullplt(1:ImHeigth,1:ImWidth));
  106. pltInts(iii)=sum(sum(fullpl(:,:)))/(ImHeigth*ImWidth);
  107. plateImage{iii}=fullpl; % 190919 to use instead of re-reading in NIscanIntensBG.m
  108. end
  109. guessOffsetAdj=0.5; %Half(0.5) is the midpoint intensity
  110. guessOffset=(max(pltInts)- min(pltInts)) * guessOffsetAdj; %Could plug-in a curvefit routine to determine an 'L' value
  111. guessVal=max(pltInts)- guessOffset;
  112. guesTpt=find(pltInts > guessVal,1,'first');
  113. if isempty(guesTpt)
  114. guesTpt=1;
  115. end
  116. PtptIm=tifFileLst(guesTpt);
  117. PtptImg=char(tifFileLst(guesTpt));
  118. ImageRd=imread(PtptImg); %Ncode actually reading a .bmp file for Ncode
  119. PltImg=(ImageRd(1:ImHeigth,1:ImWidth));
  120. PltImg=255 - PltImg;
  121. % Convert to image to BW then AND with PTmapOnesDbl
  122. GraythrShift=1;
  123. BWimage=im2bw(PltImg, GraythrShift*graythresh(PltImg));
  124. matchAnded=BWimage & PTmapOnesDbl;
  125. initPTANDed=matchAnded;
  126. ANDValInitPT=sum(sum(initPTANDed(:,:)));
  127. % Begin Image Registration for Pintool
  128. MaxANDVal=0; % sum(sum(BWimage(:,:))); %seed MaxANDVal from the BWimage
  129. regDith=4;
  130. TestmapOnesDbl=zeros(2075,1400);
  131. % Create a Search Range centered on C==1 and R==1 of the PinTool map
  132. RtopBndry=96; RbotBndry=176;
  133. ClfBndry=96; CrtBndry=154;
  134. [PTrefPt]=PTmapPos{1,1};
  135. PTrefPtR=PTrefPt(1);
  136. PTrefPtC=PTrefPt(2);
  137. rngUpper=RtopBndry - PTrefPtR;
  138. quotT=round(abs(rngUpper)/regDith);
  139. topRang=regDith * quotT;
  140. if rngUpper<0, topRang=-1*topRang; end
  141. rngLower=RbotBndry - PTrefPtR;
  142. quotB=round(abs(rngLower)/regDith);
  143. botRang=regDith * quotB;
  144. if rngLower<0, botRang=-1*botRang; end
  145. rngLeft=ClfBndry - PTrefPtC;
  146. quotLf=round(abs(rngLeft)/regDith);
  147. leftRang=regDith * quotLf;
  148. if rngLeft<0, leftRang=-1*leftRang; end
  149. rngRight=CrtBndry - PTrefPtC;
  150. quotRt=round(abs(rngRight)/regDith);
  151. rightRang=regDith * quotRt;
  152. if rngRight<0, rightRang=-1*rightRang; end
  153. % Orthagonal Registration
  154. testPos=cell(24,16);
  155. for shiftPosR=topRang:regDith:botRang
  156. for shiftPosC=leftRang:regDith:rightRang
  157. TestmapOnesDbl=zeros(2075,1400);
  158. for r=1:24
  159. for c=1:16
  160. [PTrefPt]=PTmapPos{r,c};
  161. testPosR=PTrefPt(1) + shiftPosR;
  162. testPosC=PTrefPt(2) + shiftPosC;
  163. a=[testPosR,testPosC]; %testPos(r,c)={testPosR,testPosC};
  164. testPos(r,c)={a};
  165. doCircle;
  166. TestmapOnesDbl(testPosR:(testPosR+(diaExt-1)),testPosC:(testPosC+(diaExt-1)))=cirMask; %optCirMask;
  167. end
  168. end
  169. GraythrShift=1.0;
  170. BWtestimage=im2bw(TestmapOnesDbl, GraythrShift*graythresh(TestmapOnesDbl));
  171. matchAnded=BWimage & BWtestimage;
  172. ANDVal=sum(sum(matchAnded(:,:)));
  173. if ANDVal> MaxANDVal
  174. MaxANDVal=ANDVal;
  175. MaxPos=testPos;
  176. MaxPosDbl=TestmapOnesDbl;
  177. end
  178. end
  179. end
  180. if MaxANDVal>ANDValInitPT
  181. MaxPosDbl=MaxPosDbl;
  182. MaxPos=MaxPos;
  183. else
  184. MaxPosDbl=PTmapOnesDbl;
  185. MaxPos=PTmapPos;
  186. end
  187. % SKEW Registration
  188. testSkPos=MaxPos;
  189. MaxANDValSk=0; %MaxANDVal;
  190. skQty=0.05;
  191. regDith=4;
  192. for shiftPos=-regDith/2:regDith/2
  193. for k=-5:5
  194. sk=k* skQty;
  195. TestmapSkDbl=zeros(2075,1400);
  196. for r=1:24
  197. for c=1:16
  198. [skrefPt]=testSkPos{r,c};
  199. testSkPosR=round(skrefPt(1) + (c*sk + shiftPos));
  200. testSkPosC=round(skrefPt(2) + (-r*sk + shiftPos));
  201. a=[testSkPosR,testSkPosC]; % testPos(r,c)={testPosR,testPosC};
  202. testSkPos(r,c)={a};
  203. % doCircle
  204. TestmapSkDbl(testSkPosR:(testSkPosR+(diaExt-1)),testSkPosC:(testSkPosC+(diaExt-1)))=cirMask; % optCirMask;
  205. end
  206. end
  207. GraythrShift=1;
  208. BWtestSkimage=im2bw(TestmapSkDbl, GraythrShift*graythresh(TestmapSkDbl));
  209. matchAndedSk=BWimage & BWtestSkimage;
  210. ANDValSk=sum(sum(matchAndedSk(:,:)));
  211. if ANDValSk> MaxANDValSk
  212. MaxANDValSk=ANDValSk;
  213. MaxPosSk=testSkPos;
  214. MaxPosSkDbl=TestmapSkDbl;
  215. end
  216. end
  217. end
  218. if MaxANDValSk> MaxANDVal
  219. MaxANDVal=MaxANDValSk;
  220. MaxPosDbl=MaxPosSkDbl;
  221. MaxPos=MaxPosSk;
  222. end
  223. % Center of Area for Final Registration
  224. % Note GraythrShift in NImapPT can be used to a lower or higher 'decider'
  225. % and thus effect the centroid(center of area) that this module determines.
  226. % GraythrShift=1.0 normally allowing the Matlab graythresh to calc. the BW decider
  227. range=0;
  228. lastDetMaxCentDbl=zeros(2075,1400);
  229. lastDetMaxPos=cell(24,16);
  230. % CIRCLE related
  231. for r=1:24
  232. for c=1:16
  233. [refPt]=MaxPos{r,c};
  234. refPtR=refPt(1);
  235. refPtC=refPt(2);
  236. % Calc. Centroid Position of spot
  237. detPrLo=(MaxPos{r,c}(1)) -(range); detPrHi=(MaxPos{r,c}(1)) + (diaExt+range);
  238. detPcLo=(MaxPos{r,c}(2)) -(range); detPcHi=(MaxPos{r,c}(2)) + (diaExt+range);
  239. rsum=(sum(BWimage(detPrLo:detPrHi,detPcLo:detPcHi),2));
  240. csum=(sum(BWimage(detPrLo:detPrHi,detPcLo:detPcHi),1));
  241. if sum(rsum)>1 && sum(csum)>1
  242. Rvec=detPrLo:detPrHi;
  243. Cvec=detPcLo:detPcHi;
  244. centR=round((sum(Rvec .*rsum')) /sum(rsum));
  245. centC=round((sum(Cvec .*csum)) /sum(csum));
  246. aa=[(centR-diaExt/2) (centC-diaExt/2)];
  247. else
  248. rsum=(sum(MaxPosDbl(detPrLo:detPrHi,detPcLo:detPcHi),2));
  249. csum=(sum(MaxPosDbl(detPrLo:detPrHi,detPcLo:detPcHi),1));
  250. Rvec=detPrLo:detPrHi;
  251. Cvec=detPcLo:detPcHi;
  252. centR=round((sum(Rvec .*rsum')) /sum(rsum));
  253. centC=round((sum(Cvec .*csum)) /sum(csum));
  254. end
  255. aa=[(centR-diaExt/2) (centC-diaExt/2)];
  256. lastDetMaxPos(r,c)={aa};
  257. % 2nd Recenter Based on Previous
  258. detPrLo=lastDetMaxPos{r,c}(1) -(range); detPrHi=lastDetMaxPos{r,c}(1) + (diaExt+range);
  259. detPcLo=lastDetMaxPos{r,c}(2) -(range); detPcHi=lastDetMaxPos{r,c}(2) + (diaExt+range);
  260. rsum=(sum(BWimage(detPrLo:detPrHi,detPcLo:detPcHi),2));
  261. csum=(sum(BWimage(detPrLo:detPrHi,detPcLo:detPcHi),1));
  262. if sum(rsum)>1 && sum(csum)>1
  263. Rvec=detPrLo:detPrHi;
  264. Cvec=detPcLo:detPcHi;
  265. centR=round((sum(Rvec .*rsum')) /sum(rsum));
  266. centC=round((sum(Cvec .*csum)) /sum(csum));
  267. aa=[(centR-diaExt/2) (centC-diaExt/2)];
  268. else
  269. rsum=(sum(MaxPosDbl(detPrLo:detPrHi,detPcLo:detPcHi),2));
  270. csum=(sum(MaxPosDbl(detPrLo:detPrHi,detPcLo:detPcHi),1));
  271. Rvec=detPrLo:detPrHi;
  272. Cvec=detPcLo:detPcHi;
  273. centR=round((sum(Rvec .*rsum')) /sum(rsum));
  274. centC=round((sum(Cvec .*csum)) /sum(csum));
  275. end
  276. aa=[(centR-diaExt/2) (centC-diaExt/2)];
  277. lastDetMaxPos(r,c)={aa};
  278. [CentPt]=lastDetMaxPos{r,c};
  279. CentPtR=CentPt(1);
  280. CentPtC=CentPt(2);
  281. % doCircle
  282. lastDetMaxCentDbl(CentPtR:(CentPtR+(diaExt-1)),CentPtC:(CentPtC+(diaExt-1)))=cirMask; % optCirMask;
  283. end
  284. end
  285. BWtestCentimage=im2bw(lastDetMaxCentDbl, GraythrShift*graythresh(lastDetMaxCentDbl));
  286. matchAndedCent=BWimage & BWtestCentimage;
  287. ANDValCent=sum(sum(matchAndedCent(:,:)));
  288. if ANDValCent>=MaxANDVal
  289. MaxPosDbl=lastDetMaxCentDbl;
  290. MaxPos=lastDetMaxPos;
  291. % detPos=lastDetMaxPos;
  292. end
  293. % detPos=lastDetMaxPos;
  294. detPos=MaxPos;
  295. end
  296. % Preallocation for speed on 2019_1121 ***
  297. scIntens=zeros(24,16,length(tifFileLst));
  298. BkgrdMat=zeros(24,16,length(tifFileLst));
  299. % areaOfIntensAboveBG=zeros(24,16,length(tifFileLst));
  300. lastDetMaxPos=cell(numRows, numCols);
  301. for tPt=1:length(tifFileLst)
  302. tPt;
  303. tifFile=char(tifFileLst(tPt));
  304. fullsc=plateImage{tPt};
  305. fIntsc=fullsc;
  306. % Bad Image Filter Zone
  307. % NORMALLY turned off; This is Hand HARD CODE SPECIFIC to particular ExpJOB
  308. specificHardFilterCode=0;
  309. if specificHardFilterCode==1 && tPt==1
  310. % Plate scratch filter prototype could be added here
  311. fIntsc(:,1105:1190)=uint8(67); %Specific blocking area set to background level here
  312. % fIntsc(:,:)=uint8(67); % set entire image to background level if 1.bmp is
  313. % bad. This allows acquisiton of the initial "Print Time" from .bmp time stamp.
  314. end
  315. tp=90; bt=1935; lf=90; rt=1318; blkVal=10; %tp=90;
  316. fIntsc(1:tp,:)=uint8(blkVal); %top of blocking frame
  317. fIntsc(bt:ImHeigth,:)=uint8(blkVal); %bottom of blocking frame
  318. fIntsc(:,1:lf)=uint8(blkVal); %left side of blocking frame
  319. fIntsc(:,rt:ImWidth)=uint8(blkVal); %right side of blocking frame
  320. bwIntsc=im2bw(fIntsc,80/255);
  321. % Collect Intensities (Absorbances)
  322. % end for search for maximum intensity spot detection alignment
  323. if doCircle==1
  324. earlySrc1Thres=.31*cirPixA*255; % Initial search spot det. threshold
  325. earlySrc2Thres=spotThres*cirPixA*255; % Course search spot det. threshold
  326. 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))
  327. else
  328. earlySrc1Thres=.31*(width^2)*255; % Initial search spot det. threshold
  329. earlySrc2Thres=spotThres*(width^2)*255; % Course search spot det. threshold
  330. 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))
  331. end
  332. detMaxPos=cell(numRows, numCols);
  333. bcent=cell(numRows, numCols);
  334. for r=1:numRows
  335. for c=1:numCols
  336. [refPt]=detPos{r,c};
  337. refPtR=refPt(1); InitR=refPt(1);
  338. refPtC=refPt(2); InitC=refPt(2);
  339. % ZeroSearchIntens
  340. % clear ('searchIntens')
  341. searchIntens=[];
  342. if doCircle==1
  343. % cutout=fIntsc(refPtR+1:(refPtR+(diaExt)),(refPtC+1:(refPtC+diaExt)));
  344. cutout=fIntsc(refPtR:(refPtR+(diaExt-1)),refPtC:(refPtC+(diaExt-1)));
  345. Imcir=cutout .* uint8(cirMask);
  346. zeroSearchIntens=sum(sum(Imcir(1:(diaExt-1),1:(diaExt-1))));
  347. else % standard square analysis area
  348. refPtRExt=refPtR+widthEx;
  349. if (refPtRExt)> size(fIntsc,1), refPtRExt=size(fIntsc,1); end
  350. refPtCExt=refPtC+widthEx;
  351. if (refPtCExt)> size(fIntsc,2), refPtCExt=size(fIntsc,2); end
  352. zeroSearchIntens=sum(sum(fIntsc(refPtR:(refPtRExt),refPtC:(refPtCExt))));
  353. end
  354. % clear ('searchIntens')
  355. searchIntens=[];
  356. intensMax=-1;
  357. src1cnt=0; % zero count for oneshot calc of centroid after initial location of spot during Src1
  358. if stopSearch(r,c)==false
  359. % Set Range and Dither********
  360. if intensPrev(r,c)< earlySrc1Thres %First Search parameters insert
  361. rRangeUpper=rCRangeUpper;
  362. rRangeLower=rCRangeLower;
  363. cRangeLower=cCRangeLower;
  364. cRangeUpper=cCRangeUpper;
  365. dither=ditherI;
  366. [searchStartRef]=detPos{r,c};
  367. elseif intensPrev(r,c)> earlySrc1Thres &&intensPrev(r,c)<= earlySrc2Thres %Second Search parameters insert
  368. rRangeUpper=-CSrchRng; % rCRangeUpper;
  369. rRangeLower=CSrchRng; % rCRangeLower;
  370. cRangeLower=-CSrchRng; % cCRangeLower;
  371. cRangeUpper=CSrchRng; % cCRangeUpper;
  372. dither=ditherC;
  373. % src1cnt=src1cnt+1;
  374. % After spot defined above selected threshold, start search from
  375. % center of the calc'd spot area
  376. % Calc. Center of Area (black and white (1's and 0's)
  377. % detPrLo=(detPos{r,c}(1)) -18; detPrHi=(detPos{r,c}(1)) +width+18;
  378. % detPcLo=(detPos{r,c}(2)) -18; detPcHi=(detPos{r,c}(2)) +width+18;
  379. detPrLo=(detPos{r,c}(1)) -CSrchRng; detPrHi=(detPos{r,c}(1)) +width+CSrchRng;
  380. detPcLo=(detPos{r,c}(2)) -CSrchRng; detPcHi=(detPos{r,c}(2)) +width+CSrchRng;
  381. brsum=(sum(bwIntsc(detPrLo:detPrHi,detPcLo:detPcHi),2));
  382. bcsum=(sum(bwIntsc(detPrLo:detPrHi,detPcLo:detPcHi),1));
  383. bRvec=detPrLo:detPrHi;
  384. bCvec=detPcLo:detPcHi;
  385. bcentR=round((sum(bRvec .*brsum')) /sum(brsum));
  386. bcentC=round((sum(bCvec .*bcsum)) /sum(bcsum));
  387. bcent{r,c}=[bcentR, bcentC];
  388. cornerOfset=ceil((0.5*width)+CSrchRng); %ceil(0.5*(width+(2*CSrchRng)));
  389. refCorner{r,c}=[(bcentR-cornerOfset),(bcentC-cornerOfset)];
  390. %NAN blowup preventive
  391. if sum(brsum)>5 && sum(bcsum)>5
  392. % [searchStartRef]=cell2mat(bcent(r,c));
  393. [searchStartRef]=cell2mat(refCorner(r,c));
  394. else
  395. [searchStartRef]=detPos{r,c};
  396. end
  397. elseif intensPrev(r,c) >earlySrc2Thres && intensPrev(r,c)<= definedSpot
  398. rRangeUpper=rFRangeUpper; %Fine Search parameters insert
  399. rRangeLower=rFRangeLower;
  400. cRangeLower=cFRangeLower;
  401. cRangeUpper=cFRangeUpper;
  402. dither=ditherF;
  403. zz=lastDetMaxPos(r,c);
  404. [searchStartRef]=zz{1,1};
  405. end
  406. % Fixed to Roam Switch
  407. %{
  408. if intensPrev(r,c)<=earlySrc1Thres
  409. % [searchRef]=detPos{r,c}+ [rOff,cOff];
  410. [searchStartRef]=detPos{r,c};
  411. end
  412. if intensPrev(r,c)> earlySrc1Thres && intensMax<earlySrc2Thres
  413. % [searchRef]=detPos{r,c}+ [rOff,cOff];
  414. [searchStartRef]=cell2mat(cent(r,c));
  415. end
  416. if intensPrev(r,c) >=earlySrc2Thres, %Let Roam
  417. zz=lastDetMaxPos(r,c);
  418. [searchStartRef]=zz{1,1};
  419. end
  420. %}
  421. extent=CSrchRng;
  422. Rmin=InitR-extent; Rmax=InitR+extent;
  423. Cmin=InitC-extent; Cmax=InitC+extent;
  424. % clear cutout %just found 20_0327
  425. cutout=[];
  426. for rOff=rRangeUpper:dither:rRangeLower
  427. for cOff=cRangeLower:dither:cRangeUpper
  428. [searchRef]=searchStartRef + [rOff,cOff];
  429. refPtR=searchRef(1);
  430. refPtC=searchRef(2);
  431. if refPtR<Rmin,refPtR=Rmin;end
  432. if refPtR>Rmax,refPtR=Rmax;end
  433. if refPtC<Cmin,refPtC=Cmin;end
  434. if refPtC>Cmax,refPtC=Cmax;end
  435. % Do Circle analysis
  436. if doCircle==1
  437. % cutout=fIntsc(refPtR+1:(refPtR+(diaExt)),(refPtC+1:(refPtC+diaExt)));
  438. try
  439. cutout=fIntsc(refPtR:(refPtR+(diaExt-1)),refPtC:(refPtC+(diaExt-1)));
  440. catch
  441. tPt;
  442. r;
  443. c;
  444. end
  445. Imcir=cutout .* uint8(cirMask);
  446. searchIntens=sum(sum(Imcir(1:(diaExt-1),1:(diaExt-1))));
  447. else %standard square analysis area
  448. refPtRExt=refPtR+widthEx;
  449. if (refPtRExt)> size(fIntsc,1), refPtRExt=size(fIntsc,1); end
  450. refPtCExt=refPtC+widthEx;
  451. if (refPtCExt)> size(fIntsc,2), refPtCExt=size(fIntsc,2); end
  452. searchIntens=sum(sum(fIntsc(refPtR:(refPtRExt),refPtC:(refPtCExt))));
  453. end
  454. if searchIntens > intensMax
  455. intensMax=searchIntens;
  456. intens(r,c)=searchIntens;
  457. intensPrev(r,c)=searchIntens;
  458. a=[refPtR,refPtC];
  459. detMaxPos(r,c)={a};
  460. lastDetMaxPos(r,c)=detMaxPos(r,c);
  461. end
  462. if intensMax>definedSpot || intensMax<2
  463. stopSearch(r,c)=true;
  464. end
  465. end
  466. end
  467. if intensMax< noSpot
  468. intensMax=zeroSearchIntens;
  469. lastDetMaxPos(r,c)={refPt};
  470. end
  471. else % elseif Spot is well defined STOPSearch strong
  472. [refPt]=lastDetMaxPos{r,c};
  473. refPtR=refPt(1);
  474. refPtC=refPt(2);
  475. if doCircle==1
  476. cutout=fIntsc(refPtR:(refPtR+(diaExt-1)),refPtC:(refPtC+(diaExt-1)));
  477. Imcir=cutout .* uint8(cirMask);
  478. intens(r,c)=sum(sum(Imcir(1:(diaExt-1),1:(diaExt-1))));
  479. else %standard square analysis area
  480. intens(r,c)=sum(sum(fIntsc(refPtR:(refPtR+widthEx),refPtC:(refPtC+widthEx))));
  481. end
  482. end
  483. %{
  484. if intensMax> earlySrc1Thres && intensMax< earlySrc2Thres && src1cnt==0,
  485. %{
  486. src1cnt=src1cnt+1;
  487. detPrLo=(detPos{r,c}(1)) -12; detPrHi=(detPos{r,c}(1)) +width+12;
  488. detPcLo=(detPos{r,c}(2)) -12; detPcHi=(detPos{r,c}(2)) +width+12;
  489. rsum=(sum(fIntsc(detPrLo:detPrHi,detPcLo:detPcHi),2));
  490. csum=(sum(fIntsc(detPrLo:detPrHi,detPcLo:detPcHi),1));
  491. Rvec=detPrLo:detPrHi;
  492. Cvec=detPcLo:detPcHi;
  493. centR=round((sum(Rvec .*rsum')) /sum(rsum));
  494. centC=round((sum(Cvec .*csum)) /sum(csum));
  495. cent{r,c}=[centR, centC];
  496. %}
  497. end
  498. %}
  499. end
  500. end
  501. % [F_spots,bmtp, BGthres,bmm,rwm,lstTpt,BGsc,totBkgrd]=...
  502. % NIgenBkGrdDataPar4Fnc(ImParMat, tifFileLst, lastDetMaxPos, numRows, numCols, fullsc, tPt,BGthres, doCircle, cirPixA,diaExt, cirMask, BGTav);
  503. % NIgenBkGrdDataPar4Fnc
  504. % function [F_spots,bmtp, BGthres,bmm,rwm,lstTpt,BGsc,totBkgrd ]=...
  505. % NIgenBkGrdDataPar4Fnc(ImParMat, tifFileLst, lastDetMaxPos, numRows, numCols, fullsc, tPt, BGthres, doCircle, cirPixA, diaExt, cirMask, BGTav)
  506. % Called by NIscanIntensBG.m
  507. MPnum=ImParMat(1);
  508. destPerMP=ImParMat(2);
  509. spotThres=ImParMat(4)/100; %selScan=ImParMat(4);
  510. width=ImParMat(5);
  511. dither=ImParMat(6);
  512. BGthresInput=ImParMat(3); % a value of percent to be added (20=>1.20*BGTav)
  513. lstTpt=length(tifFileLst);
  514. LfOffset=65; %lf; %Ncode
  515. TopOffset=65; %tp;
  516. rtLim=1350; %1400; %1350;
  517. rtNotchOffset=10;
  518. cushion=20;
  519. % close2edgeTol=90;
  520. botLim=2003;
  521. botNotchOffset=60;
  522. % Initialize
  523. % clear('totBkgrd')
  524. % clear('pixsAboveBG')
  525. % clear('lineV','pixCnt','lineSum,meanBkgrd')
  526. % clear('rw','rwm')
  527. totBkgrd=0;
  528. pixsAboveBG=0;
  529. lineV=0; pixCnt=0; lineSum=0; meanBkgrd=85;
  530. rw=0; rwm=0;
  531. % Start Generation of BackGround data after the 2nd Row (&& 2nd Col)
  532. % Optimized spot positions determined
  533. %if r>=2 && c>=2,
  534. BGsc=zeros(2075,1400); %Empsc;
  535. widthEx=width-1; %width extention from reference point
  536. for r=1:numRows
  537. for c=1:numCols
  538. refP=lastDetMaxPos{r,c};
  539. if r<numRows,refNxR=lastDetMaxPos{r+1,c}; end
  540. if c<numCols,refNxC=lastDetMaxPos{r,c+1}; end
  541. if r>1, refPrR=lastDetMaxPos{r-1,c}; end
  542. if c>1, refPrC=lastDetMaxPos{r,c-1}; end
  543. % At least NINE conditions which must be treated differently
  544. % Condition 0: most spots fall in this central category
  545. if r>1 && r<numRows && c>1 && c<numCols
  546. if rem(r,numRows)==1
  547. refBG(1)=refP(1)-floor(0.5*(plateBotSpace));
  548. else
  549. refBG(1)=refP(1)-floor(0.5*(refP(1)-(refPrR(1)+widthEx)));
  550. end
  551. if rem(c,numCols)==1
  552. refBG(2)=refP(2)-floor(0.5*(plateEdgeSpace));
  553. else
  554. refBG(2)=refP(2)-floor(0.5*(refP(2)-(refPrC(2)+widthEx)));
  555. end
  556. if rem(r,numRows)==0
  557. refBG(3)=refP(1)+widthEx+ floor(0.5*(plateBotSpace));
  558. else
  559. refBG(3)=refP(1)+widthEx+ floor(0.5*(refNxR(1)-(refP(1)+widthEx)));
  560. end
  561. if rem(c,numCols)==0
  562. refBG(4)=refP(2)+widthEx+ floor(0.5*(plateEdgeSpace));
  563. else
  564. refBG(4)=refP(2)+widthEx+ floor(0.5*(refNxC(2)-(refP(2)+widthEx)));
  565. end
  566. %refBG(4)=refP(2)+widthEx+ floor(0.5*(refNxC(2)-refP(2)+widthEx));
  567. detBG(r,c)={uint8(refBG)}; %Store Background Reference pt data in a Cell
  568. % clear('lineV','pixCnt','lineSum')
  569. lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
  570. if tPt==1 || tPt==lstTpt,rw=lineV';end
  571. pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  572. lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  573. % clear('lineV')
  574. lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); % mean down lf
  575. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  576. pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  577. lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  578. % clear('lineV')
  579. lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); % mean down rt
  580. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  581. pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  582. lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  583. % clear('lineV')
  584. lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); % mean across bot rt
  585. if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
  586. pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  587. lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  588. if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
  589. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  590. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  591. if doCircle==1
  592. totBkgrd(r,c)=meanBkgrd* cirPixA;
  593. else
  594. totBkgrd(r,c)=meanBkgrd* width^2;
  595. end
  596. %{
  597. lineSum(1)=sum((fullsc(refBG(1),refBG(2):refBG(4)-1))); %mean across top rt
  598. lineSum(2)=sum((fullsc(refBG(1):refBG(3)-1,refBG(2)))); %mean down lf
  599. lineSum(3)=sum((fullsc(refBG(1):refBG(3)-1,refBG(4)))); %mean down rt
  600. lineSum(4)=sum((fullsc(refBG(3),refBG(2):refBG(4)-1))); %mean across bot rt
  601. meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
  602. totBkgrd(r,c)=meanBkgrd* width^2;
  603. %}
  604. end
  605. % Condition 1
  606. if r==1 && c==1
  607. % if (size(refP(1)-4)>0
  608. refBG(1)=refP(1)- floor(0.5*(refP(1) - TopOffset));
  609. refBG(2)=refP(2)- floor(0.5*(refP(2) - LfOffset)); % Icode -0
  610. refBG(3)=refP(1)+widthEx+ floor(0.5*((refNxR(1)-(refP(1)+widthEx))));
  611. refBG(4)=refP(2)+widthEx+ floor(0.5*((refNxC(2)-(refP(2)+widthEx))));
  612. detBG(r,c)={uint8(refBG)}; % Store Background Reference pt data in a Cell
  613. % clear('lineV','pixCnt','lineSum')
  614. lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); %mean across top rt
  615. if tPt==1 || tPt==lstTpt,rw=lineV';end
  616. pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  617. lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  618. % clear('lineV')
  619. lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); %mean down lf
  620. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  621. pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  622. lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  623. % clear('lineV')
  624. lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); %mean down rt
  625. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  626. pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  627. lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  628. % clear('lineV')
  629. lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); %mean across bot rt
  630. if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
  631. pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  632. lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  633. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  634. if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
  635. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  636. % meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
  637. if doCircle==1
  638. totBkgrd(r,c)=meanBkgrd* cirPixA;
  639. else
  640. totBkgrd(r,c)=meanBkgrd* width^2;
  641. end
  642. %{
  643. lineSum(1)=sum((fullsc(refBG(1),refBG(2):refBG(4)-1))); %mean across top rt
  644. lineSum(2)=sum((fullsc(refBG(1):refBG(3)-1,refBG(2)))); %mean down lf
  645. lineSum(3)=sum((fullsc(refBG(1):refBG(3)-1,refBG(4)))); %mean down rt
  646. lineSum(4)=sum((fullsc(refBG(3),refBG(2):refBG(4)-1))); %mean across bot rt
  647. meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
  648. totBkgrd(r,c)=round(meanBkgrd* width^2);
  649. %}
  650. end
  651. % Condition 1a
  652. if r==1 && c>1 && c<numCols
  653. refBG=zeros(1,4);
  654. refBG(1)=refP(1)-floor(0.5*(refP(1)-TopOffset));
  655. if rem(c,numCols)==1
  656. refBG(2)=refP(2)-floor(0.5*(plateEdgeSpace));
  657. else
  658. refBG(2)=refP(2)-floor(0.5*(refP(2)-(refPrC(2)+widthEx)));
  659. end
  660. refBG(3)=refP(1)+widthEx+ floor(0.5*(refNxR(1)-(refP(1)+widthEx)));
  661. if rem(c,numCols)==0
  662. refBG(4)=refP(2)+widthEx+ floor(0.5*(plateEdgeSpace));
  663. else
  664. refBG(4)=refP(2)+widthEx+ floor(0.5*(refNxC(2)-(refP(2)+widthEx)));
  665. end
  666. detBG(r,c)={uint8(refBG)}; %Store Background Reference pt data in a Cell
  667. % clear('lineV','pixCnt','lineSum')
  668. lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
  669. if tPt==1 || tPt==lstTpt,rw=lineV';end
  670. pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  671. lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  672. % clear('lineV')
  673. lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); %mean down lf
  674. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  675. pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  676. lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  677. % clear('lineV')
  678. lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); %mean down rt
  679. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  680. pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  681. lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  682. % clear('lineV')
  683. lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); %mean across bot rt
  684. if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
  685. pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  686. lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  687. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  688. if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
  689. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  690. if doCircle==1
  691. totBkgrd(r,c)=meanBkgrd* cirPixA;
  692. else
  693. totBkgrd(r,c)=meanBkgrd* width^2;
  694. end
  695. %{
  696. lineSum(1)=sum((fullsc(refBG(1),refBG(2):refBG(4)-1))); %mean across top rt
  697. lineSum(2)=sum((fullsc(refBG(1):refBG(3)-1,refBG(2)))); %mean down lf
  698. lineSum(3)=sum((fullsc(refBG(1):refBG(3)-1,refBG(4)))); %mean down rt
  699. lineSum(4)=sum((fullsc(refBG(3),refBG(2):refBG(4)-1))); %mean across bot rt
  700. meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
  701. totBkgrd(r,c)=meanBkgrd* width^2;
  702. %}
  703. end
  704. % Condition 2
  705. if r==1 && c==numCols
  706. % close2edgeTol=14;
  707. refBGcExt=refP(2)+ widthEx;
  708. refBG(1)=refP(1)-floor(0.5*(refP(1)-TopOffset)); %vertical (row)
  709. refBG(2)=refP(2)-floor(0.5*(refP(2)-(refPrC(2)+widthEx))); %horizontal(col)
  710. refBG(3)=refP(1)+widthEx+ floor(0.5*((refNxR(1)-(refP(1)+widthEx))));
  711. % if refBGcExt>(size(fullsc,2)-close2edgeTol)%if intens area too close to edge
  712. if refBGcExt>(rtLim-cushion)%if intens area too close to edge
  713. refBG(4)=rtLim-rtNotchOffset;
  714. else
  715. refBG(4)=refP(2)+widthEx+ floor(0.5*(rtLim-refBGcExt));
  716. end
  717. detBG(r,c)={uint8(refBG)}; %Store Background Reference pt data in a Cell
  718. % clear('lineV','pixCnt','lineSum')
  719. lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
  720. if tPt==1 || tPt==lstTpt,rw=lineV';end
  721. pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  722. lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  723. % clear('lineV')
  724. lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); %mean down lf
  725. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  726. pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  727. lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  728. % clear('lineV')
  729. lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); %mean across bot rt
  730. if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
  731. pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  732. lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  733. % if refBGcExt>(size(fullsc,2)-close2edgeTol) %if intens area too close to edge
  734. if refBGcExt>(rtLim-cushion) %if intens area too close to edge
  735. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  736. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  737. % meanBkgrd=(lineSum(1)+lineSum(2)+lineSum(4))./ ...
  738. % ((2*(refBG(4)-refBG(2))+(refBG(3)-refBG(1)))); %divby 2*across+1*down
  739. if doCircle==1
  740. totBkgrd(r,c)=meanBkgrd* cirPixA;
  741. else
  742. totBkgrd(r,c)=meanBkgrd* width^2;
  743. end
  744. else
  745. % clear('lineV') %3
  746. lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); %mean down rt
  747. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  748. pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  749. lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  750. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  751. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  752. % meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
  753. if doCircle==1
  754. totBkgrd(r,c)=meanBkgrd* cirPixA;
  755. else
  756. totBkgrd(r,c)=meanBkgrd* width^2;
  757. end
  758. end
  759. if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
  760. end
  761. % Condition 2a: down rows 2->47 farRight col edge
  762. if r>1 && r<numRows && c==numCols
  763. % close2edgeTol=14; %Icode=4
  764. refBGcExt=refP(2)+widthEx;
  765. if rem(r,numRows)==1
  766. refBG(1)=refP(1)-floor(0.5*(plateBotSpace));
  767. else
  768. refBG(1)=refP(1)-floor(0.5*(refP(1)-(refPrR(1)+widthEx)));
  769. end
  770. refBG(2)=refP(2)-floor(0.5*(refP(2)-(refPrC(2)+widthEx)));
  771. if rem(r,numRows)==0
  772. refBG(3)=refP(1)+widthEx+ floor(0.5*(plateBotSpace));
  773. else
  774. refBG(3)=refP(1)+widthEx+ floor(0.5*(refNxR(1)-(refP(1)+widthEx)));
  775. end
  776. if refBGcExt<(rtLim-cushion) %if intens area too close to edge
  777. refBG(4)=refP(2)+widthEx+ floor(0.5*(rtLim-refBGcExt));
  778. else
  779. refBG(4)=rtLim-cushion;
  780. end
  781. detBG(r,c)={uint8(refBG)}; % store Background Reference pt data in a Cell
  782. % clear('lineV','pixCnt','lineSum') %1
  783. lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
  784. if tPt==1 || tPt==lstTpt,rw=lineV';end
  785. pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  786. lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  787. % clear('lineV') %2
  788. lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); %mean down lf
  789. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  790. pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  791. lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  792. % clear('lineV') %4
  793. lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); % mean across bot rt
  794. if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
  795. pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  796. lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  797. %if refBGcExt<(size(fullsc,2)-close2edgeTol) % if intens area too close to edge
  798. if refBGcExt<(rtLim-cushion) % if intens area too close to edge
  799. % clear('lineV') %3
  800. lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); % mean down rt
  801. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  802. pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  803. lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  804. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  805. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  806. % meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
  807. if doCircle==1
  808. totBkgrd(r,c)=meanBkgrd* cirPixA;
  809. else
  810. totBkgrd(r,c)=meanBkgrd* width^2;
  811. end
  812. else
  813. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  814. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  815. % meanBkgrd=(lineSum(1)+lineSum(2)+lineSum(4))./ ...
  816. % ((2*(refBG(4)-refBG(2))+(refBG(3)-refBG(1)))); %divby 2*across+1*down
  817. if doCircle==1
  818. totBkgrd(r,c)=meanBkgrd* cirPixA
  819. else
  820. totBkgrd(r,c)=meanBkgrd* width^2
  821. end
  822. end
  823. if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
  824. end
  825. % Condition 3: Lower right corner
  826. if r==numRows && c==numCols
  827. %close2edgeTol=14;
  828. refBGrExt=refP(1)+ widthEx;
  829. refBGcExt=refP(2)+ widthEx;
  830. refBG(1)=refP(1)-floor(0.5*(refP(1)-(refPrR(1)+widthEx))); %vertical (row)
  831. refBG(2)=refP(2)-floor(0.5*(refP(2)-(refPrC(2)+widthEx))); %horizontal(col)
  832. if refBGrExt>(botLim-botNotchOffset)%if intens area not too close to bot edge else
  833. refBG(3)=botLim + cushion;
  834. else
  835. refBG(3)=refP(1)+widthEx+ floor(0.5*(botLim-refBGrExt));
  836. end
  837. if refBGcExt>(rtLim- cushion)%if intens area not too close to edge else
  838. refBG(4)=rtLim -cushion;
  839. else
  840. refBG(4)=refP(2)+widthEx+ floor(0.5*(rtLim-refBGcExt));
  841. end
  842. detBG(r,c)={uint8(refBG)}; %Store Background Reference pt data in a Cell
  843. % clear('lineV','pixCnt','lineSum') %1
  844. lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
  845. if tPt==1 || tPt==lstTpt,rw=lineV';end
  846. pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  847. lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  848. % clear('lineV') %2
  849. lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); % mean down lf
  850. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  851. pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  852. lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  853. % if refBGcExt>(size(fullsc,2)-close2edgeTol)&&...
  854. if refBGcExt>(rtLim-cushion)&&...
  855. refBGrExt<(botLim-botNotchOffset) % if intens area too close to right edge only
  856. % clear('lineV') %4
  857. lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); % mean across bot rt
  858. if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
  859. pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  860. lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  861. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  862. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  863. % meanBkgrd=(lineSum(1)+lineSum(2)+lineSum(4))./ ...
  864. % ((2*(refBG(4)-refBG(2))+(refBG(3)-refBG(1)))); % divby 2*across+1*down
  865. if doCircle==1
  866. totBkgrd(r,c)=meanBkgrd* cirPixA;
  867. else
  868. totBkgrd(r,c)=meanBkgrd* width^2;
  869. end
  870. elseif refBGrExt>(botLim-botNotchOffset) && ...
  871. refBGcExt>(rtLim-cushion)%if intens area too close to bottom edge only
  872. % clear('lineV') %3
  873. lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); % mean down rt
  874. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  875. pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  876. lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  877. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  878. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end % meanBkgrd=(lineSum(1)+lineSum(2)+lineSum(3))./ ...
  879. %((refBG(4)-refBG(2)+(2*(refBG(3)-refBG(1))))); % divby 1*across+2*down
  880. if doCircle==1, totBkgrd(r,c)=meanBkgrd* cirPixA; else totBkgrd(r,c)=meanBkgrd* width^2; end
  881. % elseif refBGcExt>(size(fullsc,2)-close2edgeTol)&& ...
  882. elseif refBGcExt>(rtLim-cushion)&& ...
  883. refBGrExt>(botLim-botNotchOffset) % if intens area too close to both right edge and bottom edge
  884. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  885. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  886. % meanBkgrd=(lineSum(1)+lineSum(2))./ ...
  887. % ((refBG(4)-refBG(2)+(refBG(3)-refBG(1)))); %divby 1across+1*down
  888. if doCircle==1, totBkgrd(r,c)=meanBkgrd* cirPixA; else totBkgrd(r,c)=meanBkgrd* width^2; end
  889. else
  890. % clear('lineV') %3
  891. lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); %mean down rt
  892. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  893. pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  894. lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  895. % clear('lineV') %4
  896. lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); %mean across bot rt
  897. if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
  898. pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  899. lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  900. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  901. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  902. % meanBkgrd=(sum(lineSum(1:4)))./ (2*((refBG(4)-refBG(2))+(refBG(3)-refBG(1))));
  903. if doCircle==1
  904. totBkgrd(r,c)=meanBkgrd* cirPixA;
  905. else
  906. totBkgrd(r,c)=meanBkgrd* width^2;
  907. end
  908. end
  909. if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
  910. end
  911. % Condition 3a: Bottom across row48 col2-79
  912. if r==numRows && c>1 && c<numCols
  913. refBG=zeros(1,4);
  914. % close2edgeTol=14; % Icode=4
  915. refBGrExt=refP(1)+ widthEx;
  916. refBG(1)=refP(1)-floor(0.5*(refP(1)-(refPrR(1)+widthEx))); % vertical (row)
  917. if rem(c,numCols)==1
  918. refBG(2)=refP(2)-floor(0.5*(plateEdgeSpace)); % horizontal(col)
  919. else
  920. refBG(2)=refP(2)-floor(0.5*(refP(2)-(refPrC(2)+widthEx))); % horizontal(col)
  921. end
  922. if refBGrExt>(botLim-botNotchOffset) % if intens area not too close to bot edge else
  923. refBG(3)=botLim + cushion;
  924. else
  925. refBG(3)=refP(1)+widthEx+ floor(0.5*(botLim-refBGrExt));
  926. end
  927. if rem(c,numCols)==0
  928. refBG(4)=refP(2)+widthEx+ floor(0.5*(plateEdgeSpace));
  929. else
  930. refBG(4)=refP(2)+widthEx+ floor(0.5*(refNxC(2)-(refP(2)+widthEx)));
  931. %refBG(4)=refP(2)+widthEx+ floor(0.5*(size(fullsc,2)-refBGcExt));
  932. end
  933. detBG(r,c)={uint8(refBG)}; % Store Background Reference pt data in a Cell
  934. % clear('lineV','pixCnt','lineSum') %1
  935. lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
  936. if tPt==1 || tPt==lstTpt,rw=lineV';end
  937. pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  938. lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  939. % clear('lineV') %2
  940. lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); % mean down lf
  941. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  942. pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  943. lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  944. % clear('lineV') %3
  945. lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); % mean down rt
  946. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  947. pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  948. lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  949. if refBGrExt>(botLim-botNotchOffset) % if intens area too close to bottom edge
  950. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  951. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  952. % meanBkgrd=(lineSum(1)+lineSum(2)+lineSum(4))./ ...
  953. % ((refBG(4)-refBG(2)+(2*(refBG(3)-refBG(1))))); %divby 1*across+2*down
  954. if doCircle==1, totBkgrd(r,c)=meanBkgrd* cirPixA; else totBkgrd(r,c)=meanBkgrd* width^2; end
  955. else
  956. % clear('lineV') %4
  957. lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); % mean across bot rt
  958. if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
  959. pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  960. lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  961. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  962. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  963. % meanBkgrd=(sum(lineSum(1:4)))./ ((2*(refBG(4)-refBG(2))+ 2*(refBG(3)-refBG(1))));
  964. if doCircle==1, totBkgrd(r,c)=meanBkgrd* cirPixA; else totBkgrd(r,c)=meanBkgrd* width^2; end
  965. end
  966. if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
  967. end
  968. % Condition 4: Bottom Left Corner row48 col1
  969. if r==numRows && c==1
  970. %close2edgeTol=14; %Icode=4
  971. refBGrExt=refP(1)+ widthEx;
  972. refBG(1)=refP(1)-floor(0.5*(refP(1)-(refPrR(1)+widthEx))); % vertical (row)
  973. refBG(2)=refP(2)- floor(0.5*(refP(2) - LfOffset)); % horizontal(col) Icode -0 instead of LfOffset
  974. if refBGrExt>(botLim-botNotchOffset) % if intens area not too close to bot edge else
  975. refBG(3)=botLim + cushion;
  976. else
  977. refBG(3)=refP(1)+widthEx+ floor(0.5*(botLim-refBGrExt));
  978. end
  979. refBG(4)=refP(2)+widthEx+ floor(0.5*(refNxC(2)-(refP(2)+widthEx)));
  980. detBG(r,c)={uint8(refBG)}; % Store Background Reference pt data in a Cell
  981. % clear('lineV','pixCnt','lineSum') %1
  982. lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
  983. if tPt==1 || tPt==lstTpt,rw=lineV';end
  984. pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  985. lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  986. % clear('lineV') %2
  987. lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); % mean down lf
  988. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  989. pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  990. lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  991. % clear('lineV') %3
  992. lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); % mean down rt
  993. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  994. pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  995. lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  996. if refBGrExt>(botLim-botNotchOffset) % if intens area too close to bottom edge
  997. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  998. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  999. % meanBkgrd=(lineSum(1)+lineSum(2)+lineSum(3))./ ...
  1000. % ((refBG(4)-refBG(2)+(2*(refBG(3)-refBG(1))))); % divby 1*across+2*down
  1001. if doCircle==1, totBkgrd(r,c)=meanBkgrd* cirPixA; else totBkgrd(r,c)=meanBkgrd* width^2; end
  1002. else
  1003. % clear('lineV') %4
  1004. lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); %mean across bot rt
  1005. if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
  1006. pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  1007. lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  1008. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  1009. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  1010. % meanBkgrd=(sum(lineSum(1:4)))./ ((2*(refBG(4)-refBG(2))+ 2*(refBG(3)-refBG(1))));
  1011. if doCircle==1
  1012. totBkgrd(r,c)=meanBkgrd* cirPixA;
  1013. else
  1014. totBkgrd(r,c)=meanBkgrd* width^2;
  1015. end
  1016. end
  1017. if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
  1018. end
  1019. % Condition 4a: Left edge center area rows2-47 col1
  1020. if r>1 && r<numRows && c==1
  1021. % close2edgeTol=14; %Icode=4
  1022. refBGrExt=refP(1)+ widthEx;
  1023. if rem(r,numRows)==1
  1024. refBG(1)=refP(1)-floor(0.5*(plateBotSpace)); % vertical (row)
  1025. else
  1026. refBG(1)=refP(1)-floor(0.5*(refP(1)-(refPrR(1)+widthEx))); % vertical (row)
  1027. end
  1028. refBG(2)=refP(2)- floor(0.5*(refP(2) - LfOffset)); % horizontal(col) %Icode=0
  1029. if rem(r,numRows)==0
  1030. refBG(3)=refP(1)+widthEx+ floor(0.5*(plateBotSpace));
  1031. else
  1032. refBG(3)=refP(1)+widthEx+ floor(0.5*(refNxR(1)-(refP(1)+widthEx)));
  1033. end
  1034. refBG(4)=refP(2)+widthEx+ floor(0.5*(refNxC(2)-(refP(2)+widthEx)));
  1035. detBG(r,c)={uint8(refBG)}; % Store Background Reference pt data in a Cell
  1036. % clear('lineV','pixCnt','lineSum') %1
  1037. lineV=fullsc(refBG(1),refBG(2):refBG(4)-1); % across top rt
  1038. if tPt==1 || tPt==lstTpt,rw=lineV';end
  1039. pixCnt(1)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  1040. lineSum(1)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  1041. % clear('lineV') %2
  1042. lineV=fullsc(refBG(1):refBG(3)-1,refBG(2)); %mean down lf
  1043. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  1044. pixCnt(2)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  1045. lineSum(2)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  1046. % clear('lineV') %3
  1047. lineV=fullsc(refBG(1):refBG(3)-1,refBG(4)); %mean down rt
  1048. if tPt==1 || tPt==lstTpt,rw=[rw;lineV];end
  1049. pixCnt(3)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  1050. lineSum(3)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  1051. % clear('lineV') %4
  1052. lineV=fullsc(refBG(3),refBG(2):refBG(4)-1); %mean across bot rt
  1053. if tPt==1 || tPt==lstTpt,rw=[rw;lineV'];end
  1054. pixCnt(4)=nnz(lineV .* uint8(lineV<BGthres(r,c)));
  1055. lineSum(4)=sum(lineV .* uint8(lineV<BGthres(r,c)));
  1056. meanBkgrd=(sum(lineSum(1:4)))./ (sum(pixCnt(1:4)));
  1057. if tPt==1 || tPt==lstTpt,rwm(r,c)=floor(mean(rw));end
  1058. if sum(pixCnt(1:4))==0, meanBkgrd=double(BGthres(r,c));end
  1059. if doCircle==1
  1060. totBkgrd(r,c)=meanBkgrd* cirPixA;
  1061. else
  1062. totBkgrd(r,c)=meanBkgrd* width^2;
  1063. end
  1064. end
  1065. % Determine the actual number of pixels(area) of growth above the background
  1066. % Intensity
  1067. BG=meanBkgrd;
  1068. [refPtPxAbove]=lastDetMaxPos{r,c};
  1069. refPtRx=refPtPxAbove(1);
  1070. refPtCx=refPtPxAbove(2);
  1071. if doCircle==1
  1072. cutout=(fullsc(refPtRx:(refPtRx+ (diaExt-1)),refPtCx:(refPtCx+ (diaExt-1))));
  1073. Imcir=cutout .* uint8(cirMask);
  1074. pixsAboveBG(r,c)=nnz(Imcir>BG); %calc non-background within circular analysis area
  1075. % pixsAboveBG(r,c)=nnz(fullsc(refPtRx:(refPtRx+ (diaExt-1)),refPtCx:(refPtCx+ (diaExt-1)))>BG); %BG);
  1076. else % standard square analysis area
  1077. pixsAboveBG(r,c)=nnz(fullsc(refPtRx:(refPtRx+widthEx),refPtCx:(refPtCx+widthEx))>BG); % calc non-background within square analysis area
  1078. end
  1079. % TimePT average
  1080. % meanBGTsum(r,c)=meanBGTsum(r,c)+meanBkgrd;
  1081. % meanBGTav(r,c)=meanBGTsum(r,c) /tPt;
  1082. bmm(r,c)=uint8(floor(meanBkgrd));
  1083. % For FOTOS only
  1084. F_spots(r,c,tPt)={refBG}; % Foto spot coordinates
  1085. bmtp(r,c,tPt)=bmm(r,c); % Added/used for FOTOS 11_0830
  1086. % TimePT Average BG intensity calc method
  1087. BGTav(r,c)=uint8(round((BGTav(r,c)*(tPt-1))+meanBkgrd)/tPt);
  1088. % BGTav(r,c)=((BGTav(r,c)*(tPt-1))+meanBkgrd)/tPt;
  1089. % sliding BG average
  1090. % if r==1 && c==1, slavg(1:5)=BG;end % slavg(1:5)=50; %120620 Epsonvalue is 33; end
  1091. % slavg(5)=slavg(4);
  1092. % slavg(4)=slavg(3);
  1093. % slavg(3)=slavg(2);
  1094. % slavg(2)=slavg(1);
  1095. % slavg(1)=BG;
  1096. % slidingAvg=mean(slavg);
  1097. % For Visualization Purposes only
  1098. %1->2 across rt 1, 2-4
  1099. BGsc(refBG(1),refBG(2):refBG(4))=1;
  1100. %1->4 down lf 1-3, 2
  1101. BGsc(refBG(1):refBG(3),refBG(2))=1;
  1102. %2->3 down rt 1-3, 4
  1103. BGsc(refBG(1):refBG(3),refBG(4))=1;
  1104. %4->3 Bot Across rt 1, 2-4
  1105. BGsc(refBG(3),refBG(2):refBG(4))=1;
  1106. % BGsc(refBG(1):refBG(3),refBG(2):refBG(4)) %just displays matrices
  1107. end
  1108. end
  1109. BGthres=uint8(double(BGTav) * (1+(BGthresInput/100)));
  1110. optomizedPos(:,:,tPt)=lastDetMaxPos;
  1111. % if(isequal(SWsingleSc,1))|| ((~isequal(SWsingleSc,1))&&tPt==tptLength) %190731 change to capture last timePt PTmap if(isequal(SWsingleSc,1))
  1112. % if(isequal(SWsingleSc,1))&& (tPt==1 || tPt==guesTpt || tPt==tptLength)|| ((~isequal(SWsingleSc,1))&&tPt==tptLength) %190919 Option for observation
  1113. % if (tPt==1 || tPt==guesTpt || tPt==tptLength) %190919 Option for observation
  1114. if (tPt==tptLength) % 190919 change to capture ONLY last timePt PTmap
  1115. % clear OptmapOnes;
  1116. OptmapOnes=[];
  1117. % OptmapOnes=Empsc+ 1.4;
  1118. OptmapOnesDbl=double(Empsc)+ .8;
  1119. for r=1:numRows
  1120. for c=1:numCols
  1121. [refPt]=lastDetMaxPos{r,c};
  1122. refPtR=refPt(1);
  1123. refPtC=refPt(2);
  1124. if doCircle==1
  1125. % OptmapOnesDbl(refPtR:(refPtR+diaExt),refPtC:(refPtC+diaExt))=cirMask;
  1126. OptmapOnesDbl(refPtR:(refPtR+(diaExt-1)),refPtC:(refPtC+(diaExt-1)))=optCirMask;
  1127. else
  1128. refPtRExt=refPtR+widthEx;
  1129. if (refPtRExt)> size(fullsc,1), refPtRExt=size(fullsc,1); end
  1130. refPtCExt=refPtC+widthEx;
  1131. if (refPtCExt)> size(fullsc,2), refPtCExt=size(fullsc,2); end
  1132. OptmapOnesDbl(refPtR:(refPtRExt),refPtC:(refPtCExt))=1;%255;
  1133. end
  1134. end
  1135. end
  1136. % Cross-optimized Map Matrix onto tifFile Matrix
  1137. % For visualization purposes only
  1138. if tPt==1,figure;end
  1139. resIm=uint8((double(BGsc) + OptmapOnesDbl) .* double(fullsc));
  1140. % clear('OptmapOnesDbl')
  1141. OptmapOnesDbl=[];
  1142. % hres=imagesc(resIm);
  1143. hfscanIm=imagesc(resIm);
  1144. hfIm=gcf;
  1145. haxis=gca;
  1146. title(strcat('Scan',num2str(selScan),'timePt-',num2str(tPt)));
  1147. clf(hfscanIm,'reset');
  1148. set(hfIm,'NumberTitle','off')
  1149. set(hfIm,'Name', char(matFile))
  1150. if Fflg==1
  1151. fullfile(fotosResultsDir,strcat('Scan',num2str(selScan),'_timePt-',num2str(tPt)))
  1152. hgsave(fullfile(fotosResultsDir,strcat('Scan',num2str(selScan),'_timePt-',num2str(tPt)))); %F 14_0626
  1153. else
  1154. hgsave(fullfile(figsResultsDir,strcat('Scan',num2str(selScan),'_timePt-',num2str(tPt))));
  1155. end
  1156. else
  1157. selScan;
  1158. % tifFile
  1159. end
  1160. BkgrdMat=totBkgrd; % BkgrdMat(:,:,tPt)=totBkgrd; %debug parfor effort
  1161. areaOfIntensAboveBG(:,:,tPt)=pixsAboveBG; % actual area of growth for each timepoint (used for printout of grArea ?not used in previous analysis)
  1162. scanIntens(:,:,tPt)=intens-BkgrdMat;
  1163. % Capture first and last background values
  1164. if tPt==1
  1165. bdg(:,:,1)=bmm;
  1166. bdg(:,:,3)=rwm;
  1167. Tmpsbdg1={bdg};
  1168. end
  1169. if tPt==lstTpt
  1170. bdg(:,:,2)=bmm;
  1171. bdg(:,:,4)=rwm;
  1172. Tmpsbdg1={bdg};
  1173. end
  1174. TmpexpScanIntens1={scanIntens}; % par4Gbl 20_0122
  1175. TmpFexpScanSpots1={F_spots};
  1176. TmpFexpScanBMtp1={bmtp};
  1177. TmpanlZoneRefs1={optomizedPos}; % added to store tPt by tPt positions
  1178. TmpexpScanIntens2=TmpexpScanIntens1;
  1179. TmpFexpScanSpots2=TmpFexpScanSpots1;
  1180. TmpFexpScanBMtp2=TmpFexpScanBMtp1;
  1181. TmpanlZoneRefs2=TmpanlZoneRefs1;
  1182. Tmpsbdg2=Tmpsbdg1;
  1183. end
  1184. end