CameraRealTimeStitchingDialog.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using OpenCvSharp;
  13. using OpenCvSharp.Extensions;
  14. using PaintDotNet.Adjust;
  15. using PaintDotNet.Adjust.BaseImage;
  16. using PaintDotNet.Annotation.Measure;
  17. using PaintDotNet.Base.SettingModel;
  18. using PaintDotNet.Camera;
  19. namespace PaintDotNet.ImageCollect
  20. {
  21. internal class CameraRealTimeStitchingDialog : FloatingToolForm
  22. {
  23. #region
  24. /// <summary>
  25. /// Required designer variable.
  26. /// </summary>
  27. private System.ComponentModel.IContainer components = null;
  28. /// <summary>
  29. /// Clean up any resources being used.
  30. /// </summary>
  31. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  32. protected override void Dispose(bool disposing)
  33. {
  34. if (disposing && (components != null))
  35. {
  36. components.Dispose();
  37. }
  38. base.Dispose(disposing);
  39. }
  40. #region Windows Form Designer generated code
  41. private void InitializeLanguageText()
  42. {
  43. this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  44. this.btnCreatImgFile.Text = PdnResources.GetString("Menu.Createpicture.text");
  45. this.btnWorking.Text = PdnResources.GetString("Menu.Started.text");
  46. this.groupBox3.Text = PdnResources.GetString("Menu.Preview.text");
  47. this.groupBox4.Text = PdnResources.GetString("Menu.Puzzleeffect.text");
  48. this.Text = PdnResources.GetString("Menu.imagecapture.Reatimldexpansion.Reaestitching.text");
  49. }
  50. /// <summary>
  51. /// Required method for Designer support - do not modify
  52. /// the contents of this method with the code editor.
  53. /// </summary>
  54. private void InitializeComponent()
  55. {
  56. this.groupBox1 = new System.Windows.Forms.GroupBox();
  57. this.btnCreatImgFile = new System.Windows.Forms.Button();
  58. this.btnWorking = new System.Windows.Forms.Button();
  59. this.groupBox3 = new System.Windows.Forms.GroupBox();
  60. this.picPreview = new System.Windows.Forms.PictureBox();
  61. this.groupBox4 = new System.Windows.Forms.GroupBox();
  62. this.picMatch = new DocumentStitchWindow();// System.Windows.Forms.PictureBox();
  63. this.groupBox1.SuspendLayout();
  64. this.groupBox3.SuspendLayout();
  65. ((System.ComponentModel.ISupportInitialize)(this.picPreview)).BeginInit();
  66. this.groupBox4.SuspendLayout();
  67. //((System.ComponentModel.ISupportInitialize)(this.picMatch)).BeginInit();
  68. this.SuspendLayout();
  69. //
  70. // groupBox1
  71. //
  72. this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  73. | System.Windows.Forms.AnchorStyles.Right)));
  74. this.groupBox1.Controls.Add(this.btnCreatImgFile);
  75. this.groupBox1.Controls.Add(this.btnWorking);
  76. this.groupBox1.Location = new System.Drawing.Point(12, 12);
  77. this.groupBox1.Name = "groupBox1";
  78. this.groupBox1.Size = new System.Drawing.Size(1217, 87);
  79. this.groupBox1.TabIndex = 1;
  80. this.groupBox1.TabStop = false;
  81. this.groupBox1.Text = "操作";
  82. //
  83. // btnCreatImgFile
  84. //
  85. this.btnCreatImgFile.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  86. this.btnCreatImgFile.Location = new System.Drawing.Point(1106, 28);
  87. this.btnCreatImgFile.Name = "btnCreatImgFile";
  88. this.btnCreatImgFile.Size = new System.Drawing.Size(92, 34);
  89. this.btnCreatImgFile.TabIndex = 1;
  90. this.btnCreatImgFile.Text = "创建图片";
  91. this.btnCreatImgFile.UseVisualStyleBackColor = true;
  92. this.btnCreatImgFile.Click += new System.EventHandler(this.btnCreatImgFile_Click);
  93. //
  94. // btnWorking
  95. //
  96. this.btnWorking.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  97. this.btnWorking.Location = new System.Drawing.Point(993, 28);
  98. this.btnWorking.Name = "btnWorking";
  99. this.btnWorking.Size = new System.Drawing.Size(89, 34);
  100. this.btnWorking.TabIndex = 0;
  101. this.btnWorking.Text = "开始";
  102. this.btnWorking.UseVisualStyleBackColor = true;
  103. this.btnWorking.Click += new System.EventHandler(this.btnWorking_Click);
  104. //
  105. // groupBox3
  106. //
  107. this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  108. | System.Windows.Forms.AnchorStyles.Left)));
  109. this.groupBox3.Controls.Add(this.picPreview);
  110. this.groupBox3.Location = new System.Drawing.Point(12, 105);
  111. this.groupBox3.Name = "groupBox3";
  112. this.groupBox3.Size = new System.Drawing.Size(466, 517);
  113. this.groupBox3.TabIndex = 3;
  114. this.groupBox3.TabStop = false;
  115. this.groupBox3.Text = "预览";
  116. //
  117. // picPreview
  118. //
  119. this.picPreview.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  120. | System.Windows.Forms.AnchorStyles.Left)
  121. | System.Windows.Forms.AnchorStyles.Right)));
  122. this.picPreview.Location = new System.Drawing.Point(20, 74);
  123. this.picPreview.Name = "picPreview";
  124. this.picPreview.Size = new System.Drawing.Size(429, 364);
  125. this.picPreview.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  126. this.picPreview.TabIndex = 0;
  127. this.picPreview.TabStop = false;
  128. //
  129. // groupBox4
  130. //
  131. this.groupBox4.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  132. | System.Windows.Forms.AnchorStyles.Left)
  133. | System.Windows.Forms.AnchorStyles.Right)));
  134. this.groupBox4.Controls.Add(this.picMatch);
  135. this.groupBox4.Location = new System.Drawing.Point(484, 105);
  136. this.groupBox4.Name = "groupBox4";
  137. this.groupBox4.Size = new System.Drawing.Size(745, 517);
  138. this.groupBox4.TabIndex = 4;
  139. this.groupBox4.TabStop = false;
  140. this.groupBox4.Text = "拼图效果";
  141. //
  142. // picMatch
  143. //
  144. this.picMatch.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  145. | System.Windows.Forms.AnchorStyles.Left)
  146. | System.Windows.Forms.AnchorStyles.Right)));
  147. this.picMatch.Location = new System.Drawing.Point(17, 74);
  148. this.picMatch.Name = "picMatch";
  149. this.picMatch.Size = new System.Drawing.Size(709, 416);
  150. //this.picMatch.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  151. this.picMatch.TabIndex = 1;
  152. this.picMatch.TabStop = false;
  153. //
  154. // CameraRealTimeStitchingDialog
  155. //
  156. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  157. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  158. this.ClientSize = new System.Drawing.Size(1241, 634);
  159. this.Controls.Add(this.groupBox4);
  160. this.Controls.Add(this.groupBox3);
  161. this.Controls.Add(this.groupBox1);
  162. this.Name = "CameraRealTimeStitchingDialog";
  163. this.Text = "图像实时拼接";
  164. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraRealTimeStitchingDialog_FormClosing);
  165. this.Load += new System.EventHandler(this.CameraRealTimeStitchingDialog_Load);
  166. this.Controls.SetChildIndex(this.groupBox1, 0);
  167. this.Controls.SetChildIndex(this.groupBox3, 0);
  168. this.Controls.SetChildIndex(this.groupBox4, 0);
  169. this.groupBox1.ResumeLayout(false);
  170. this.groupBox3.ResumeLayout(false);
  171. ((System.ComponentModel.ISupportInitialize)(this.picPreview)).EndInit();
  172. this.groupBox4.ResumeLayout(false);
  173. //((System.ComponentModel.ISupportInitialize)(this.picMatch)).EndInit();
  174. this.ResumeLayout(false);
  175. }
  176. #endregion
  177. private System.Windows.Forms.GroupBox groupBox1;
  178. private System.Windows.Forms.Button btnCreatImgFile;
  179. private System.Windows.Forms.Button btnWorking;
  180. private System.Windows.Forms.GroupBox groupBox3;
  181. private System.Windows.Forms.PictureBox picPreview;
  182. private System.Windows.Forms.GroupBox groupBox4;
  183. private /*System.Windows.Forms.PictureBox*/DocumentStitchWindow picMatch;
  184. #endregion
  185. private AppWorkspace m_appWorkspace;
  186. private ICamera m_camera;
  187. private CameraParamModel m_cameraParamModel;
  188. private Mat m_mat;
  189. private Thread m_stitchingThread;
  190. private bool is_depth;
  191. private Mat[] Coll_Ori = new Mat[2];
  192. private int newOriX = 10000;
  193. private int newOriY = 10000;
  194. private Mat m_merge;
  195. private string m_filePath = Application.StartupPath + "\\Picture\\";
  196. private bool m_isWorking;
  197. private bool _isWorkingDone;
  198. public CameraRealTimeStitchingDialog(AppWorkspace appWorkspace)
  199. {
  200. m_appWorkspace = appWorkspace;
  201. InitializeComponent();
  202. InitializeLanguageText();
  203. }
  204. private void CameraRealTimeStitchingDialog_Load(object sender, EventArgs e)
  205. {
  206. m_camera = CameraManager.CurrentCamera;
  207. m_cameraParamModel = Startup.instance.cameraParamModel;
  208. CameraConfigs cameraConfig = CameraConfigs.GetInstance();
  209. cameraConfig.CameraParamInit();
  210. CameraManager.FrameCallback += CallbackDraw;
  211. }
  212. private byte[] m_pbuf;
  213. /// <summary>
  214. /// 绘制
  215. /// </summary>
  216. /// <param name="pBuf"></param>
  217. /// <param name="obj"></param>
  218. public void CallbackDraw(Bitmap bitmap)
  219. {
  220. if (bitmap == null) return;
  221. m_mat = Tools.ToMat(bitmap);
  222. picPreview.Image = (Image)bitmap.Clone();
  223. }
  224. private void btnWorking_Click(object sender, EventArgs e)
  225. {
  226. if (m_isWorking)
  227. {
  228. m_isWorking = false;
  229. m_stitchingThread.Abort();
  230. m_stitchingThread.DisableComObjectEagerCleanup();
  231. btnWorking.Text = PdnResources.GetString("Menu.Started.text");
  232. new Task(() =>
  233. {
  234. Thread.Sleep(500);
  235. this.Invoke(new Action(() =>
  236. {
  237. btnWorking.Text = PdnResources.GetString("Menu.Started.text");
  238. }));
  239. }).Start();
  240. return;
  241. }
  242. if (m_merge != null)
  243. {
  244. newOriX = 10000;
  245. newOriY = 10000;
  246. picMatch.StitchBounds = new Rectangle(newOriX, newOriY, 0, 0);
  247. picMatch.StitchDocument = null;
  248. picMatch.Refresh();
  249. m_merge.Dispose(); m_merge = null;
  250. this.picMatch.Visible = false;
  251. this.groupBox4.Controls.Remove(this.picMatch);
  252. this.picMatch = new DocumentStitchWindow();
  253. this.groupBox4.Controls.Add(this.picMatch);
  254. // picMatch
  255. this.picMatch.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  256. | System.Windows.Forms.AnchorStyles.Left)
  257. | System.Windows.Forms.AnchorStyles.Right)));
  258. this.picMatch.Location = new System.Drawing.Point(17, 74);
  259. this.picMatch.Name = "picMatch";
  260. this.picMatch.Size = new System.Drawing.Size(709, 416);
  261. this.picMatch.TabIndex = 1;
  262. this.picMatch.TabStop = false;
  263. }
  264. m_isWorking = true;
  265. btnWorking.Text = PdnResources.GetString("Menu.stop.text");
  266. lastRect = new Rect(-10000, -10000, 0, 0);
  267. m_merge = null;
  268. m_stitchingThread = new Thread(new ThreadStart(WaitForStitchingThreadEntry));
  269. m_stitchingThread.Start();
  270. }
  271. private Rect lastRect = new Rect(-10000, -10000, 0, 0);
  272. /// <summary>
  273. /// 图像拼接
  274. /// </summary>
  275. private void WaitForStitchingThreadEntry()
  276. {
  277. while (m_isWorking)
  278. {
  279. if (m_mat == null)
  280. {
  281. Thread.Sleep(100);
  282. continue;
  283. }
  284. Mat mat = m_mat;//.Clone();
  285. if (m_merge == null)
  286. {
  287. m_merge = new Mat(20000 + newOriY, 20000 + newOriX, MatType.CV_8UC3);
  288. picMatch.StitchDocument = StitchDocument.FromMat(m_merge/*tmp*/);
  289. picMatch.StitchBounds = new Rectangle(newOriX, newOriY, 0, 0);
  290. picMatch.StitchDocument.surface.stitchBackColor = picMatch.BackColor;
  291. picMatch.ActiveTool = Annotation.Enum.DrawToolType.MoveMode;
  292. AdjustIntent.InitMatchPicOneByOne(true, newOriX, newOriY, m_merge);
  293. }
  294. Rect rect;
  295. var result = AdjustIntent.MatchPicRealtime(mat, m_merge, out bool error, out rect);
  296. if (!error)
  297. {
  298. if (result != null)
  299. m_merge = result;
  300. }
  301. else
  302. {
  303. Console.WriteLine("拼图失败");
  304. }
  305. picMatch.GraphicsList.Clear();
  306. MeasureRectangle rectObject = new MeasureRectangle(picMatch, rect.X, rect.Y, rect.Width, rect.Height);
  307. picMatch.GraphicsList.Add(rectObject);
  308. //var tmp = m_merge.Clone();
  309. //Cv2.Rectangle(tmp, rect, Scalar.Lime, 5, LineTypes.Link4, 4);
  310. ////picMatch.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(tmp);
  311. ////picMatch.Invalidate();
  312. int disX = Math.Abs(rect.X - lastRect.X);
  313. int disY = Math.Abs(rect.Y - lastRect.Y);
  314. lastRect = rect;
  315. if (picMatch.StitchBounds.Width == 0)
  316. {
  317. picMatch.StitchDocument.surface.stitchBounds.Width = rect.Width;// matNew.Width;
  318. picMatch.StitchDocument.surface.stitchBounds.Height = rect.Height;// matNew.Height;
  319. }
  320. else
  321. {
  322. int x = Math.Min(picMatch.StitchBounds.X, rect.X);
  323. int y = Math.Min(picMatch.StitchBounds.Y, rect.Y);
  324. int x2 = Math.Max(picMatch.StitchBounds.Right, rect.Right);
  325. int y2 = Math.Max(picMatch.StitchBounds.Bottom, rect.Bottom);
  326. picMatch.StitchBounds = new Rectangle(x, y, x2 - x, y2 - y);
  327. //picMatch.StitchDocument.surface.stitchBounds.Width += stitchStep;
  328. //picMatch.StitchDocument.surface.stitchBounds.Height += stitchStep;
  329. }
  330. if (!m_isWorking)
  331. return;
  332. this.Invoke(new Action(() =>
  333. {
  334. //PointF centerPt = picMatch.DocumentCenterPointF;
  335. ////PointF centerPt = new PointF(picMatch.picRectWidth/*(newOriX + matNew.Width)*/ * 0.5f, picMatch.picRectHeight/*(newOriY + matNew.Height)*/ * 0.5f);// picMatch.DocumentCenterPointF;
  336. if (disX > 500 || disY > 500)
  337. {
  338. picMatch.ZoomBasis = ZoomBasis.ScaleFactor;
  339. picMatch.ZoomBasis = ZoomBasis.FitToWindow;
  340. picMatch.ZoomBasis = ZoomBasis.ScaleFactor;
  341. picMatch.panel.HorizontalScroll.Value = (int)(picMatch.panel.HorizontalScroll.Maximum * picMatch.StitchBounds.X / (picMatch.StitchWidth));// (int)(picMatch.ScaleFactor.Ratio * 440);// (int)(picMatch.panel.HorizontalScroll.LargeChange * 0.5);// picMatch.ScaleFactor.Ratio * 225.0 / (picMatch.picRectWidth/*20006*/ - 0/*picMatch.picRectWidth*/));
  342. picMatch.panel.VerticalScroll.Value = (int)(picMatch.panel.VerticalScroll.Maximum * picMatch.StitchBounds.Y / picMatch.StitchHeight);// (int)(picMatch.ScaleFactor.Ratio * 150);// (int)(picMatch.panel.VerticalScroll.LargeChange * 0.5);// * picMatch.ScaleFactor.Ratio * 150.0 / (picMatch.picRectHeight/*20006*/ - 0/*picMatch.picRectHeight*/));
  343. }
  344. picMatch.Refresh();
  345. }));
  346. GC.Collect();
  347. }
  348. }
  349. private void btnCreatImgFile_Click(object sender, EventArgs e)
  350. {
  351. if (picMatch.StitchMat/*m_merge*/ == null)
  352. {
  353. MessageBox.Show(PdnResources.GetString("Menu.mageMosaicimagehanotbeengeneratedyet.Text"));
  354. return;
  355. }
  356. //Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_merge);
  357. DocumentWorkspace dw = m_appWorkspace.AddNewDocumentWorkspace();
  358. Document document = Document.FromMat(picMatch.StitchMat/*m_merge*/.Clone())/*FromImage(bitmap)*/;
  359. dw.Document = document;
  360. dw.xmlSaveModel = Startup.instance.ruleDB;
  361. dw.InitRulerInfo();
  362. dw.fileText = "1";
  363. dw.fileText = string.Format("{0}-{1:D3}", "实时拼图", Startup.instance.step_length++);
  364. m_appWorkspace.ActiveDocumentWorkspace = dw;
  365. }
  366. private void CameraRealTimeStitchingDialog_FormClosing(object sender, FormClosingEventArgs e)
  367. {
  368. if (m_stitchingThread != null)
  369. {
  370. m_isWorking = false;
  371. //m_stitchingThread.Join();
  372. //m_stitchingThread.Abort();
  373. //m_stitchingThread.DisableComObjectEagerCleanup();
  374. }
  375. CameraManager.FrameCallback -= CallbackDraw;
  376. if (m_mat != null && !m_mat.IsDisposed)
  377. {
  378. m_mat.Dispose();
  379. }
  380. if (m_merge != null && !m_merge.IsDisposed)
  381. {
  382. m_merge.Dispose();
  383. }
  384. GC.Collect();
  385. }
  386. }
  387. }