| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 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 OTSCOMMONCLR;
- using OTSIMGPROC;
-
- using System.Windows;
- public class CImageHandler:IImageProcess
- {
- //获取测量的BSE图
- //COTSSample WSample: 工作样品测量
- //Byte[] BSEImage: 带背景图数据
- //int iHeight: 图像高度
- //int iWidth: 图像宽度
- //Byte[]BSEImageNoBG : 去背景图数据
- public bool GetBSEImage(COTSSample WSample, 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 == WSample)
- {
- return false;
- }
- COTSImageProcParam ImgProcPrm = WSample.GetMsrParams().GetImageProcessParam();
- RemoveBackGround(pBSEImageIn, ImgProcPrm, 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)
- {
- 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 );
- COTSImageProcParam ImgProcPrm = new COTSImageProcParam();
- CIntRange cIntRangeClr = new CIntRange();
- cIntRangeClr.SetStart(grayStart);
- cIntRangeClr.SetEnd(grayEnd);
- ImgProcPrm.SetBGGray(cIntRangeClr);
- RemoveBackGround(pBSEImageIn, ImgProcPrm, ref pBSEImageOut);
-
- BSEImageNoBG = pBSEImageOut.GetImageDataPtr();
- return true;
- }
- // remove background
- public void RemoveBackGround(CBSEImgClr a_pImgIn, COTSImageProcParam a_pImgProcessParam, ref CBSEImgClr a_pImgOut)
- {
- OTSIMGPROC.ImageProForClr imgpro = new OTSIMGPROC.ImageProForClr();
- OTSIMGPROC.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)
- {
- OTSIMGPROC.ImageProForClr imgpro = new OTSIMGPROC.ImageProForClr();
- imgpro.CalcuParticleImagePropertes(part,a_pixelSize);
-
- return true;
- }
-
- public bool RemoveBSEImageBG(CBSEImgClr img, COTSImageProcParam a_pImgProcessParam,ref List<COTSParticleClr> parts)
- {
- OTSIMGPROC.ImageProForClr imgpro = new OTSIMGPROC.ImageProForClr();
-
- OTSIMGPROC.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam);
- OTSCOMMONCLR.COTSFieldDataClr flddataclr = new OTSCOMMONCLR.COTSFieldDataClr();
- imgpro.GetFieldDataFromImage(img, prm, flddataclr);
-
- parts = flddataclr.GetParticleList();
- return true;
- }
- private COTSImgProcPrmClr GetImageProcPrmClr(COTSImageProcParam a_oSource)
- {
- COTSImgProcPrmClr prmclr = new COTSImgProcPrmClr();
- OTSCOMMONCLR.CDoubleRangeClr r1 = new OTSCOMMONCLR.CDoubleRangeClr(a_oSource.GetIncAreaRange().GetStart(), a_oSource.GetIncAreaRange().GetEnd());
- prmclr.SetIncArea(r1);
- OTSCOMMONCLR.CIntRangeClr r2= new OTSCOMMONCLR.CIntRangeClr(a_oSource.GetBGGray().GetStart(), a_oSource.GetBGGray().GetEnd());
- prmclr.SetBGGray(r2);
- OTSCOMMONCLR.CIntRangeClr r3= new OTSCOMMONCLR.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<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.GetPosition());
- fldclr.SetImageWidth(f.Width);
- fldclr.SetImageHeight(f.Height);
-
- foreach (var p in f.ListAnalysisParticles)
- {
- fldclr.AddParticle(p);
- }
- fldclrs.Add(fldclr);
- }
- OTSIMGPROC.ImageProForClr imgpro = new OTSIMGPROC.ImageProForClr();
- imgpro.MergeBigBoundaryParticles(fldclrs, pixelSize, scanFieldSize, ResolutionSize, mergedParts);
- return true;
- }
-
- }
- }
|