CImageHandler.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using OTSDataType;
  2. using OTSModelSharp.ImageProcess;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using static OTSDataType.otsdataconst;
  10. namespace OTSModelSharp.ServiceInterface
  11. {
  12. using OTSCOMMONCLR;
  13. using OTSIMGPROC;
  14. using System.Windows;
  15. public class CImageHandler:IImageProcess
  16. {
  17. //获取测量的BSE图
  18. //COTSSample WSample: 工作样品测量
  19. //Byte[] BSEImage: 带背景图数据
  20. //int iHeight: 图像高度
  21. //int iWidth: 图像宽度
  22. //Byte[]BSEImageNoBG : 去背景图数据
  23. public bool GetBSEImage(COTSSample WSample, byte[] BSEImage, int iHeight, int iWidth, ref byte[] BSEImageNoBG)
  24. {
  25. CBSEImgClr pBSEImageIn = new CBSEImgClr();
  26. CBSEImgClr pBSEImageOut = new CBSEImgClr();
  27. Rectangle rect = new Rectangle();
  28. rect.Height = iHeight;
  29. rect.Width = iWidth;
  30. pBSEImageIn.SetImageRect(rect);
  31. pBSEImageIn.SetImageData(BSEImage,iWidth, iHeight );
  32. if (null == WSample)
  33. {
  34. return false;
  35. }
  36. COTSImageProcParam ImgProcPrm = WSample.GetMsrParams().GetImageProcessParam();
  37. RemoveBackGround(pBSEImageIn, ImgProcPrm, ref pBSEImageOut);
  38. BSEImageNoBG = pBSEImageOut.GetImageDataPtr();
  39. return true;
  40. }
  41. /// <summary>
  42. /// 获取测量的BSE图
  43. /// </summary>
  44. /// <param name="BSEImage">BSE原数据</param>
  45. /// <param name="iHeight">图像高度</param>
  46. /// <param name="iWidth">图像宽度</param>
  47. /// <param name="grayStart"></param>
  48. /// <param name="grayEnd"></param>
  49. /// <param name="BSEImageNoBG">去背景图数据</param>
  50. /// <returns></returns>
  51. public bool GetBSEImage(byte[] BSEImage, int iHeight, int iWidth, int grayStart, int grayEnd, ref byte[] BSEImageNoBG)
  52. {
  53. CBSEImgClr pBSEImageIn = new CBSEImgClr();
  54. CBSEImgClr pBSEImageOut = new CBSEImgClr();
  55. Rectangle rect = new Rectangle();
  56. rect.Height = iHeight;
  57. rect.Width = iWidth;
  58. pBSEImageIn.SetImageRect(rect);
  59. pBSEImageIn.SetImageData(BSEImage, iWidth, iHeight );
  60. COTSImageProcParam ImgProcPrm = new COTSImageProcParam();
  61. CIntRange cIntRangeClr = new CIntRange();
  62. cIntRangeClr.SetStart(grayStart);
  63. cIntRangeClr.SetEnd(grayEnd);
  64. ImgProcPrm.SetBGGray(cIntRangeClr);
  65. RemoveBackGround(pBSEImageIn, ImgProcPrm, ref pBSEImageOut);
  66. BSEImageNoBG = pBSEImageOut.GetImageDataPtr();
  67. return true;
  68. }
  69. // remove background
  70. public void RemoveBackGround(CBSEImgClr a_pImgIn, COTSImageProcParam a_pImgProcessParam, ref CBSEImgClr a_pImgOut)
  71. {
  72. OTSIMGPROC.ImageProForClr imgpro = new OTSIMGPROC.ImageProForClr();
  73. OTSIMGPROC.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam);
  74. int pixelnum=0;
  75. CBSEImgClr imgout = new CBSEImgClr();
  76. imgpro.RemoveBackGround(a_pImgIn, prm, imgout, ref pixelnum);// the background pixel will be 0,and the other part will be 255.
  77. // Binary
  78. int nWidth = a_pImgIn.GetWidth();
  79. int nHeight = a_pImgIn.GetHeight();
  80. int nImgSize = nWidth * nHeight;
  81. byte[] pSrcImg = a_pImgIn.GetImageDataPtr();
  82. byte[] pPixel = imgout.GetImageDataPtr();
  83. byte[] pTargetImg = new byte[nImgSize];
  84. // delete background
  85. for (uint i = 0; i < nImgSize; i++)
  86. {
  87. if (pPixel[i]==0)
  88. {
  89. pTargetImg[i] = 255;
  90. }
  91. else
  92. {
  93. byte pixelGray = pSrcImg[i];
  94. pTargetImg[i] = pixelGray;
  95. }
  96. }
  97. a_pImgOut.SetImageData(pTargetImg, nWidth, nHeight);
  98. return;
  99. }
  100. public void BDilate3(string source, string target, int wDegree, int rows, int columns)
  101. {
  102. throw new NotImplementedException();
  103. }
  104. public void BErode3(string source, string target, int wDegree, int rows, int columns)
  105. {
  106. throw new NotImplementedException();
  107. }
  108. public bool CalParticleImageProp( COTSParticleClr part, double a_pixelSize)
  109. {
  110. OTSIMGPROC.ImageProForClr imgpro = new OTSIMGPROC.ImageProForClr();
  111. imgpro.CalcuParticleImagePropertes(part,a_pixelSize);
  112. return true;
  113. }
  114. public bool RemoveBSEImageBG(CBSEImgClr img, COTSImageProcParam a_pImgProcessParam,ref List<COTSParticleClr> parts)
  115. {
  116. OTSIMGPROC.ImageProForClr imgpro = new OTSIMGPROC.ImageProForClr();
  117. OTSIMGPROC.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam);
  118. OTSCOMMONCLR.COTSFieldDataClr flddataclr = new OTSCOMMONCLR.COTSFieldDataClr();
  119. imgpro.GetFieldDataFromImage(img, prm, flddataclr);
  120. parts = flddataclr.GetParticleList();
  121. return true;
  122. }
  123. private COTSImgProcPrmClr GetImageProcPrmClr(COTSImageProcParam a_oSource)
  124. {
  125. COTSImgProcPrmClr prmclr = new COTSImgProcPrmClr();
  126. OTSCOMMONCLR.CDoubleRangeClr r1 = new OTSCOMMONCLR.CDoubleRangeClr(a_oSource.GetIncAreaRange().GetStart(), a_oSource.GetIncAreaRange().GetEnd());
  127. prmclr.SetIncArea(r1);
  128. OTSCOMMONCLR.CIntRangeClr r2= new OTSCOMMONCLR.CIntRangeClr(a_oSource.GetBGGray().GetStart(), a_oSource.GetBGGray().GetEnd());
  129. prmclr.SetBGGray(r2);
  130. OTSCOMMONCLR.CIntRangeClr r3= new OTSCOMMONCLR.CIntRangeClr(a_oSource.GetParticleGray().GetStart(), a_oSource.GetParticleGray().GetEnd());
  131. prmclr.SetParticleGray(r3);
  132. prmclr.SetBGRemoveType((int)a_oSource.GetBGRemoveType());
  133. prmclr.SetAutoBGRemoveType((int)a_oSource.GetAutoBGRemoveType());
  134. return prmclr;
  135. }
  136. public bool MergeBigBoundaryParticles(List<COTSFieldData> allFields, double pixelSize, int scanFieldSize, Size ResolutionSize, ref List<COTSParticleClr> mergedParts)
  137. {
  138. List<COTSFieldDataClr> fldclrs = new List<COTSFieldDataClr>();
  139. foreach (var f in allFields)
  140. {
  141. COTSFieldDataClr fldclr = new COTSFieldDataClr();
  142. fldclr.SetPosition(f.GetPosition());
  143. fldclr.SetImageWidth(f.Width);
  144. fldclr.SetImageHeight(f.Height);
  145. foreach (var p in f.ListAnalysisParticles)
  146. {
  147. fldclr.AddParticle(p);
  148. }
  149. fldclrs.Add(fldclr);
  150. }
  151. OTSIMGPROC.ImageProForClr imgpro = new OTSIMGPROC.ImageProForClr();
  152. imgpro.MergeBigBoundaryParticles(fldclrs, pixelSize, scanFieldSize, ResolutionSize, mergedParts);
  153. return true;
  154. }
  155. }
  156. }