123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- 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, double pixelSize, byte[] BSEImage, int iHeight, int iWidth, ref byte[] BSEImageNoBG)
- {
- Rectangle rect = new Rectangle();
- rect.Height = iHeight;
- rect.Width = iWidth;
- CBSEImgClr pBSEImageIn = new CBSEImgClr(rect);
- CBSEImgClr pBSEImageOut = new CBSEImgClr(rect);
-
-
- //pBSEImageIn.SetImageRect(rect);
- pBSEImageIn.SetImageData(BSEImage,iWidth, iHeight );
- //pBSEImageOut.SetImageRect(rect);
- if (null == ImgProcPrm)
- {
- return false;
- }
-
- RemoveBackGround(pBSEImageIn, ImgProcPrm, pixelSize ,ref pBSEImageOut);
-
- BSEImageNoBG = pBSEImageOut.GetImageDataPtr();
- return true;
- }
- /// <summary>
- /// 获取测量的BSE图
- /// </summary>
- /// <param name="BSEImage">BSE原数据</param>
- /// <param name="iHeight">图像高度</param>
- /// <param name="iWidth">图像宽度</param>
- /// <param name="grayStart"></param>
- /// <param name="grayEnd"></param>
- /// <param name="BSEImageNoBG">去背景图数据</param>
- /// <returns></returns>
- public bool GetBSEImage(byte[] BSEImage, int iHeight, int iWidth, int grayStart, int grayEnd, ref byte[] BSEImageNoBG)
- {
- Rectangle rect = new Rectangle();
- rect.Height = iHeight;
- rect.Width = iWidth;
- CBSEImgClr pBSEImageIn = new CBSEImgClr(rect);
- CBSEImgClr pBSEImageOut = new CBSEImgClr(rect);
-
- //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);
- for (int i = 0; i < iWidth; i++)
- {
- for (int j = 0; j < iHeight; j++)
- {
- var bse = pBSEImageOut.GetBSEValue(i, j);
- if (bse == 255)
- {
- var originalBse = pBSEImageIn.GetBSEValue(i, j);
- pBSEImageOut.SetBSEValue(i, j, originalBse);
- }
- else
- {
- pBSEImageOut.SetBSEValue(i, j, 255);
- }
-
-
- }
-
- }
-
- BSEImageNoBG = pBSEImageOut.GetImageDataPtr();
- return true;
- }
- // remove background
- public void RemoveBackGround(CBSEImgClr a_pImgIn, COTSImageProcParam a_pImgProcessParam, double a_pixelSize, ref CBSEImgClr a_pImgOut)
- {
-
- List<COTSParticleClr> parts = new List<COTSParticleClr>();
- List<COTSParticleClr> specialGreyparts = new List<COTSParticleClr>();
- if (!RemoveBGAndGetParts(a_pImgIn, a_pImgProcessParam, a_pixelSize, ref parts))
- {
- return;
- }
- if (a_pImgProcessParam.SpecialGreyRangeParam.GetIsToRun())
- {
- var param = a_pImgProcessParam.SpecialGreyRangeParam;
- var ranges = param.GetSpecialGreyRanges();
-
-
- foreach (var r in ranges)
- {
- CIntRangeClr r1 = new CIntRangeClr();
- r1.SetStart(r.range.GetStart());
- r1.SetEnd(r.range.GetEnd());
- CDoubleRangeClr r2 = new CDoubleRangeClr();
- r2.SetStart(r.diameterRange.GetStart());
- r2.SetEnd(r.diameterRange.GetEnd());
- GetParticlesBySpecialGray(a_pImgIn, r1, r2, a_pixelSize, ref specialGreyparts);
- }
-
- }
- for (int i = 0; i < a_pImgOut.GetWidth(); i++)
- {
- for (int j = 0; j < a_pImgOut.GetHeight(); j++)
- {
- a_pImgOut.SetBSEValue(i, j, 255);
- }
- }
- if (specialGreyparts.Count > 0)
- {
- foreach (var p in specialGreyparts)
- {
- foreach (var s in p.GetFeature().GetSegmentsList())
- {
- for (int i = s.GetStart(); i < s.GetStart() + s.GetLength(); i++)
- {
- var bseValue = a_pImgIn.GetBSEValue(i, s.GetHeight());
- a_pImgOut.SetBSEValue(i, s.GetHeight(), bseValue);
- }
- }
- }
- }
- foreach (var p in parts)
- {
- foreach (var s in p.GetFeature().GetSegmentsList())
- {
- for (int i = s.GetStart(); i < s.GetStart() + s.GetLength(); i++)
- {
- var bseValue = a_pImgIn.GetBSEValue(i, s.GetHeight());
- a_pImgOut.SetBSEValue(i, s.GetHeight(), bseValue);
- }
- }
-
- }
-
-
-
- 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 RemoveBGAndGetParts(CBSEImgClr img, COTSImageProcParam a_pImgProcessParam,double a_pixelSize,ref List<COTSParticleClr> parts)
- {
-
- OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr();
-
- OTSCLRINTERFACE.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam);
- OTSCLRINTERFACE.COTSFieldDataClr flddataclr = new OTSCLRINTERFACE.COTSFieldDataClr();
- if (!imgpro.GetFieldDataFromImage(img, prm, a_pixelSize, flddataclr))
- {
- return false;
- }
-
- parts = flddataclr.GetParticleList();
- return true;
- }
- public bool GetParticlesBySpecialGray(CBSEImgClr img, CIntRangeClr grayrange, CDoubleRangeClr diameterRange,double a_pixelSize, ref List<COTSParticleClr> 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());
- prmclr.SetErrodDilateParam(a_oSource.GetErrodDilateParam());
- return prmclr;
- }
- public bool MergeBigBoundaryParticles(List<COTSFieldData> allFields, double pixelSize, int scanFieldSize, Size ResolutionSize, ref List<COTSParticleClr> mergedParts)
- {
- List<COTSFieldDataClr> fldclrs = new List<COTSFieldDataClr>();
- foreach (var f in allFields)
- {
- COTSFieldDataClr fldclr = new COTSFieldDataClr();
- fldclr.SetPosition(f.GetOTSPosition());
- fldclr.SetImageWidth(f.Width);
- fldclr.SetImageHeight(f.Height);
- var parts = f.GetListAnalysisParticles();
- foreach (var p in parts)
- {
- fldclr.AddParticle(p);
- }
- fldclrs.Add(fldclr);
- }
- OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr();
- imgpro.MergeBigBoundaryParticles(fldclrs, pixelSize, scanFieldSize, ResolutionSize, mergedParts);
- return true;
- }
-
- }
- }
|