using OTSDataType; using OTSModelSharp.ImageProcess; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using static OTSDataType.otsdataconst; namespace OTSModelSharp.ServiceInterface { using OTSCLRINTERFACE; using OTSIMGPROC; using System.Windows; public class CImageHandler:IImageProcess { //获取测量的BSE图 //COTSSample WSample: 工作样品测量 //Byte[] BSEImage: 带背景图数据 //int iHeight: 图像高度 //int iWidth: 图像宽度 //Byte[]BSEImageNoBG : 去背景图数据 public bool GetBSEImage(COTSImageProcParam ImgProcPrm, byte[] BSEImage, int iHeight, int iWidth, ref byte[] BSEImageNoBG) { CBSEImgClr pBSEImageIn = new CBSEImgClr(); CBSEImgClr pBSEImageOut = new CBSEImgClr(); Rectangle rect = new Rectangle(); rect.Height = iHeight; rect.Width = iWidth; pBSEImageIn.SetImageRect(rect); pBSEImageIn.SetImageData(BSEImage,iWidth, iHeight ); if (null == ImgProcPrm) { return false; } //COTSImageProcParam ImgProcPrm = WSample.GetMsrParams().GetImageProcessParam(); RemoveBackGround(pBSEImageIn, ImgProcPrm, ref pBSEImageOut); BSEImageNoBG = pBSEImageOut.GetImageDataPtr(); return true; } /// /// 获取测量的BSE图 /// /// BSE原数据 /// 图像高度 /// 图像宽度 /// /// /// 去背景图数据 /// public bool GetBSEImage(byte[] BSEImage, int iHeight, int iWidth, int grayStart, int grayEnd, ref byte[] BSEImageNoBG) { CBSEImgClr pBSEImageIn = new CBSEImgClr(); CBSEImgClr pBSEImageOut = new CBSEImgClr(); Rectangle rect = new Rectangle(); rect.Height = iHeight; rect.Width = iWidth; pBSEImageIn.SetImageRect(rect); pBSEImageIn.SetImageData(BSEImage, iWidth, iHeight ); CIntRangeClr cIntRangeClr = new CIntRangeClr(); cIntRangeClr.SetStart(grayStart); cIntRangeClr.SetEnd(grayEnd); OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr(); int num=0; imgpro.GetSpecialGrayRangeImage(pBSEImageIn, cIntRangeClr, pBSEImageOut,ref num); BSEImageNoBG = pBSEImageOut.GetImageDataPtr(); return true; } // remove background public void RemoveBackGround(CBSEImgClr a_pImgIn, COTSImageProcParam a_pImgProcessParam, ref CBSEImgClr a_pImgOut) { OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr(); OTSCLRINTERFACE.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam); int pixelnum=0; CBSEImgClr imgout = new CBSEImgClr(); imgpro.RemoveBackGround(a_pImgIn, prm, imgout, ref pixelnum);// the background pixel will be 0,and the other part will be 255. // Binary int nWidth = a_pImgIn.GetWidth(); int nHeight = a_pImgIn.GetHeight(); int nImgSize = nWidth * nHeight; byte[] pSrcImg = a_pImgIn.GetImageDataPtr(); byte[] pPixel = imgout.GetImageDataPtr(); byte[] pTargetImg = new byte[nImgSize]; // delete background for (uint i = 0; i < nImgSize; i++) { if (pPixel[i]==0) { pTargetImg[i] = 255; } else { byte pixelGray = pSrcImg[i]; pTargetImg[i] = pixelGray; } } a_pImgOut.SetImageData(pTargetImg, nWidth, nHeight); return; } public void BDilate3(string source, string target, int wDegree, int rows, int columns) { throw new NotImplementedException(); } public void BErode3(string source, string target, int wDegree, int rows, int columns) { throw new NotImplementedException(); } public bool CalParticleImageProp( COTSParticleClr part, double a_pixelSize) { OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr(); imgpro.CalcuParticleImagePropertes(part,a_pixelSize); return true; } public bool RemoveBSEImageBG(CBSEImgClr img, COTSImageProcParam a_pImgProcessParam,double a_pixelSize,ref List parts) { OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr(); OTSCLRINTERFACE.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam); OTSCLRINTERFACE.COTSFieldDataClr flddataclr = new OTSCLRINTERFACE.COTSFieldDataClr(); imgpro.GetFieldDataFromImage(img, prm, a_pixelSize, flddataclr); parts = flddataclr.GetParticleList(); return true; } public bool GetParticlesBySpecialGray(CBSEImgClr img, CIntRangeClr grayrange, CIntRangeClr diameterRange,double a_pixelSize, ref List parts) { OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr(); //OTSCLRINTERFACE.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam); OTSCLRINTERFACE.COTSFieldDataClr flddataclr = new OTSCLRINTERFACE.COTSFieldDataClr(); imgpro.GetParticlesBySpecialPartGrayRange(img, grayrange,diameterRange,a_pixelSize, flddataclr); parts = flddataclr.GetParticleList(); return true; } private COTSImgProcPrmClr GetImageProcPrmClr(COTSImageProcParam a_oSource) { COTSImgProcPrmClr prmclr = new COTSImgProcPrmClr(); OTSCLRINTERFACE.CDoubleRangeClr r1 = new OTSCLRINTERFACE.CDoubleRangeClr(a_oSource.GetIncAreaRange().GetStart(), a_oSource.GetIncAreaRange().GetEnd()); prmclr.SetIncArea(r1); OTSCLRINTERFACE.CIntRangeClr r2= new OTSCLRINTERFACE.CIntRangeClr(a_oSource.GetBGGray().GetStart(), a_oSource.GetBGGray().GetEnd()); prmclr.SetBGGray(r2); OTSCLRINTERFACE.CIntRangeClr r3= new OTSCLRINTERFACE.CIntRangeClr(a_oSource.GetParticleGray().GetStart(), a_oSource.GetParticleGray().GetEnd()); prmclr.SetParticleGray(r3); prmclr.SetBGRemoveType((int)a_oSource.GetBGRemoveType()); prmclr.SetAutoBGRemoveType((int)a_oSource.GetAutoBGRemoveType()); return prmclr; } public bool MergeBigBoundaryParticles(List allFields, double pixelSize, int scanFieldSize, Size ResolutionSize, ref List mergedParts) { List fldclrs = new List(); foreach (var f in allFields) { COTSFieldDataClr fldclr = new COTSFieldDataClr(); fldclr.SetPosition(f.GetOTSPosition()); fldclr.SetImageWidth(f.Width); fldclr.SetImageHeight(f.Height); foreach (var p in f.GetListAnalysisParticles()) { fldclr.AddParticle(p); } fldclrs.Add(fldclr); } OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr(); imgpro.MergeBigBoundaryParticles(fldclrs, pixelSize, scanFieldSize, ResolutionSize, mergedParts); return true; } } }