SmplMeasureCleanliness.cs 18 KB

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