ImageStitchingFocusDialog.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using PaintDotNet.Annotation.ImageCollect;
  2. using PaintDotNet.Base.SettingModel;
  3. using StageController;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using PaintDotNet.Adjust;
  14. using TUCamera;
  15. using PaintDotNet.ImageCollect;
  16. namespace PaintDotNet
  17. {
  18. public partial class ImageStitchingFocusDialog : PdnBaseForm
  19. {
  20. AxisController m_stage;
  21. private LoadingStageModel m_loadingStageModel;
  22. private int _lowSpeed;
  23. private int _highSpeed;
  24. protected double m_PxLength;
  25. private CurvedSurface[] ResultList;
  26. public Action<CurvedSurface[]> CallbackResult;
  27. public ImageStitchingFocusDialog(AxisController stage, List<DrawStithchingBase> graphicsList)
  28. {
  29. InitializeComponent();
  30. InitializeButtonBackgroundImage();
  31. InitLanguage();
  32. m_stage = stage;
  33. m_loadingStageModel = Startup.instance.loadingStageModel;
  34. m_graphicsList = graphicsList;
  35. UpdateGrid();
  36. _lowSpeed = m_loadingStageModel.items[0].ZSpeed;
  37. _highSpeed = m_loadingStageModel.items[5].ZSpeed;
  38. Startup.instance.rules.TryGetValue(MeasurementUnit.Micron, out m_PxLength);
  39. ResultList = new CurvedSurface[graphicsList.Count];
  40. _fittingMap = new Dictionary<int, Dictionary<int, double>>();
  41. }
  42. private void InitializeButtonBackgroundImage()
  43. {
  44. btnContinuityUp.FlatStyle = FlatStyle.Flat;
  45. btnContinuityUp.FlatAppearance.BorderSize = 0;
  46. btnContinuityUp.BackgroundImageLayout = ImageLayout.Zoom;
  47. btnContinuityUp.BackgroundImage = PdnResources.GetImageResource("Images.ButtonUp.png").Reference;
  48. btnContinuityLower.FlatStyle = FlatStyle.Flat;
  49. btnContinuityLower.FlatAppearance.BorderSize = 0;
  50. btnContinuityLower.BackgroundImageLayout = ImageLayout.Zoom;
  51. btnContinuityLower.BackgroundImage = PdnResources.GetImageResource("Images.ButtonDown.png").Reference;
  52. btnFixedUp.FlatStyle = FlatStyle.Flat;
  53. btnFixedUp.FlatAppearance.BorderSize = 0;
  54. btnFixedUp.BackgroundImageLayout = ImageLayout.Zoom;
  55. btnFixedUp.BackgroundImage = PdnResources.GetImageResource("Images.ButtonUp.png").Reference;
  56. btnFixedLower.FlatStyle = FlatStyle.Flat;
  57. btnFixedLower.FlatAppearance.BorderSize = 0;
  58. btnFixedLower.BackgroundImageLayout = ImageLayout.Zoom;
  59. btnFixedLower.BackgroundImage = PdnResources.GetImageResource("Images.ButtonDown.png").Reference;
  60. }
  61. private void InitLanguage()
  62. {
  63. this.groupBox2.Text = PdnResources.GetString("Menu.Preview.text");
  64. this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  65. this.groupBox6.Text = PdnResources.GetString("Menu.operation.text");
  66. this.groupBox4.Text = PdnResources.GetString("Menu.continuous.text");
  67. this.groupBox7.Text = PdnResources.GetString("Menu.Fixedlength.text"); this.groupBox3.Text = PdnResources.GetString("Menu.imagecapture.Imagestitching.Listofpuzzleareas.text");
  68. groupBox5.Text = PdnResources.GetString("LocationList");
  69. groupBox8.Text = PdnResources.GetString("Menu.Runningspeed.text");
  70. btnOk.Text = PdnResources.GetString("Form.OkButton.Text");
  71. BtnCancel.Text = PdnResources.GetString("Form.CancelButton.Text");
  72. btnSaveOne.Text = PdnResources.GetString("Form.SaveButton.Text");
  73. btnStartFocus.Text = PdnResources.GetString("Menu.ImageCollection.Autofocus.Text");
  74. rbtHighS.Text = PdnResources.GetString("Button.Text.HighSpeed");
  75. rbtLowS.Text = PdnResources.GetString("Button.Text.LowSpeed");
  76. }
  77. private void CallbackDraw(Bitmap obj)
  78. {
  79. try
  80. {
  81. this.Invoke(new Action(() => { pctPreview.Image = (Image)obj.Clone(); }));
  82. m_BitmapCurrent = (Bitmap)obj.Clone();
  83. }
  84. catch
  85. { }
  86. }
  87. private void UpdateGrid()
  88. {
  89. for (int i = 0; i < m_graphicsList.Count; i++)
  90. {
  91. var draw = m_graphicsList[i];
  92. int[] tiles = draw.GetTiles();
  93. double area = draw.GetArea();
  94. string name = "";
  95. if (draw is DrawStitchingRectangle)
  96. {
  97. name = PdnResources.GetString("Menu.LabelAction.DrawRectangle.Text");
  98. }
  99. else if (draw is DrawStitchingPolygon)
  100. {
  101. name = PdnResources.GetString("Menu.LabelAction.DrawPolygon.Text");
  102. }
  103. else
  104. {
  105. name = PdnResources.GetString("Menu.LabelAction.DrawCircle.Text");
  106. }
  107. dgvRegionTeb.Rows.Add(i, name, draw.ColumnNum + "," + draw.RowNum, 0, area);
  108. }
  109. }
  110. private void ImageStitchingFocusDialog_Load(object sender, EventArgs e)
  111. {
  112. m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
  113. m_camera.m_bufHandler += CallbackDraw;
  114. rbtLowS.Checked = true;
  115. SpeedChangeClick(rbtLowS, null);
  116. }
  117. private void ImageStitchingFocusDialog_FormClosing(object sender, FormClosingEventArgs e)
  118. {
  119. m_camera.m_bufHandler -= CallbackDraw;
  120. }
  121. #region Z operations
  122. private void btnContinuityUp_MouseDown(object sender, MouseEventArgs e)
  123. {
  124. m_stage.GoTop(true);
  125. }
  126. private void btnContinuityStop_MouseUp(object sender, MouseEventArgs e)
  127. {
  128. m_stage.LockZ();
  129. }
  130. private void btnContinuityLower_MouseDown(object sender, MouseEventArgs e)
  131. {
  132. m_stage.GoTop(false);
  133. }
  134. /// <summary>
  135. /// Z 微动距离
  136. /// </summary>
  137. double _stepping = 1.0;
  138. private void btnFixedUp_Click(object sender, EventArgs e)
  139. {
  140. m_stage.Up(_stepping);
  141. }
  142. private void btnFixedLower_Click(object sender, EventArgs e)
  143. {
  144. m_stage.Up(-_stepping);
  145. }
  146. private void SpeedChangeClick(object sender, EventArgs e)
  147. {
  148. if (sender == rbtLowS)
  149. {
  150. m_stage.SetSpeedZ(_lowSpeed);
  151. }
  152. if (sender == rbtHighS)
  153. {
  154. m_stage.SetSpeedZ(_highSpeed);
  155. }
  156. }
  157. #endregion
  158. #region Region Grid
  159. private DrawStithchingBase m_graphicsCurrent;
  160. private List<DrawStithchingBase> m_graphicsList;
  161. private List<Control> _labelList;
  162. private Label _selectLabel;
  163. private Dictionary<int, Dictionary<int, double>> _fittingMap;
  164. private TUCamera.TUCamera m_camera;
  165. private Dictionary<int, double> _fittingList;
  166. private DataGridViewRow _selectRow;
  167. private Bitmap m_BitmapCurrent;
  168. private void dgvRegionTeb_CellClick(object sender, DataGridViewCellEventArgs e)
  169. {
  170. if (e.RowIndex < 0) return;
  171. _selectRow = dgvRegionTeb.Rows[e.RowIndex];
  172. var select = m_graphicsList[e.RowIndex];
  173. _fittingMap.TryGetValue(e.RowIndex, out _fittingList);
  174. if (m_graphicsCurrent != select)
  175. {
  176. _labelList = new List<Control>();
  177. if (_fittingList == null)
  178. _fittingList = new Dictionary<int, double>();
  179. BuildGrid(select);
  180. m_graphicsCurrent = select;
  181. }
  182. }
  183. private void BuildGrid(DrawStithchingBase drawobj)
  184. {
  185. var _rowN = drawobj.RowNum;
  186. var _columnN = drawobj.ColumnNum;
  187. int width = pnlGridRegion.Width / _columnN;
  188. int height = pnlGridRegion.Height / _rowN;
  189. int i = 0;
  190. pnlGridRegion.Controls.Clear();
  191. _labelList.Clear();
  192. for (int r = 0; r < _rowN; r++)
  193. for (int c = 0; c < _columnN; c++)
  194. {
  195. Label label = new Label();
  196. label.Width = width;
  197. label.Height = height;
  198. label.Location = new Point(width * c, r * height);
  199. label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  200. label.BackColor = System.Drawing.Color.White;
  201. label.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  202. if (!(r % 2 == 0))
  203. i = r * _columnN + _columnN - c - 1;
  204. else
  205. i = r * _columnN + c;
  206. label.Tag = drawobj.GetViewPoints()[i];
  207. label.MouseClick += Label_Click;
  208. //label.DoubleClick += Label_DoubleClick;
  209. pnlGridRegion.Controls.Add(label);
  210. _labelList.Add(label);
  211. if (_fittingList.ContainsKey(i))
  212. {
  213. label.BackColor = Color.Gold;
  214. label.Text = _fittingList[i].ToString("f1");
  215. }
  216. }
  217. }
  218. protected double ConvertPXToUm(double length)
  219. {
  220. return length * m_PxLength;
  221. }
  222. private void Label_Click(object sender, MouseEventArgs e)
  223. {
  224. if (e.Button == MouseButtons.Left)
  225. {
  226. if (m_stage.IsMoving) return;
  227. if (_selectLabel != null)
  228. _selectLabel.BorderStyle = BorderStyle.FixedSingle;
  229. _selectLabel = sender as Label;
  230. _selectLabel.BorderStyle = BorderStyle.Fixed3D;
  231. PointF nextP = (PointF)(_selectLabel.Tag as Dictionary<int, object>)[0];
  232. var x = ConvertPXToUm(nextP.X);
  233. var y = ConvertPXToUm(nextP.Y);
  234. m_stage.To(x, y);
  235. }
  236. else if (e.Button == MouseButtons.Right)
  237. {
  238. var label = sender as Label;
  239. int i = _labelList.IndexOf(label);
  240. if (label.BackColor == Color.Gold)
  241. {
  242. _fittingList.Remove(i);
  243. label.BackColor = Color.White;
  244. label.Text = "";
  245. }
  246. else
  247. {
  248. _fittingList.Add(i, m_stage.Z);
  249. label.BackColor = Color.Gold;
  250. label.Text = m_stage.Z.ToString("f1");
  251. }
  252. }
  253. }
  254. #endregion
  255. #region Operate
  256. private void btnStartFocus_Click(object sender, EventArgs e)
  257. {
  258. new Task(new Action(() =>
  259. {
  260. try
  261. {
  262. AutoFocusWorkflow.AutoFocus(m_stage, () => { return m_BitmapCurrent; });
  263. }
  264. catch (Exception ex)
  265. {
  266. MessageBox.Show(ex.Message);
  267. }
  268. })).Start();
  269. }
  270. private void btnSaveOne_Click(object sender, EventArgs e)
  271. {
  272. CurvedSurface curvedSurface;
  273. bool result = Calculate(out curvedSurface);
  274. if (result)
  275. {
  276. _fittingMap.Add(_selectRow.Index, _fittingList);
  277. _selectRow.DefaultCellStyle.BackColor = Color.Lime;
  278. }
  279. else
  280. {
  281. _selectRow.DefaultCellStyle.BackColor = Color.Yellow;
  282. }
  283. ResultList[_selectRow.Index] = curvedSurface;
  284. }
  285. private bool Calculate(out CurvedSurface curvedSurface)
  286. {
  287. curvedSurface = null;
  288. if (_fittingList.Keys.Count < 3)
  289. {
  290. MessageBox.Show("Need at last 3 points.");
  291. return false;
  292. }
  293. var list = new List<CurvedSurfacePoint>();
  294. foreach (var item in _fittingList)
  295. {
  296. PointF nextP = (PointF)(_labelList[item.Key].Tag as Dictionary<int, object>)[0];
  297. var x = ConvertPXToUm(nextP.X);
  298. var y = ConvertPXToUm(nextP.Y);
  299. list.Add(new CurvedSurfacePoint(x, y, item.Value));
  300. }
  301. curvedSurface = new CurvedSurface();
  302. curvedSurface.Init(list);
  303. try
  304. {
  305. curvedSurface.CalPara();
  306. }
  307. catch
  308. { return false; }
  309. return true;
  310. }
  311. private void btnSetZ_Click(object sender, EventArgs e)
  312. {
  313. int i = _labelList.IndexOf(_selectLabel);
  314. if (_selectLabel.BackColor == Color.Gold)
  315. {
  316. _fittingList.Remove(i);
  317. _selectLabel.BackColor = Color.White;
  318. _selectLabel.Text = "";
  319. }
  320. else
  321. {
  322. _fittingList.Add(i, m_stage.Z);
  323. _selectLabel.BackColor = Color.Gold;
  324. _selectLabel.Text = m_stage.Z.ToString("f1");
  325. }
  326. }
  327. private void btnOk_Click(object sender, EventArgs e)
  328. {
  329. CallbackResult(ResultList);
  330. Close();
  331. }
  332. #endregion
  333. }
  334. }