ImageStitchingFocusDialog.cs 13 KB

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