SmplMeasureCleanliness.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. ClassifyFieldParticles();
  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. fld.ListSmallParticles = smallparts;
  116. fld.ListBigParticles = bigparts;
  117. //fld.SmallParticlePercentage=percentage;
  118. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  119. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2")+ "): " + bigparts.Count);
  120. return ;
  121. }
  122. public void CollectParticlesXrayData(CFieldDataClean fld)
  123. {
  124. // get x-ray parameters
  125. COTSXRayParam pXRayParam = m_Sample.GetMsrParams ().GetXRayParam();
  126. // calculate search x-ray acquire time
  127. uint nXRayAQTime;
  128. var smallparts = fld.ListSmallParticles;
  129. var bigparts = fld.ListBigParticles;
  130. var pStatus = m_Sample.GetMsrStatus();
  131. //// particle x-ray analysis
  132. ////=============================================
  133. curFldData.CreateXrayList(bigparts); // big particle using the full xray strategy.
  134. curFldData.CreateXrayList(smallparts); //small particle using the fast xray strategy
  135. // get x-ray list (analysis) by particle features
  136. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  137. m_EDSHardwareMgr.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  138. nXRayAQTime = (uint)pXRayParam.GetFastXrayTime();
  139. // get x-ray list (analysis) by points
  140. m_EDSHardwareMgr.GetXRayByPoints(smallparts, nXRayAQTime, true);
  141. return ;
  142. }
  143. public void ParticleSpecialTreatment(CFieldDataClean fld)
  144. {
  145. //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.
  146. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>(); //std.map<COTSParticlePtr, int> mapPartXray = new std.map<COTSParticlePtr, int>();
  147. double edsTime = 0; //to store the EDSTime returned from the engine.
  148. var bigparts = fld.ListBigParticles;
  149. var m_ClassifyEngine = new CClassifyEngine();
  150. for (int i = 0; i < bigparts.Count(); i++)
  151. {
  152. var engine = m_ClassifyEngine.GetParticleEngine(m_Sample.GetMsrParams().GetSTDName());
  153. var p = bigparts[i];
  154. // 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.
  155. //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.
  156. edsTime = engine.IfNeedMaxEDS(p);
  157. if (edsTime > 0)
  158. {
  159. mapPartXray[p] = i;
  160. }
  161. }
  162. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  163. if (mapPartXray.Count() > 0)
  164. {
  165. foreach (var p in mapPartXray)
  166. {
  167. partsMax.Add(p.Key);
  168. }
  169. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  170. m_EDSHardwareMgr.GetXRayByFeatures(partsMax, edsTime, true);
  171. }
  172. }
  173. public override void ClassifyFieldParticles()
  174. {
  175. try
  176. {
  177. CFieldDataClean curFldDataCln = (CFieldDataClean)curFldData;
  178. string libname = m_Sample.GetMsrParams().GetSTDName();
  179. if (libname != "NoSTDDB")
  180. {
  181. log.Info("Begin to classify big particles!Using " + libname);
  182. var bigparts = curFldDataCln.ListBigParticles;
  183. ClassifyQuantifyParticles(bigparts,libname);
  184. var smallParts = curFldDataCln.ListSmallParticles;
  185. ClassifySmallParticles(smallParts, libname);
  186. }
  187. }
  188. catch (Exception e)
  189. {
  190. log.Info("calcu the particle image property or classify failed. "+e.Message);
  191. }
  192. }
  193. public override void ClassifyMergedParticles(List<COTSParticleClr> mergedParts)
  194. {
  195. try
  196. {
  197. try
  198. {
  199. string libname = m_Sample.GetMsrParams().GetSTDName();
  200. if (libname != "NoSTDDB")
  201. {
  202. log.Info("Begin to classify big particles!Using " + libname);
  203. var bigparts = mergedParts;
  204. ClassifyQuantifyParticles(bigparts, libname);
  205. }
  206. }
  207. catch (Exception e)
  208. {
  209. log.Info("calcu the particle image property or classify failed. " + e.Message);
  210. }
  211. }
  212. catch (Exception e)
  213. {
  214. log.Info(" classify failed. " + e.Message);
  215. }
  216. }
  217. public void SaveFieldParticlesData()
  218. {
  219. StartSaveFileThread(curFldData);
  220. }
  221. public void GetOriginalParticles()
  222. {
  223. // measure status
  224. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  225. // get image process parameter
  226. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  227. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  228. // remove BES image background
  229. curFldData.RemoveBSEImageBG(pImgProcessParam);
  230. // check if this is an empty image
  231. if (curFldData.NoParticle())
  232. { // empty fields
  233. log.Info("ImageProcess: empty field.");
  234. //return false;
  235. }
  236. return ;
  237. }
  238. public bool ClassifyQuantifyParticles(List<COTSParticleClr> a_listAnalysisParticles, string libname)// classify particles
  239. {
  240. int nSize = (int)a_listAnalysisParticles.Count();
  241. // go through all analysis particles
  242. for (int i = 0; i < nSize; ++i)
  243. {
  244. COTSParticleClr pParticle = a_listAnalysisParticles[i];
  245. //CPosXrayClr pXray = a_listAnalysisParticles[i].GetXray();
  246. // CLEEngine engine = GetParticleEngine(libname.GetBuffer());
  247. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  248. if (!engine.Classify(pParticle))
  249. {
  250. // invalid x-ray pointer.
  251. log.Info("ClassifyParticle: can't identify the particle as any inclusion.");
  252. return false;
  253. }
  254. }
  255. // ok, return TRUE
  256. return true;
  257. }
  258. public bool ClassifySmallParticles(List<COTSParticleClr> smallparts, string libname)
  259. {
  260. List<COTSParticleClr> a_listAnalysisParticles = smallparts;
  261. int nSize = (int)a_listAnalysisParticles.Count();
  262. // go through all analysis particles
  263. for (int i = 0; i < nSize; ++i)
  264. {
  265. COTSParticleClr pParticle = a_listAnalysisParticles[i];
  266. IClassifyEngine engine = m_classifyEngine.GetCurveCompareEngine(libname);
  267. if (!engine.Classify(pParticle))
  268. {
  269. log.Info("ClassifyParticle: can't identify the particle as any inclusion.");
  270. return false;
  271. }
  272. }
  273. return true;
  274. }
  275. public bool SaveFieldFiles(CFieldDataClean f, string filedFileFoler)
  276. {
  277. string strFieldFileFolder = filedFileFoler;
  278. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  279. pBSEImgFileMgr.SetBSEImg(f.GetBSEImage());
  280. int nId = f.GetId();
  281. string sFieldId;
  282. sFieldId = nId.ToString();
  283. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  284. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  285. {
  286. log.Info("SaveFieldFiles: save BSE file failed.");
  287. return false;
  288. }
  289. // IncA Data list
  290. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  291. //pDBFileMgr.BeginTransaction();
  292. pDBFileMgr.SaveStatusDataToDB();
  293. if (!pDBFileMgr.SaveIncADataToDB(f.ListAnalysisParticles, f.GetPosition()))
  294. {
  295. log.Info("SaveFieldFiles: save inclusion file failed.");
  296. return false;
  297. }
  298. //save the xray data and element data.
  299. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  300. var m_listAnalysisPosXray = new List<CPosXrayClr>();
  301. foreach (var p in f. ListAnalysisParticles)
  302. {
  303. m_listAnalysisPosXray.Add(p.GetXray());
  304. }
  305. if (!PosXrayDBMgr.SaveXray(m_listAnalysisPosXray, true))
  306. {
  307. log.Info("SaveFieldFiles: save analysis x-ray failed.");
  308. return false;
  309. }
  310. //save the small particle info
  311. CSmallParticleInfoDB smallPartDB = pDBFileMgr.GetSmallParticleInfoDB();
  312. Dictionary<int, List<COTSParticleClr>> mapSmallParts = new Dictionary<int, List<COTSParticleClr>>();
  313. foreach (COTSParticleClr smallP in f.ListSmallParticles)
  314. {
  315. if (mapSmallParts.ContainsKey((int)smallP.GetType()))
  316. {
  317. mapSmallParts[(int)smallP.GetType()].Add(smallP);
  318. }
  319. else
  320. {
  321. var plist = new List<COTSParticleClr>();
  322. plist.Add(smallP);
  323. mapSmallParts.Add((int)smallP.GetType(), plist);
  324. }
  325. }
  326. List<CSmallParticleInfo> smallparts = new List<CSmallParticleInfo>();
  327. foreach (KeyValuePair<int, List<COTSParticleClr>> particles in mapSmallParts)
  328. {
  329. CSmallParticleInfo partInfo = new CSmallParticleInfo();
  330. partInfo.FieldId = particles.Value[0].GetFieldId();
  331. partInfo.AveGray = particles.Value[0].GetAveGray();
  332. partInfo.Quantity = (int)(particles.Value.Count / f.SmallParticlePercentage);
  333. partInfo.TypeId = particles.Key;
  334. partInfo.TypeColor = particles.Value[0].GetTypeColor();
  335. partInfo.TypeName = particles.Value[0].GetTypeName();
  336. double area = 0;
  337. foreach (var p in particles.Value)
  338. {
  339. area += p.GetArea();
  340. }
  341. //partInfo.Area = Convert.ToInt32(area / f.SmallParticlePercentage);
  342. partInfo.Area =(int) area ;
  343. smallparts.Add(partInfo);
  344. }
  345. foreach (CSmallParticleInfo smallp in smallparts)
  346. {
  347. smallPartDB.SaveAKindOfSmallParticle(smallp, new CPosXrayClr(), new System.Drawing.Point(0, 0));
  348. }
  349. //pDBFileMgr.CommitTransaction();
  350. return true;
  351. }
  352. }
  353. }