frmSpecialGrayParticle.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Windows.Forms;
  8. using OTS.WinFormsUI.Docking;
  9. using OTSDataType;
  10. using OTSCommon;
  11. using static OTSMeasureApp.OTSMeasureStatusWindow;
  12. using OTSModelSharp.ServiceInterface;
  13. using OTSModelSharp.ServiceCenter;
  14. namespace OTSMeasureApp
  15. {
  16. public partial class frmSpecialGrayParticle : DockContent
  17. {
  18. //国际化
  19. OTSCommon.Language lan;
  20. Hashtable table;
  21. //测量主窗体
  22. public OTSIncAMeasureAppForm m_MeasureAppForm;
  23. public OTSMeasureStatusWindow MeasureStatuWindow;
  24. OTSImageData m_ImageData = null;
  25. NLog.Logger log ;
  26. public frmSpecialGrayParticle(OTSIncAMeasureAppForm m_MeasureForm, OTSMeasureStatusWindow MeasureStatuForm)
  27. {
  28. log = NLog.LogManager.GetCurrentClassLogger();
  29. InitializeComponent();
  30. m_MeasureAppForm = m_MeasureForm;
  31. MeasureStatuWindow = MeasureStatuForm;
  32. //国际化
  33. lan = new OTSCommon.Language(this);
  34. table = lan.GetNameTable(this.Name);
  35. }
  36. private Bitmap bseImg;
  37. /// <summary>
  38. /// 获取当前的BSE原图
  39. /// </summary>
  40. public Bitmap BseImg { get => bseImg; set => bseImg = value; }
  41. private byte[] bBseData;
  42. private byte[] originalBseData;
  43. /// <summary>
  44. /// 获取当前的BSE原图数据
  45. /// </summary>
  46. public byte[] GetBBseData()
  47. {
  48. return bBseData;
  49. }
  50. /// <summary>
  51. /// 获取当前的BSE原图数据
  52. /// </summary>
  53. public void SetBBseData(byte[] value)
  54. {
  55. bBseData = value;
  56. originalBseData = value;
  57. }
  58. private void RestoreOriginalBseData()
  59. {
  60. bBseData = originalBseData;
  61. }
  62. //去背景灰度最小值
  63. private int bseGrayMinValue=-1;
  64. /// <summary>
  65. /// 去背景灰度最小值
  66. /// </summary>
  67. public int BseGrayMinValue { get => bseGrayMinValue; set => bseGrayMinValue = value; }
  68. //去背景灰度最大值
  69. private int bseGrayMaxValue = -1;
  70. /// <summary>
  71. /// 去背景灰度最大值
  72. /// </summary>
  73. public int BseGrayMaxValue { get => bseGrayMaxValue; set => bseGrayMaxValue = value; }
  74. private void frmSpecialGrayParticle_Load(object sender, EventArgs e)
  75. {
  76. if (BseImg != null)
  77. {
  78. pbBSEImage.Image = BseImg;
  79. }
  80. if (BseGrayMinValue >-1)
  81. {
  82. nuDownGrayStart.Value = BseGrayMinValue;
  83. tbGrayStart.Value = BseGrayMinValue;
  84. txtGrayStart.Text = BseGrayMinValue.ToString();
  85. }
  86. if (BseGrayMaxValue >-1)
  87. {
  88. nuDownGrayEnd.Value = BseGrayMaxValue;
  89. tbGrayEnd.Value = BseGrayMaxValue;
  90. txtGrayEnd.Text = BseGrayMaxValue.ToString();
  91. }
  92. if (m_ImageData == null)
  93. {
  94. m_ImageData = new OTSImageData(MeasureStatuWindow, m_MeasureAppForm);
  95. }
  96. }
  97. #region BSE图去背景
  98. /// <summary>
  99. /// BSE图去背景
  100. /// </summary>
  101. /// <param name="bInput">BSE原图</param>
  102. /// <param name="grayStart">开始灰度值</param>
  103. /// <param name="grayEnd">结束灰度值</param>
  104. /// <returns></returns>
  105. protected Bitmap RemoveBseGray(Bitmap bInput,int grayStart, int grayEnd)
  106. {
  107. int imgWidth = bInput.Width;
  108. int imgHeight = bInput.Height;
  109. //转换颜色
  110. List<ColorMap> colorMapTemp = new List<ColorMap>();
  111. for (int i = 0; i <= grayStart; i++)
  112. {
  113. ColorMap colorMap = new ColorMap();
  114. string colorName = "#" + Color.FromArgb(i, i, i).Name.ToString();
  115. colorMap.OldColor = ColorTranslator.FromHtml(colorName);
  116. colorMap.NewColor = Color.White;
  117. colorMapTemp.Add(colorMap);
  118. }
  119. for (int i = grayEnd; i <= 255; i++)
  120. {
  121. ColorMap colorMap = new ColorMap();
  122. string colorName = "#" + Color.FromArgb(i, i, i).Name.ToString();
  123. colorMap.OldColor = ColorTranslator.FromHtml(colorName);
  124. colorMap.NewColor = Color.White;
  125. colorMapTemp.Add(colorMap);
  126. }
  127. if (colorMapTemp.Count > 0)
  128. {
  129. Bitmap cutbitmap = new Bitmap(bInput.Width, bInput.Height);
  130. //创建Graphics对象
  131. Graphics g = Graphics.FromImage(cutbitmap);
  132. //生成的图像大小
  133. int width = bInput.Width;
  134. int height = bInput.Height;
  135. //编辑被着急图像所要显示的位置
  136. Rectangle DrawRect = new Rectangle(0, 0, imgWidth, imgHeight);
  137. //编辑输出画布中着色的位置
  138. Rectangle ShowRect = new Rectangle(0, 0, imgWidth, imgHeight);
  139. ImageAttributes attr = new ImageAttributes();
  140. attr.SetRemapTable(colorMapTemp.ToArray());
  141. //从输入图像中截图至临时图像中
  142. g.DrawImage(bInput, ShowRect, 0, 0, DrawRect.Width, DrawRect.Height, GraphicsUnit.Pixel, attr);
  143. g.Dispose();
  144. MemoryStream ms = new MemoryStream();
  145. cutbitmap.Save(ms, ImageFormat.Png);
  146. cutbitmap.Dispose();
  147. //返回图像对象
  148. return (Bitmap)Image.FromStream(ms);
  149. }
  150. else
  151. {
  152. return null;
  153. }
  154. }
  155. #endregion
  156. protected void ShowRemoveBGImage(int startGray,int endGray)
  157. {
  158. try
  159. {
  160. //获取电镜中图像大小
  161. int m_iWidth=0, m_iHeight=0;
  162. string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolution();
  163. string[] sArray = str.Split('X');
  164. if (sArray[0] != "" && sArray[1] != "")
  165. {
  166. m_iWidth = Convert.ToInt32(sArray[0]);
  167. m_iHeight = Convert.ToInt32(sArray[1]);
  168. }
  169. byte[] cBseData=new byte[m_iHeight*m_iWidth];
  170. var bfResult = m_ImageData.GetSpecialGrayImage(bBseData,m_iWidth, m_iHeight, startGray,endGray, ref cBseData);
  171. bBseData = cBseData;
  172. //取图不成功就返回
  173. if (!bfResult) { return; }
  174. Bitmap bitmap = CImageHandler.ToGrayBitmap(cBseData, m_iWidth, m_iHeight);
  175. ShowBSEImage(bitmap);
  176. }
  177. catch (Exception ex)
  178. {
  179. log.Error("(LZMeasureStatusWindow.ShowRemoveBGImage_Click) " + ex.ToString());
  180. }
  181. }
  182. #region 显示去背景BSE图
  183. public void ShowBSEImage(Bitmap BseImg)
  184. {
  185. if (BseImg != null)
  186. {
  187. pbBSEImage.Image = BseImg;
  188. pbBSEImage.Refresh();
  189. }
  190. }
  191. #endregion
  192. private void nuDwonGrayStart_ValueChanged(object sender, EventArgs e)
  193. {
  194. RemoveBseGrayValueChanged();
  195. }
  196. private void nuDownGrayEnd_ValueChanged(object sender, EventArgs e)
  197. {
  198. RemoveBseGrayValueChanged();
  199. }
  200. protected void RemoveBseGrayValueChanged()
  201. {
  202. int grayStart = (int)nuDownGrayStart.Value;
  203. int grayEnd = (int)nuDownGrayEnd.Value;
  204. if (grayStart <= grayEnd)
  205. {
  206. Bitmap reBseImg = RemoveBseGray(BseImg, grayStart, grayEnd);
  207. if (reBseImg != null)
  208. {
  209. ShowBSEImage(reBseImg);
  210. }
  211. }
  212. else
  213. {
  214. nuDownGrayEnd.Value = nuDownGrayStart.Value;
  215. }
  216. txtGrayStart.Text = nuDownGrayStart.Value.ToString();
  217. txtGrayEnd.Text = nuDownGrayEnd.Value.ToString();
  218. tbGrayStart.Value = (int)nuDownGrayStart.Value;
  219. tbGrayEnd.Value = (int)nuDownGrayEnd.Value;
  220. }
  221. private void tbGrayStart_Scroll(object sender, EventArgs e)
  222. {
  223. int grayStart = (int)tbGrayStart.Value;
  224. int grayEnd = (int)tbGrayEnd.Value;
  225. if (grayStart <= grayEnd)
  226. {
  227. Bitmap reBseImg = RemoveBseGray(BseImg, grayStart, grayEnd);
  228. if (reBseImg != null)
  229. {
  230. ShowBSEImage(reBseImg);
  231. }
  232. }
  233. else
  234. {
  235. tbGrayStart.Value = tbGrayStart.Value;
  236. tbGrayEnd.Value = tbGrayStart.Value;
  237. }
  238. txtGrayStart.Text = tbGrayStart.Value.ToString();
  239. txtGrayEnd.Text = tbGrayEnd.Value.ToString();
  240. nuDownGrayStart.Value = (int)tbGrayStart.Value;
  241. nuDownGrayEnd.Value = (int)tbGrayEnd.Value;
  242. }
  243. private void tbGrayEnd_Scroll(object sender, EventArgs e)
  244. {
  245. int grayStart = (int)tbGrayStart.Value;
  246. int grayEnd = (int)tbGrayEnd.Value;
  247. if (grayStart <= grayEnd)
  248. {
  249. txtGrayStart.Text = grayStart.ToString();
  250. txtGrayEnd.Text = grayEnd.ToString();
  251. Bitmap reBseImg = RemoveBseGray(BseImg, grayStart, grayEnd);
  252. if (reBseImg != null)
  253. {
  254. ShowBSEImage(reBseImg);
  255. }
  256. }
  257. else
  258. {
  259. tbGrayEnd.Value = tbGrayStart.Value;
  260. txtGrayEnd.Text = grayStart.ToString();
  261. }
  262. nuDownGrayStart.Value = tbGrayStart.Value;
  263. nuDownGrayEnd.Value = tbGrayEnd.Value;
  264. }
  265. private void btnYes_Click(object sender, EventArgs e)
  266. {
  267. RestoreOriginalBseData();
  268. BseGrayMinValue = Convert.ToInt32(txtGrayStart.Text);
  269. BseGrayMaxValue = Convert.ToInt32(txtGrayEnd.Text);
  270. COTSImageProcParam cOTSImgProc = new COTSImageProcParam();
  271. //remove the low end gray pixel
  272. //CIntRange cIntRange = new CIntRange();
  273. //cIntRange.SetStart(0);
  274. //cIntRange.SetEnd(BseGrayMinValue);
  275. //cOTSImgProc.SetBGGray(cIntRange);
  276. var reBseImg= ShowRemoveBGImage(bBseData,BseGrayMinValue, bseGrayMaxValue);
  277. ShowBSEImage(reBseImg);
  278. //remove the high end gray pixel
  279. //cIntRange.SetStart(BseGrayMaxValue);
  280. //cIntRange.SetEnd(255);
  281. //cOTSImgProc.SetBGGray(cIntRange);
  282. //ShowRemoveBGImage(cOTSImgProc);
  283. }
  284. private void btnCancel_Click(object sender, EventArgs e)
  285. {
  286. this.DialogResult = DialogResult.Cancel;
  287. }
  288. #region BSE图去背景
  289. protected Bitmap ShowRemoveBGImage(byte[] bBseData,int grayStart,int grayEnd)
  290. {
  291. try
  292. {
  293. int m_iWidth = 0;
  294. int m_iHeight = 0;
  295. //获取电镜中图像大小
  296. string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolution();
  297. string[] sArray = str.Split('X');
  298. if (sArray[0] != "" && sArray[1] != "")
  299. {
  300. m_iWidth = Convert.ToInt32(sArray[0]);
  301. m_iHeight = Convert.ToInt32(sArray[1]);
  302. }
  303. //去背景图
  304. byte[] cBseData = null;
  305. //获取图像数据
  306. var imagefun = new OTSBSEImageFun();
  307. bool bfResult = imagefun.GetBSEImage(bBseData, m_iHeight, m_iWidth, grayStart, grayEnd, ref cBseData);
  308. if (bfResult)
  309. {
  310. Bitmap reImg= CImageHandler.ToGrayBitmap(cBseData, m_iWidth, m_iHeight);
  311. return reImg;
  312. }
  313. else
  314. {
  315. return null;
  316. }
  317. }
  318. catch (Exception ex)
  319. {
  320. log.Error("(ShowRemoveBGImage_Click):" + ex.ToString());
  321. }
  322. return null;
  323. }
  324. #endregion
  325. private void btnOk_Click(object sender, EventArgs e)
  326. {
  327. this.bseGrayMinValue = Convert.ToInt32(txtGrayStart.Text);
  328. this.bseGrayMaxValue = Convert.ToInt32(txtGrayEnd.Text);
  329. this.DialogResult = DialogResult.OK;
  330. }
  331. }
  332. }