SmplMeasureCleanliness.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using OTSCOMMONCLR;
  9. using OTSDataType;
  10. using OTSModelSharp.Measure.OTSCleanliness;
  11. using OTSModelSharp.ServiceCenter;
  12. using OTSModelSharp.ServiceInterface;
  13. using static OTSDataType.otsdataconst;
  14. namespace OTSModelSharp
  15. {
  16. class CSmplMeasureCleanliness : CSmplMeasure
  17. {
  18. public CSmplMeasureCleanliness(string a_strWorkingFolder, COTSSample a_pSample):base(a_strWorkingFolder,a_pSample)
  19. {
  20. SetWorkingFolder(a_strWorkingFolder);
  21. SetSample(a_pSample);
  22. m_classifyEngine = new CClassifyEngine();
  23. }
  24. // field image process
  25. public override bool FieldImageProcess(Point fldCenter, CBSEImgClr a_BSEImg)
  26. {
  27. int nNewFieldId;
  28. nNewFieldId = m_pSampleRstFile.GetIdForANewField();
  29. //first step:remove background of the bse image and compound all the finded particles.
  30. curFldData = new CFieldDataClean( a_BSEImg,m_Sample.CalculatePixelSize());
  31. CFieldDataClean curFldDataCln =( CFieldDataClean) curFldData;
  32. curFldData.SetId(nNewFieldId);
  33. curFldData.SetPosition(fldCenter);
  34. GetOriginalParticles();
  35. // second step :filter the finded particles.
  36. FilterParticles((CFieldDataClean)curFldData);
  37. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  38. //collect xray data.
  39. if (pXRayParam.GetUsingXray() == (int)OTS_USING_X_RAY.Yes)
  40. {
  41. Thread.Sleep(100);
  42. CollectParticlesXrayData((CFieldDataClean)curFldData);
  43. Thread.Sleep(100);
  44. }
  45. //special treatment.
  46. //ParticleSpecialTreatment();
  47. log.Info("Begin to Calculate the image property of every particle!");
  48. var analysisparts = curFldDataCln.ListBigParticles;
  49. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  50. ClassifyParticles(curFldData.ListAnalysisParticles);
  51. // save field files
  52. m_Sample.GetMsrStatus() .SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  53. StartSaveFileThread(curFldData);
  54. return true;
  55. }
  56. // save field data
  57. protected void SaveFieldMgrData()
  58. {
  59. while (bSaveThreadWorking)
  60. {
  61. while (fieldQueue.Count() > 0)
  62. {
  63. CFieldDataClean f = (CFieldDataClean) fieldQueue.Dequeue();
  64. double pixelSize = m_Sample.CalculatePixelSize();
  65. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  66. SaveFieldFiles(f,m_pSampleRstFile.GetFieldFileSubFolderStr());
  67. }
  68. if (fieldQueue.Count() == 0)
  69. {
  70. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  71. return;
  72. }
  73. }
  74. return;
  75. }
  76. protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
  77. {
  78. fieldQueue.Enqueue(a_pFieldMgr);
  79. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  80. {
  81. if (bSaveThreadWorking == false)
  82. {
  83. bSaveThreadWorking = true;
  84. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  85. m_thread_ptr.Start();//m_thread_ptr->detach();
  86. }
  87. }
  88. }
  89. public void FilterParticles(CFieldDataClean fld)
  90. {
  91. // 1)remove over sized particles, too small particles and get a analysis particles list
  92. double dPixelSize = m_Sample.CalculatePixelSize();
  93. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  94. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  95. curFldData.FilterParticles(pImgProcessParam, dPixelSize);
  96. if (curFldData.NoAnalysisParticle())
  97. {
  98. log.Warn("There's no analysis particles!");
  99. }
  100. //2) according to the quantify threshold size value saperate the analysis particles into two group :the bigparticles and the smallparticles.
  101. int quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetQuantifyMinSize();
  102. var smallparts = fld.ListSmallParticles;
  103. var bigparts = fld.ListBigParticles;
  104. foreach (var part in curFldData.ListAnalysisParticles)
  105. {
  106. if (part.GetArea() < quantifyThreshold)
  107. {
  108. smallparts.Add(part);
  109. }
  110. else
  111. {
  112. bigparts.Add(part);
  113. }
  114. }
  115. // 3) according to the presettings,make sure we won't do the xray processing work for too many small particles.
  116. //var fieldParam = m_Sample.GetMsrParams().GetImageScanParam();
  117. //double smallPartnum;
  118. // List<COTSParticleClr> smallPartList = new List<COTSParticleClr>();
  119. //double percentage = 100;
  120. //switch (fieldParam.GetFieldStopMode())
  121. //{
  122. // case otsdataconst.OTS_MEASURE_FIELD_STOP_MODE.ParticlesPercentage:
  123. // percentage = fieldParam.GetFieldSmallParticlesPercentage();
  124. // percentage = percentage / 100;// according to the percentage
  125. // smallPartnum = smallparts.Count() * percentage;
  126. // for (int i = 0; i < smallPartnum; i++)
  127. // {
  128. // smallPartList.Add(smallparts[i]);
  129. // }
  130. // break;
  131. // case otsdataconst.OTS_MEASURE_FIELD_STOP_MODE.Particles:
  132. // percentage = 1;
  133. // smallPartnum = fieldParam.GetFieldSmallParticleNum();//according to the numbers.
  134. // if (smallPartnum < smallparts.Count())
  135. // {
  136. // for (int i = 0; i < smallPartnum; i++)
  137. // {
  138. // smallPartList.Add(smallparts[i]);
  139. // }
  140. // }
  141. // break;
  142. // default:
  143. // percentage = 1;
  144. // smallPartList = smallparts;
  145. // break;
  146. //}
  147. fld.ListSmallParticles = smallparts;
  148. fld.ListBigParticles = bigparts;
  149. //fld.SmallParticlePercentage=percentage;
  150. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  151. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2")+ "): " + bigparts.Count);
  152. return ;
  153. }
  154. public void CollectParticlesXrayData(CFieldDataClean fld)
  155. {
  156. // get x-ray parameters
  157. COTSXRayParam pXRayParam = m_Sample.GetMsrParams ().GetXRayParam();
  158. // calculate search x-ray acquire time
  159. uint nXRayAQTime;
  160. var smallparts = fld.ListSmallParticles;
  161. var bigparts = fld.ListBigParticles;
  162. var pStatus = m_Sample.GetMsrStatus();
  163. //// particle x-ray analysis
  164. ////=============================================
  165. curFldData.CreateXrayList(bigparts); // big particle using the full xray strategy.
  166. curFldData.CreateXrayList(smallparts); //small particle using the fast xray strategy
  167. // get x-ray list (analysis) by particle features
  168. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  169. m_EDSHardwareMgr.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  170. nXRayAQTime = (uint)pXRayParam.GetFastXrayTime();
  171. // get x-ray list (analysis) by points
  172. m_EDSHardwareMgr.GetXRayByPoints(smallparts, nXRayAQTime, true);
  173. return ;
  174. }
  175. public void ParticleSpecialTreatment(CFieldDataClean fld)
  176. {
  177. //we adopt such a strategy here:if some particles satisfy the predefined condition then we go through the second collecting with max EDS time,so that we got a more acurate result.
  178. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>(); //std.map<COTSParticlePtr, int> mapPartXray = new std.map<COTSParticlePtr, int>();
  179. double edsTime = 0; //to store the EDSTime returned from the engine.
  180. var bigparts = fld.ListBigParticles;
  181. var m_ClassifyEngine = new CClassifyEngine();
  182. for (int i = 0; i < bigparts.Count(); i++)
  183. {
  184. var engine = m_ClassifyEngine.GetParticleEngine(m_Sample.GetMsrParams().GetSTDName());
  185. var p = bigparts[i];
  186. // there will be very less particle satisfied the condition ,so we use only one MaxEDS time,though we set MaxEDSTime for every rule separately .Here we store the last one.
  187. //if the particle satisfied the MaxEDS condition then we go through the second colleting.Here we record the particle and the EDSTime and the corresponding xray position.
  188. edsTime = engine.IfNeedMaxEDS(p);
  189. if (edsTime > 0)
  190. {
  191. mapPartXray[p] = i;
  192. }
  193. }
  194. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  195. if (mapPartXray.Count() > 0)
  196. {
  197. foreach (var p in mapPartXray)
  198. {
  199. partsMax.Add(p.Key);
  200. }
  201. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  202. m_EDSHardwareMgr.GetXRayByFeatures(partsMax, edsTime, true);
  203. }
  204. }
  205. public void ClassifyParticles(CFieldDataClean fld)
  206. {
  207. try
  208. {
  209. string libname = m_Sample.GetMsrParams().GetSTDName();
  210. if (libname != "NoSTDDB")
  211. {
  212. log.Info("Begin to classify big particles!Using " + libname);
  213. var bigparts = fld.ListBigParticles;
  214. ClassifyQuantifyParticles(bigparts,libname);
  215. ClassifySmallParticles(fld, libname);
  216. }
  217. }
  218. catch (Exception e)
  219. {
  220. log.Info("calcu the particle image property or classify failed. "+e.Message);
  221. }
  222. }
  223. public void SaveFieldParticlesData()
  224. {
  225. StartSaveFileThread(curFldData);
  226. }
  227. public void GetOriginalParticles()
  228. {
  229. // measure status
  230. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  231. // get image process parameter
  232. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  233. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  234. // remove BES image background
  235. curFldData.RemoveBSEImageBG(pImgProcessParam);
  236. // check if this is an empty image
  237. if (curFldData.NoParticle())
  238. { // empty fields
  239. log.Info("ImageProcess: empty field.");
  240. //return false;
  241. }
  242. return ;
  243. }
  244. public bool ClassifyQuantifyParticles(List<COTSParticleClr> a_listAnalysisParticles, string libname)// classify particles
  245. {
  246. int nSize = (int)a_listAnalysisParticles.Count();
  247. // go through all analysis particles
  248. for (int i = 0; i < nSize; ++i)
  249. {
  250. COTSParticleClr pParticle = a_listAnalysisParticles[i];
  251. //CPosXrayClr pXray = a_listAnalysisParticles[i].GetXray();
  252. // CLEEngine engine = GetParticleEngine(libname.GetBuffer());
  253. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  254. if (!engine.Classify(pParticle))
  255. {
  256. // invalid x-ray pointer.
  257. log.Info("ClassifyParticle: can't identify the particle as any inclusion.");
  258. return false;
  259. }
  260. }
  261. // ok, return TRUE
  262. return true;
  263. }
  264. public bool ClassifySmallParticles(CFieldDataClean fld, string libname)
  265. {
  266. List<COTSParticleClr> a_listAnalysisParticles = fld.ListSmallParticles;
  267. int nSize = (int)a_listAnalysisParticles.Count();
  268. // go through all analysis particles
  269. for (int i = 0; i < nSize; ++i)
  270. {
  271. COTSParticleClr pParticle = a_listAnalysisParticles[i];
  272. IClassifyEngine engine = m_classifyEngine.GetCurveCompareEngine(libname);
  273. if (!engine.Classify(pParticle))
  274. {
  275. log.Info("ClassifyParticle: can't identify the particle as any inclusion.");
  276. return false;
  277. }
  278. }
  279. return true;
  280. }
  281. public bool SaveFieldFiles(CFieldDataClean f, string filedFileFoler)
  282. {
  283. string strFieldFileFolder = filedFileFoler;
  284. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  285. pBSEImgFileMgr.SetBSEImg(f.GetBSEImage());
  286. int nId = f.GetId();
  287. string sFieldId;
  288. sFieldId = nId.ToString();
  289. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  290. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  291. {
  292. log.Info("SaveFieldFiles: save BSE file failed.");
  293. return false;
  294. }
  295. // IncA Data list
  296. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  297. //pDBFileMgr.BeginTransaction();
  298. pDBFileMgr.SaveStatusDataToDB();
  299. if (!pDBFileMgr.SaveIncADataToDB(f.ListAnalysisParticles, f.GetPosition()))
  300. {
  301. log.Info("SaveFieldFiles: save inclusion file failed.");
  302. return false;
  303. }
  304. //save the xray data and element data.
  305. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  306. var m_listAnalysisPosXray = new List<CPosXrayClr>();
  307. foreach (var p in f. ListAnalysisParticles)
  308. {
  309. m_listAnalysisPosXray.Add(p.GetXray());
  310. }
  311. if (!PosXrayDBMgr.SaveXray(m_listAnalysisPosXray, true))
  312. {
  313. log.Info("SaveFieldFiles: save analysis x-ray failed.");
  314. return false;
  315. }
  316. //save the small particle info
  317. CSmallParticleInfoDB smallPartDB = pDBFileMgr.GetSmallParticleInfoDB();
  318. Dictionary<int, List<COTSParticleClr>> mapSmallParts = new Dictionary<int, List<COTSParticleClr>>();
  319. foreach (COTSParticleClr smallP in f.ListSmallParticles)
  320. {
  321. if (mapSmallParts.ContainsKey((int)smallP.GetType()))
  322. {
  323. mapSmallParts[(int)smallP.GetType()].Add(smallP);
  324. }
  325. else
  326. {
  327. var plist = new List<COTSParticleClr>();
  328. plist.Add(smallP);
  329. mapSmallParts.Add((int)smallP.GetType(), plist);
  330. }
  331. }
  332. List<CSmallParticleInfo> smallparts = new List<CSmallParticleInfo>();
  333. foreach (KeyValuePair<int, List<COTSParticleClr>> particles in mapSmallParts)
  334. {
  335. CSmallParticleInfo partInfo = new CSmallParticleInfo();
  336. partInfo.FieldId = particles.Value[0].GetFieldId();
  337. partInfo.AveGray = particles.Value[0].GetAveGray();
  338. partInfo.Quantity = (int)(particles.Value.Count / f.SmallParticlePercentage);
  339. partInfo.TypeId = particles.Key;
  340. partInfo.TypeColor = particles.Value[0].GetTypeColor();
  341. partInfo.TypeName = particles.Value[0].GetTypeName();
  342. double area = 0;
  343. foreach (var p in particles.Value)
  344. {
  345. area += p.GetArea();
  346. }
  347. //partInfo.Area = Convert.ToInt32(area / f.SmallParticlePercentage);
  348. partInfo.Area =(int) area ;
  349. smallparts.Add(partInfo);
  350. }
  351. foreach (CSmallParticleInfo smallp in smallparts)
  352. {
  353. smallPartDB.SaveAKindOfSmallParticle(smallp, new CPosXrayClr(), new System.Drawing.Point(0, 0));
  354. }
  355. //pDBFileMgr.CommitTransaction();
  356. return true;
  357. }
  358. }
  359. }