CImageHandler.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 OTSCLRINTERFACE;
  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(COTSImageProcParam ImgProcPrm, double pixelSize, byte[] BSEImage, int iHeight, int iWidth, ref byte[] BSEImageNoBG)
  24. {
  25. Rectangle rect = new Rectangle();
  26. rect.Height = iHeight;
  27. rect.Width = iWidth;
  28. CBSEImgClr pBSEImageIn = new CBSEImgClr(rect);
  29. CBSEImgClr pBSEImageOut = new CBSEImgClr(rect);
  30. //pBSEImageIn.SetImageRect(rect);
  31. pBSEImageIn.SetImageData(BSEImage,iWidth, iHeight );
  32. //pBSEImageOut.SetImageRect(rect);
  33. if (null == ImgProcPrm)
  34. {
  35. return false;
  36. }
  37. RemoveBackGround(pBSEImageIn, ImgProcPrm, pixelSize ,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. Rectangle rect = new Rectangle();
  54. rect.Height = iHeight;
  55. rect.Width = iWidth;
  56. CBSEImgClr pBSEImageIn = new CBSEImgClr(rect);
  57. CBSEImgClr pBSEImageOut = new CBSEImgClr(rect);
  58. //pBSEImageIn.SetImageRect(rect);
  59. pBSEImageIn.SetImageData(BSEImage, iWidth, iHeight );
  60. CIntRangeClr cIntRangeClr = new CIntRangeClr();
  61. cIntRangeClr.SetStart(grayStart);
  62. cIntRangeClr.SetEnd(grayEnd);
  63. OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr();
  64. int num=0;
  65. imgpro.GetSpecialGrayRangeImage(pBSEImageIn, cIntRangeClr, pBSEImageOut,ref num);
  66. BSEImageNoBG = pBSEImageOut.GetImageDataPtr();
  67. return true;
  68. }
  69. // remove background
  70. public void RemoveBackGround(CBSEImgClr a_pImgIn, COTSImageProcParam a_pImgProcessParam, double a_pixelSize, ref CBSEImgClr a_pImgOut)
  71. {
  72. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  73. RemoveBGAndGetParts(a_pImgIn, a_pImgProcessParam, a_pixelSize,ref parts);
  74. for (int i = 0; i < a_pImgOut.GetWidth(); i++)
  75. {
  76. for (int j = 0; j < a_pImgOut.GetHeight(); j++)
  77. {
  78. a_pImgOut.SetBSEValue(i, j, 255);
  79. }
  80. }
  81. foreach (var p in parts)
  82. {
  83. foreach (var s in p.GetFeature().GetSegmentsList())
  84. {
  85. for (int i = s.GetStart(); i < s.GetStart() + s.GetLength(); i++)
  86. {
  87. var bseValue = a_pImgIn.GetBSEValue(i, s.GetHeight());
  88. a_pImgOut.SetBSEValue(i, s.GetHeight(), bseValue);
  89. }
  90. }
  91. }
  92. return;
  93. }
  94. public void BDilate3(string source, string target, int wDegree, int rows, int columns)
  95. {
  96. throw new NotImplementedException();
  97. }
  98. public void BErode3(string source, string target, int wDegree, int rows, int columns)
  99. {
  100. throw new NotImplementedException();
  101. }
  102. public bool CalParticleImageProp( COTSParticleClr part, double a_pixelSize)
  103. {
  104. OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr();
  105. imgpro.CalcuParticleImagePropertes(part,a_pixelSize);
  106. return true;
  107. }
  108. public bool RemoveBGAndGetParts(CBSEImgClr img, COTSImageProcParam a_pImgProcessParam,double a_pixelSize,ref List<COTSParticleClr> parts)
  109. {
  110. OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr();
  111. OTSCLRINTERFACE.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam);
  112. OTSCLRINTERFACE.COTSFieldDataClr flddataclr = new OTSCLRINTERFACE.COTSFieldDataClr();
  113. imgpro.GetFieldDataFromImage(img, prm, a_pixelSize, flddataclr);
  114. parts = flddataclr.GetParticleList();
  115. return true;
  116. }
  117. public bool GetParticlesBySpecialGray(CBSEImgClr img, CIntRangeClr grayrange, CIntRangeClr diameterRange,double a_pixelSize, ref List<COTSParticleClr> parts)
  118. {
  119. OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr();
  120. //OTSCLRINTERFACE.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam);
  121. OTSCLRINTERFACE.COTSFieldDataClr flddataclr = new OTSCLRINTERFACE.COTSFieldDataClr();
  122. imgpro.GetParticlesBySpecialPartGrayRange(img, grayrange,diameterRange,a_pixelSize, flddataclr);
  123. parts = flddataclr.GetParticleList();
  124. return true;
  125. }
  126. private COTSImgProcPrmClr GetImageProcPrmClr(COTSImageProcParam a_oSource)
  127. {
  128. COTSImgProcPrmClr prmclr = new COTSImgProcPrmClr();
  129. OTSCLRINTERFACE.CDoubleRangeClr r1 = new OTSCLRINTERFACE.CDoubleRangeClr(a_oSource.GetIncAreaRange().GetStart(), a_oSource.GetIncAreaRange().GetEnd());
  130. prmclr.SetIncArea(r1);
  131. OTSCLRINTERFACE.CIntRangeClr r2= new OTSCLRINTERFACE.CIntRangeClr(a_oSource.GetBGGray().GetStart(), a_oSource.GetBGGray().GetEnd());
  132. prmclr.SetBGGray(r2);
  133. OTSCLRINTERFACE.CIntRangeClr r3= new OTSCLRINTERFACE.CIntRangeClr(a_oSource.GetParticleGray().GetStart(), a_oSource.GetParticleGray().GetEnd());
  134. prmclr.SetParticleGray(r3);
  135. prmclr.SetBGRemoveType((int)a_oSource.GetBGRemoveType());
  136. prmclr.SetAutoBGRemoveType((int)a_oSource.GetAutoBGRemoveType());
  137. return prmclr;
  138. }
  139. public bool MergeBigBoundaryParticles(List<COTSFieldData> allFields, double pixelSize, int scanFieldSize, Size ResolutionSize, ref List<COTSParticleClr> mergedParts)
  140. {
  141. List<COTSFieldDataClr> fldclrs = new List<COTSFieldDataClr>();
  142. foreach (var f in allFields)
  143. {
  144. COTSFieldDataClr fldclr = new COTSFieldDataClr();
  145. fldclr.SetPosition(f.GetOTSPosition());
  146. fldclr.SetImageWidth(f.Width);
  147. fldclr.SetImageHeight(f.Height);
  148. var parts = f.GetListAnalysisParticles();
  149. foreach (var p in parts)
  150. {
  151. fldclr.AddParticle(p);
  152. }
  153. fldclrs.Add(fldclr);
  154. }
  155. OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr();
  156. imgpro.MergeBigBoundaryParticles(fldclrs, pixelSize, scanFieldSize, ResolutionSize, mergedParts);
  157. return true;
  158. }
  159. }
  160. }