frmSpecialGrayParticle.cs 12 KB

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