123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using OTSCOMMONCLR;
- using OTSDataType;
- using OTSModelSharp.Measure.OTSCleanliness;
- using OTSModelSharp.ServiceCenter;
- using OTSModelSharp.ServiceInterface;
- namespace OTSModelSharp
- {
- class CSmplMeasureCleanliness : CSmplMeasure
- {
- public CSmplMeasureCleanliness(string a_strWorkingFolder, COTSSample a_pSample):base(a_strWorkingFolder,a_pSample)
- {
- SetWorkingFolder(a_strWorkingFolder);
- SetSample(a_pSample);
- m_classifyEngine = new CClassifyEngine();
- }
- // field image process
- public override bool FieldImageProcess(Point fldCenter, CBSEImgClr a_BSEImg)
- {
- int nNewFieldId;
- nNewFieldId = m_pSampleRstFile.GetIdForANewField();
- //first step:remove background of the bse image and compound all the finded particles.
- CFieldDataClean curFldDataMgr = new CFieldDataClean( a_BSEImg,m_Sample.CalculatePixelSize());
- curFldDataMgr.SetId(nNewFieldId);
- curFldDataMgr.SetPosition(fldCenter);
- if (!GetOriginalParticles())
- {
- StartSaveFileThread(curFldDataMgr);
- loger.Error("ImageProcess: failed to find any particles.");
- return false;
- }
- // second step :filter the finded particles.
- if (!FilterParticles(curFldDataMgr))
- {
- StartSaveFileThread(curFldDataMgr);
- loger.Info("ImageProcess: there is no particles to be analyzed.");
- return false;
- }
- //collect xray data.
- if (!CollectParticlesXrayData(curFldDataMgr))
- {
- StartSaveFileThread(curFldDataMgr);
- loger.Info("ImageProcess: there is no particles to be analyzed.");
- return false;
- }
- //special treatment.
- //ParticleSpecialTreatment();
- loger.Info("Begin to Calculate the image property of every particle!");
- var analysisparts = curFldDataMgr.ListBigParticles;
- curFldDataMgr.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
- ClassifyParticles(curFldDataMgr);
-
- // save field files
- m_Sample.GetMsrStatus() .SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
- StartSaveFileThread(curFldDataMgr);
- return true;
- }
- // save field data
- protected void SaveFieldMgrData()
- {
- while (bSaveThreadWorking)
- {
- while (fieldQueue.Count() > 0)
- {
- CFieldDataClean f = (CFieldDataClean) fieldQueue.Dequeue();
- double pixelSize = m_Sample.CalculatePixelSize();
-
- //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
- SaveFieldFiles(f,m_pSampleRstFile.GetFieldFileSubFolderStr());
- }
- if (fieldQueue.Count() == 0)
- {
- bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
- return;
- }
- }
- return;
- }
- protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
- {
-
- fieldQueue.Enqueue(a_pFieldMgr);
- if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
- {
- if (bSaveThreadWorking == false)
- {
- bSaveThreadWorking = true;
- m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
- m_thread_ptr.Start();//m_thread_ptr->detach();
- }
- }
- }
- public bool FilterParticles(CFieldDataClean fld)
- {
- // 1)remove over sized particles, too small particles and get a analysis particles list
-
- double dPixelSize = m_Sample.CalculatePixelSize();
-
-
- CSampleParam pMsrParam = m_Sample.GetMsrParams();
- COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
- curFldData.FilterParticles(pImgProcessParam, dPixelSize);
- if (curFldData.NoAnalysisParticle())
- {
- return false;
- }
- //2) according to the quantify threshold size value saperate the analysis particles into two group :the bigparticles and the smallparticles.
- int quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetQuantifyMinSize();
- var smallparts = fld.ListSmallParticles;
- var bigparts = fld.ListBigParticles;
- foreach (var part in curFldData.ListAnalysisParticles)
- {
- if (part.GetArea() < quantifyThreshold)
- {
- smallparts.Add(part);
- }
- else
- {
- bigparts.Add(part);
- }
- }
- // 3) according to the presettings,make sure we won't do the xray processing work for too many small particles.
- //var fieldParam = m_Sample.GetMsrParams().GetImageScanParam();
- //double smallPartnum;
- // List<COTSParticleClr> smallPartList = new List<COTSParticleClr>();
- //double percentage = 100;
- //switch (fieldParam.GetFieldStopMode())
- //{
- // case otsdataconst.OTS_MEASURE_FIELD_STOP_MODE.ParticlesPercentage:
- // percentage = fieldParam.GetFieldSmallParticlesPercentage();
- // percentage = percentage / 100;// according to the percentage
- // smallPartnum = smallparts.Count() * percentage;
- // for (int i = 0; i < smallPartnum; i++)
- // {
- // smallPartList.Add(smallparts[i]);
- // }
- // break;
- // case otsdataconst.OTS_MEASURE_FIELD_STOP_MODE.Particles:
- // percentage = 1;
- // smallPartnum = fieldParam.GetFieldSmallParticleNum();//according to the numbers.
- // if (smallPartnum < smallparts.Count())
- // {
- // for (int i = 0; i < smallPartnum; i++)
- // {
- // smallPartList.Add(smallparts[i]);
- // }
- // }
- // break;
- // default:
- // percentage = 1;
- // smallPartList = smallparts;
- // break;
- //}
- fld.ListSmallParticles = smallparts;
- fld.ListBigParticles = bigparts;
- //fld.SmallParticlePercentage=percentage;
- loger.Info("SmallQuantifyParts: " + smallparts.Count);
- loger.Info("BigQuantifyParts: " + bigparts.Count);
- return true;
- }
- public bool CollectParticlesXrayData(CFieldDataClean fld)
- {
- // get x-ray parameters
- COTSXRayParam pXRayParam = m_Sample.GetMsrParams ().GetXRayParam();
- // calculate search x-ray acquire time
- uint nXRayAQTime;
- var smallparts = fld.ListSmallParticles;
- var bigparts = fld.ListBigParticles;
- var pStatus = m_Sample.GetMsrStatus();
-
-
- //// particle x-ray analysis
- ////=============================================
- curFldData.CreateXrayList(bigparts); // big particle using the full xray strategy.
- curFldData.CreateXrayList(smallparts); //small particle using the fast xray strategy
-
- // get x-ray list (analysis) by particle features
- nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
- if (!m_EDSHardwareMgr.GetXRayByFeatures(bigparts, nXRayAQTime, true))
- {
- loger.Error("ImageProcess: call GetXRayByFeatures method.");
- pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
- StartSaveFileThread(curFldData);
- return false;
- }
-
- nXRayAQTime = (uint)pXRayParam.GetFastXrayTime();
- // get x-ray list (analysis) by points
- if (!m_EDSHardwareMgr.GetXRayByPoints(smallparts, nXRayAQTime, true))
- { // failed to call GetXRayByPoints method
- loger.Error("ImageProcess: call GetXRayByPoints method.");
- pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
- StartSaveFileThread(curFldData);
- return false;
- }
- return true;
- }
- public void ParticleSpecialTreatment(CFieldDataClean fld)
- {
- //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.
- Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>(); //std.map<COTSParticlePtr, int> mapPartXray = new std.map<COTSParticlePtr, int>();
- double edsTime = 0; //to store the EDSTime returned from the engine.
- var bigparts = fld.ListBigParticles;
- var m_ClassifyEngine = new CClassifyEngine();
- for (int i = 0; i < bigparts.Count(); i++)
- {
- var engine = m_ClassifyEngine.GetParticleEngine(m_Sample.GetMsrParams().GetSTDName());
- var p = bigparts[i];
-
- // 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.
- //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.
- edsTime = engine.IfNeedMaxEDS(p);
- if (edsTime > 0)
- {
- mapPartXray[p] = i;
- }
- }
- List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
- if (mapPartXray.Count() > 0)
- {
- foreach (var p in mapPartXray)
- {
- partsMax.Add(p.Key);
- }
- List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
- if (!m_EDSHardwareMgr.GetXRayByFeatures(partsMax, edsTime, true))
- {
- loger.Error("ImageProcess: call GetXRayByFeatures method for MaxEDS");
- m_Sample.GetMsrStatus(). SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
- StartSaveFileThread(curFldData);
- return;
- }
- }
- }
- public void ClassifyParticles(CFieldDataClean fld)
- {
-
- try
- {
-
- string libname = m_Sample.GetMsrParams().GetSTDName();
- if (libname != "NoSTDDB")
- {
- loger.Info("Begin to classify big particles!Using " + libname);
- var bigparts = fld.ListBigParticles;
- ClassifyQuantifyParticles(bigparts,libname);
- ClassifySmallParticles(fld, libname);
-
- }
- }
- catch (Exception e)
- {
- loger.Info("calcu the particle image property or classify failed. "+e.Message);
- }
- }
- public void SaveFieldParticlesData()
- {
- StartSaveFileThread(curFldData);
- }
- public bool GetOriginalParticles()
- {
- // measure status
- CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
-
- // get image process parameter
- CSampleParam pMsrParam = m_Sample.GetMsrParams();
- COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
- // remove BES image background
- if (!curFldData.RemoveBSEImageBG(pImgProcessParam))
- { // failed to call RemoveBSEImageBG method
- loger.Error("ImageProcess: call RemoveBSEImageBG method.");
- pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
- return false;
- }
- // check if this is an empty image
- if (curFldData.NoParticle())
- { // empty fields
- loger.Info("ImageProcess: empty field.");
- return false;
- }
- return true;
- }
- public bool ClassifyQuantifyParticles(List<COTSParticleClr> a_listAnalysisParticles, string libname)// classify particles
- {
- int nSize = (int)a_listAnalysisParticles.Count();
- // go through all analysis particles
- for (int i = 0; i < nSize; ++i)
- {
- COTSParticleClr pParticle = a_listAnalysisParticles[i];
- //CPosXrayClr pXray = a_listAnalysisParticles[i].GetXray();
- // CLEEngine engine = GetParticleEngine(libname.GetBuffer());
- IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
- if (!engine.Classify(pParticle))
- {
- // invalid x-ray pointer.
- loger.Info("ClassifyParticle: can't identify the particle as any inclusion.");
- return false;
- }
- }
- // ok, return TRUE
- return true;
- }
- public bool ClassifySmallParticles(CFieldDataClean fld, string libname)
- {
- List<COTSParticleClr> a_listAnalysisParticles = fld.ListSmallParticles;
- int nSize = (int)a_listAnalysisParticles.Count();
- // go through all analysis particles
- for (int i = 0; i < nSize; ++i)
- {
- COTSParticleClr pParticle = a_listAnalysisParticles[i];
-
- IClassifyEngine engine = m_classifyEngine.GetCurveCompareEngine(libname);
- if (!engine.Classify(pParticle))
- {
- loger.Info("ClassifyParticle: can't identify the particle as any inclusion.");
- return false;
- }
- }
- return true;
- }
- public bool SaveFieldFiles(CFieldDataClean f, string filedFileFoler)
- {
- string strFieldFileFolder = filedFileFoler;
- CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
- pBSEImgFileMgr.SetBSEImg(f.GetBSEImage());
- int nId = f.GetId();
- string sFieldId;
- sFieldId = nId.ToString();
-
- string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
- if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
- {
- loger.Info("SaveFieldFiles: save BSE file failed.");
- return false;
- }
- // IncA Data list
- CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
- pDBFileMgr.BeginTransaction();
- pDBFileMgr.SaveStatusDataToDB();
- if (!pDBFileMgr.SaveIncADataToDB(f.ListAnalysisParticles, f.GetPosition()))
- {
- loger.Info("SaveFieldFiles: save inclusion file failed.");
- return false;
- }
- //save the xray data and element data.
- CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
- var m_listAnalysisPosXray = new List<CPosXrayClr>();
- foreach (var p in f. ListAnalysisParticles)
- {
- m_listAnalysisPosXray.Add(p.GetXray());
- }
- if (!PosXrayDBMgr.SaveXray(m_listAnalysisPosXray, true))
- {
- loger.Info("SaveFieldFiles: save analysis x-ray failed.");
- return false;
- }
- //save the small particle info
- CSmallParticleInfoDB smallPartDB = pDBFileMgr.GetSmallParticleInfoDB();
- Dictionary<int, List<COTSParticleClr>> mapSmallParts = new Dictionary<int, List<COTSParticleClr>>();
- foreach (COTSParticleClr smallP in f.ListSmallParticles)
- {
- if (mapSmallParts.ContainsKey((int)smallP.GetType()))
- {
- mapSmallParts[(int)smallP.GetType()].Add(smallP);
- }
- else
- {
- var plist = new List<COTSParticleClr>();
- plist.Add(smallP);
- mapSmallParts.Add((int)smallP.GetType(), plist);
- }
- }
- List<CSmallParticleInfo> smallparts = new List<CSmallParticleInfo>();
- foreach (KeyValuePair<int, List<COTSParticleClr>> particles in mapSmallParts)
- {
- CSmallParticleInfo partInfo = new CSmallParticleInfo();
- partInfo.FieldId = particles.Value[0].GetFieldId();
- partInfo.AveGray = particles.Value[0].GetAveGray();
- partInfo.Quantity = (int)(particles.Value.Count / f.SmallParticlePercentage);
- partInfo.TypeId = particles.Key;
- partInfo.TypeColor = particles.Value[0].GetTypeColor();
- partInfo.TypeName = particles.Value[0].GetTypeName();
- double area = 0;
- foreach (var p in particles.Value)
- {
- area += p.GetArea();
- }
- //partInfo.Area = Convert.ToInt32(area / f.SmallParticlePercentage);
- partInfo.Area =(int) area ;
- smallparts.Add(partInfo);
- }
- foreach (CSmallParticleInfo smallp in smallparts)
- {
- smallPartDB.SaveAKindOfSmallParticle(smallp, new CPosXrayClr(), new System.Drawing.Point(0, 0));
- }
- pDBFileMgr.CommitTransaction();
- return true;
- }
- }
- }
|