frmSpecialGrayParticle.cs 12 KB

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