CameraSettingDialog.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Annotation.relationModel;
  3. using PaintDotNet.Base.SettingModel;
  4. using PaintDotNet.Base.CommTool;
  5. using PaintDotNet.ImageCollect;
  6. using PaintDotNet.Setting.LabelComponent;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Windows.Forms;
  12. using TUCAMAPI;
  13. using TUCamera;
  14. namespace PaintDotNet.Setting
  15. {
  16. /// <summary>
  17. /// 设置->相机设置
  18. /// </summary>
  19. internal class CameraSettingDialog : FloatingToolForm
  20. {
  21. /// <summary>
  22. /// 标注一二级关系
  23. /// </summary>
  24. private static List<CameraRelationModel> list = new List<CameraRelationModel>();
  25. /// <summary>
  26. /// 当前选中的参数
  27. /// </summary>
  28. private CameraParamType cameraParamType;
  29. /// <summary>
  30. /// 相机参数
  31. /// </summary>
  32. //public CameraParamModel m_cameraParamModel = CameraConfigs.GetInstance().GetDefaultCameraParamModel();
  33. public CameraParamModel m_cameraParamModel = Startup.instance.cameraParamModel;
  34. /// <summary>
  35. /// 主配置文件的model
  36. /// </summary>
  37. private ConfigModel configModel = Startup.instance.configModel;
  38. /// <summary>
  39. /// 操作样式下拉数据
  40. /// </summary>
  41. private List<string> files = new List<string>();
  42. /// <summary>
  43. /// 另存为弹出框显示
  44. /// </summary>
  45. private CreateNameDialog dialog;
  46. /// <summary>
  47. /// 另存为文件名
  48. /// </summary>
  49. private string newName;
  50. // private Bitmap bitmap;
  51. // private OpenCvSharp.Mat mat;
  52. // private OpenCvSharp.Mat oldMat;
  53. //各页面组件
  54. /*private LabelTextControl textControl;
  55. private LabelMarkDateControl dateMarkControl;
  56. private LabelMarkTimeControl timeMarkControl;*/
  57. private AdjustCameraControl adjustCameraControl;
  58. private FrameCameraControl frameCameraControl;
  59. private CommonCameraControl commonCameraControl;
  60. //private int width, height;
  61. /// <summary>
  62. /// 相机
  63. /// </summary>
  64. //private MIChrome5Pro m5pro;
  65. /// <summary>
  66. /// 一个矩阵数组,用来接收直方图,记得全部初始化
  67. /// </summary>
  68. OpenCvSharp.Mat[] oldHists = new OpenCvSharp.Mat[] { new OpenCvSharp.Mat(), new OpenCvSharp.Mat(), new OpenCvSharp.Mat() };
  69. /// <summary>
  70. /// BGR线条颜色
  71. /// </summary>
  72. private OpenCvSharp.Scalar[] color = new OpenCvSharp.Scalar[] { new OpenCvSharp.Scalar(255, 0, 0, 255), new OpenCvSharp.Scalar(0, 255, 0, 255), new OpenCvSharp.Scalar(0, 0, 255, 255) };
  73. #region 控件
  74. private System.Windows.Forms.GroupBox groupBox1;
  75. private System.Windows.Forms.GroupBox groupBox3;
  76. private System.Windows.Forms.Panel controlPanel;
  77. private System.Windows.Forms.ListBox listBox1;
  78. private Button button3;
  79. private Button button2;
  80. private Label label1;
  81. private Button btnClose;
  82. private Label label2;
  83. private PictureBox pictureBox1;
  84. private Label lblParamName;
  85. private Label label4;
  86. private Button btnRest;
  87. #endregion
  88. public CameraSettingDialog()
  89. {
  90. if (list == null || list.Count == 0)
  91. {
  92. CameraRelationModel crm1 = new CameraRelationModel(1, PdnResources.GetString("Menu.Image.Adjust.Text"), CameraParamType.Adjust);
  93. list.Add(crm1);
  94. CameraRelationModel crm2 = new CameraRelationModel(2, PdnResources.GetString("Menu.ImageCollection.CameraFrame.Text"), CameraParamType.Frame);
  95. list.Add(crm2);
  96. CameraRelationModel crm3 = new CameraRelationModel(3, PdnResources.GetString("Menu.ImageCollection.CameraConventional.Text"), CameraParamType.Common);
  97. list.Add(crm3);
  98. }
  99. adjustCameraControl = new AdjustCameraControl(m_cameraParamModel, true);
  100. adjustCameraControl.Location = new Point(10, 15);
  101. frameCameraControl = new FrameCameraControl(m_cameraParamModel, true);
  102. frameCameraControl.Location = new Point(10, 15);
  103. commonCameraControl = new CommonCameraControl(m_cameraParamModel, true);
  104. commonCameraControl.Location = new Point(10, 15);
  105. InitializeComponent();
  106. InitializeLanguageText();
  107. InitializeData();
  108. this.FormClosing += (s, e) => { frameCameraControl.Dispose(); adjustCameraControl.Dispose(); };
  109. }
  110. private void InitializeLanguageText()
  111. {
  112. this.label1.Text = PdnResources.GetString("Menu.Systemusecamera.text") + ":";
  113. this.button3.Text = PdnResources.GetString("Menu.File.SaveAs.Text");
  114. this.button2.Text = PdnResources.GetString("Menu.File.Save.Text");
  115. this.groupBox3.Text = PdnResources.GetString("Menu.Type.text");
  116. this.btnClose.Text = PdnResources.GetString("Menu.File.Close.Text");
  117. this.btnRest.Text = PdnResources.GetString("Menu.reinformation.text");
  118. this.Text = PdnResources.GetString("Menu.ImageCollection.CameraSetting.Text");
  119. }
  120. private void InitializeComponent()
  121. {
  122. this.groupBox1 = new System.Windows.Forms.GroupBox();
  123. this.lblParamName = new System.Windows.Forms.Label();
  124. this.label4 = new System.Windows.Forms.Label();
  125. this.label2 = new System.Windows.Forms.Label();
  126. this.label1 = new System.Windows.Forms.Label();
  127. this.button3 = new System.Windows.Forms.Button();
  128. this.button2 = new System.Windows.Forms.Button();
  129. this.groupBox3 = new System.Windows.Forms.GroupBox();
  130. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  131. this.btnClose = new System.Windows.Forms.Button();
  132. this.btnRest = new System.Windows.Forms.Button();
  133. this.listBox1 = new System.Windows.Forms.ListBox();
  134. this.controlPanel = new System.Windows.Forms.Panel();
  135. this.groupBox1.SuspendLayout();
  136. this.groupBox3.SuspendLayout();
  137. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  138. this.SuspendLayout();
  139. //
  140. // groupBox1
  141. //
  142. this.groupBox1.Controls.Add(this.lblParamName);
  143. this.groupBox1.Controls.Add(this.label4);
  144. this.groupBox1.Controls.Add(this.label2);
  145. this.groupBox1.Controls.Add(this.label1);
  146. this.groupBox1.Controls.Add(this.button3);
  147. this.groupBox1.Controls.Add(this.button2);
  148. this.groupBox1.Location = new System.Drawing.Point(134, 13);
  149. this.groupBox1.Name = "groupBox1";
  150. this.groupBox1.Size = new System.Drawing.Size(556, 78);
  151. this.groupBox1.TabIndex = 1;
  152. this.groupBox1.TabStop = false;
  153. //
  154. // lblParamName
  155. //
  156. this.lblParamName.AutoSize = true;
  157. this.lblParamName.Location = new System.Drawing.Point(109, 47);
  158. this.lblParamName.Name = "lblParamName";
  159. this.lblParamName.Size = new System.Drawing.Size(53, 12);
  160. this.lblParamName.TabIndex = 8;
  161. this.lblParamName.Text = " ";
  162. //
  163. // label4
  164. //
  165. this.label4.AutoSize = true;
  166. this.label4.Location = new System.Drawing.Point(14, 47);
  167. this.label4.Name = "label4";
  168. this.label4.Size = new System.Drawing.Size(89, 12);
  169. this.label4.TabIndex = 7;
  170. this.label4.Text = "相机参数名称:";
  171. //
  172. // label2
  173. //
  174. this.label2.AutoSize = true;
  175. this.label2.Location = new System.Drawing.Point(109, 25);
  176. this.label2.Name = "label2";
  177. this.label2.Size = new System.Drawing.Size(53, 12);
  178. this.label2.TabIndex = 6;
  179. this.label2.Text = " ";
  180. //
  181. // label1
  182. //
  183. this.label1.AutoSize = true;
  184. this.label1.Location = new System.Drawing.Point(14, 25);
  185. this.label1.Name = "label1";
  186. this.label1.Size = new System.Drawing.Size(89, 12);
  187. this.label1.TabIndex = 5;
  188. this.label1.Text = "系统使用相机:";
  189. //
  190. // button3
  191. //
  192. this.button3.Location = new System.Drawing.Point(457, 20);
  193. this.button3.Name = "button3";
  194. this.button3.Size = new System.Drawing.Size(75, 23);
  195. this.button3.TabIndex = 2;
  196. this.button3.Text = "另存为";
  197. this.button3.UseVisualStyleBackColor = true;
  198. this.button3.Click += new System.EventHandler(this.button3_Click);
  199. //
  200. // button2
  201. //
  202. this.button2.Location = new System.Drawing.Point(376, 19);
  203. this.button2.Name = "button2";
  204. this.button2.Size = new System.Drawing.Size(75, 23);
  205. this.button2.TabIndex = 1;
  206. this.button2.Text = "保存";
  207. this.button2.UseVisualStyleBackColor = true;
  208. this.button2.Click += new System.EventHandler(this.button2_Click);
  209. //
  210. // groupBox3
  211. //
  212. this.groupBox3.Controls.Add(this.pictureBox1);
  213. this.groupBox3.Controls.Add(this.btnClose);
  214. this.groupBox3.Controls.Add(this.btnRest);
  215. this.groupBox3.Controls.Add(this.listBox1);
  216. this.groupBox3.Location = new System.Drawing.Point(13, 13);
  217. this.groupBox3.Name = "groupBox3";
  218. this.groupBox3.Size = new System.Drawing.Size(115, 471);
  219. this.groupBox3.TabIndex = 3;
  220. this.groupBox3.TabStop = false;
  221. //
  222. // pictureBox1
  223. //
  224. this.pictureBox1.Location = new System.Drawing.Point(6, 301);
  225. this.pictureBox1.Name = "pictureBox1";
  226. this.pictureBox1.Size = new System.Drawing.Size(100, 50);
  227. this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  228. this.pictureBox1.TabIndex = 4;
  229. this.pictureBox1.TabStop = false;
  230. //
  231. // btnClose
  232. //
  233. this.btnClose.Location = new System.Drawing.Point(16, 431);
  234. this.btnClose.Name = "btnClose";
  235. this.btnClose.Size = new System.Drawing.Size(75, 23);
  236. this.btnClose.TabIndex = 3;
  237. this.btnClose.Text = "关闭";
  238. this.btnClose.UseVisualStyleBackColor = true;
  239. this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
  240. //
  241. // btnRest
  242. //
  243. this.btnRest.Location = new System.Drawing.Point(16, 393);
  244. this.btnRest.Name = "btnRest";
  245. this.btnRest.Size = new System.Drawing.Size(75, 23);
  246. this.btnRest.TabIndex = 2;
  247. this.btnRest.Text = "重置";
  248. this.btnRest.UseVisualStyleBackColor = true;
  249. this.btnRest.Click += new System.EventHandler(this.btnRest_Click);
  250. //
  251. // listBox1
  252. //
  253. this.listBox1.FormattingEnabled = true;
  254. this.listBox1.ItemHeight = 12;
  255. this.listBox1.Location = new System.Drawing.Point(6, 20);
  256. this.listBox1.Name = "listBox1";
  257. this.listBox1.Size = new System.Drawing.Size(103, 208);
  258. this.listBox1.TabIndex = 0;
  259. this.listBox1.SelectedValueChanged += new System.EventHandler(this.listBox1_SelectedValueChanged);
  260. //
  261. // controlPanel
  262. //
  263. this.controlPanel.AutoScroll = true;
  264. this.controlPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  265. this.controlPanel.Location = new System.Drawing.Point(134, 98);
  266. this.controlPanel.Name = "controlPanel";
  267. this.controlPanel.Size = new System.Drawing.Size(556, 386);
  268. this.controlPanel.TabIndex = 5;
  269. //
  270. // CameraSettingDialog
  271. //
  272. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  273. this.ClientSize = new System.Drawing.Size(702, 496);
  274. this.Controls.Add(this.controlPanel);
  275. this.Controls.Add(this.groupBox3);
  276. this.Controls.Add(this.groupBox1);
  277. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  278. this.Name = "CameraSettingDialog";
  279. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  280. this.Text = "相机设置";
  281. this.Activated += new System.EventHandler(this.CameraSettingDialog_Activated);
  282. this.Controls.SetChildIndex(this.groupBox1, 0);
  283. this.Controls.SetChildIndex(this.groupBox3, 0);
  284. this.Controls.SetChildIndex(this.controlPanel, 0);
  285. this.groupBox1.ResumeLayout(false);
  286. this.groupBox1.PerformLayout();
  287. this.groupBox3.ResumeLayout(false);
  288. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  289. this.ResumeLayout(false);
  290. }
  291. /// <summary>
  292. /// 初始化数据
  293. /// </summary>
  294. public void InitializeData()
  295. {
  296. //绑定左侧listbox数据
  297. this.listBox1.Items.Clear();
  298. foreach (CameraRelationModel model in list)
  299. {
  300. this.listBox1.Items.Add(model.name);
  301. }
  302. this.listBox1.SelectedIndex = 0;
  303. try
  304. {
  305. // 获取系统使用的相机名称
  306. TUCamera.TUCamera camera = TUCameraManager.GetInstance().GetCurrentCamera();
  307. // 获取系统使用的相机SN
  308. string sn;
  309. camera.ReadRegisterData(out sn);
  310. //label2.Text = camera.GetName();
  311. if (!string.IsNullOrEmpty(sn))
  312. {
  313. label2.Text = sn;
  314. }
  315. m_cameraParamModel = Startup.instance.cameraParamModel;
  316. lblParamName.Text = CameraConfigs.GetInstance().CurrentConfigFileName;
  317. }
  318. catch (Exception ex)
  319. {
  320. MessageBox.Show(ex.Message);
  321. }
  322. }
  323. /// <summary>
  324. /// 当前操作样式 下拉选切换
  325. /// </summary>
  326. /// <param name="sender"></param>
  327. /// <param name="e"></param>
  328. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  329. {
  330. InitializeData();
  331. }
  332. /// <summary>
  333. /// 左侧listbox选择事件
  334. /// </summary>
  335. /// <param name="sender"></param>
  336. /// <param name="e"></param>
  337. private void listBox1_SelectedValueChanged(object sender, EventArgs e)
  338. {
  339. //首先更新右侧分类部分
  340. UpdateRightCatalog();
  341. //更新右侧设置界面
  342. UpdateRightSettingUI();
  343. //刷新右侧预览
  344. UpdateRightPreview();
  345. }
  346. /// <summary>
  347. /// 更新右侧分类
  348. /// </summary>
  349. private void UpdateRightCatalog()
  350. {
  351. this.controlPanel.Controls.Clear();
  352. CameraRelationModel model = list[this.listBox1.SelectedIndex];
  353. this.cameraParamType = model.cameraParamType;
  354. }
  355. /// <summary>
  356. /// 更新右侧设置
  357. /// </summary>
  358. private void UpdateRightSettingUI()
  359. {
  360. this.controlPanel.Controls.Clear();
  361. switch (this.cameraParamType)
  362. {
  363. case CameraParamType.Adjust: //调节
  364. adjustCameraControl.Location = new Point(0, 0);
  365. this.controlPanel.Controls.Add(adjustCameraControl);
  366. break;
  367. case CameraParamType.Frame: //边框
  368. frameCameraControl.Location = new Point(0, 0);
  369. this.controlPanel.Controls.Add(frameCameraControl);
  370. break;
  371. case CameraParamType.Common: //常规
  372. commonCameraControl.Location = new Point(0, 0);
  373. this.controlPanel.Controls.Add(commonCameraControl);
  374. break;
  375. }
  376. }
  377. /// <summary>
  378. /// 刷新右侧预览图
  379. /// </summary>
  380. private void UpdateRightPreview()
  381. {
  382. }
  383. /// <summary>
  384. /// 另存为 出弹窗
  385. /// </summary>
  386. /// <param name="sender"></param>
  387. /// <param name="e"></param>
  388. private void button3_Click(object sender, EventArgs e)
  389. {
  390. dialog = new CreateNameDialog(this);
  391. dialog.Text = PdnResources.GetString("Menu.Savecamerasettings.text");
  392. dialog.StartPosition = FormStartPosition.CenterParent;
  393. dialog.ShowDialog();
  394. }
  395. /// <summary>
  396. /// 另存为弹窗 确定保存
  397. /// </summary>
  398. /// <param name="name"></param>
  399. public override void GetCreateName(string name)
  400. {
  401. // string currentcomboBox = this.comboBox1.Text;
  402. this.newName = name;
  403. if (CameraConfigs.GetInstance().FileExist(this.newName))
  404. {
  405. MessageBox.Show(PdnResources.GetString("Menu.Namecannotbepeated.Text"), PdnResources.GetString("Menu.ensure.text"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
  406. return;
  407. }
  408. CameraConfigs.GetInstance().Save(this.m_cameraParamModel, this.newName);
  409. dialog.Close();
  410. MessageBox.Show(PdnResources.GetString("Menu.Successfullysaved.text"));
  411. }
  412. /// <summary>
  413. /// 保存
  414. /// </summary>
  415. /// <param name="sender"></param>
  416. /// <param name="e"></param>
  417. private void button2_Click(object sender, EventArgs e)
  418. {
  419. CameraConfigs.GetInstance().Save(this.m_cameraParamModel);
  420. MessageBox.Show(PdnResources.GetString("Menu.Successfullysaved.text"));
  421. }
  422. /// <summary>
  423. /// 将拟合点绘制到空白图上
  424. /// //原文链接:https://blog.csdn.net/guduruyu/article/details/72866144
  425. /// </summary>
  426. /// <param name="key_point">拟合点</param>
  427. /// <param name="n">拟合矩阵的??数量</param>
  428. /// <param name="A">空白图</param>
  429. /// <returns></returns>
  430. private unsafe bool polynomial_curve_fit(OpenCvSharp.Point[] key_point, int n, OpenCvSharp.Mat A)
  431. {
  432. //Number of key points
  433. int N = key_point.Length;
  434. //构造矩阵X
  435. OpenCvSharp.Mat X = new OpenCvSharp.Mat(n + 1, n + 1, OpenCvSharp.MatType.CV_64FC1, new OpenCvSharp.Scalar(0));// cv::Mat X = cv::Mat::zeros(n + 1, n + 1, CV_64FC1);
  436. for (int i = 0; i < n + 1; i++)
  437. {
  438. for (int j = 0; j < n + 1; j++)
  439. {
  440. ((double*)X.Ptr(i))[j] = 0;//111
  441. for (int k = 0; k < N; k++)
  442. {
  443. ((double*)X.Ptr(i))[j] = X/*[i]*/.At<double>(i, j) + (double)Math.Pow(key_point[k].X, i + j);// X.at<double>(i, j) = X.at<double>(i, j) + std::pow(key_point[k].x, i + j);
  444. }
  445. }
  446. }
  447. //构造矩阵Y
  448. OpenCvSharp.Mat Y = new OpenCvSharp.Mat(n + 1, 1, OpenCvSharp.MatType.CV_64FC1, new OpenCvSharp.Scalar(0));// cv::Mat Y = cv::Mat::zeros(n + 1, 1, CV_64FC1);
  449. for (int i = 0; i < n + 1; i++)
  450. {
  451. ((double*)Y.Ptr(i))[0] = 0;//111
  452. for (int k = 0; k < N; k++)
  453. {
  454. ((double*)Y.Ptr(i))[0] = Y.At<double>(i, 0) + (double)(Math.Pow(key_point[k].X, i) * key_point[k].Y);// Y.at<double>(i, 0) = Y.at<double>(i, 0) + std::pow(key_point[k].x, i) * key_point[k].y;
  455. }
  456. }
  457. //求解矩阵A
  458. OpenCvSharp.Cv2.Solve(X, Y, A, OpenCvSharp.DecompTypes.LU);//Cv2.Solve(X, Y, A, DecompTypes.LU); cv::solve(X, Y, A, cv::DECOMP_LU);
  459. return true;
  460. }
  461. private void btnClose_Click(object sender, EventArgs e)
  462. {
  463. this.Close();
  464. this.Dispose();
  465. }
  466. private void btnRest_Click(object sender, EventArgs e)
  467. {
  468. CameraConfigs cameraConfig = CameraConfigs.GetInstance();
  469. Startup.instance.cameraParamModel = cameraConfig.GetDefaultCameraParamModel();
  470. m_cameraParamModel = Startup.instance.cameraParamModel;
  471. if (adjustCameraControl != null)
  472. {
  473. adjustCameraControl.ResetCameraParamModel(Startup.instance.cameraParamModel);
  474. }
  475. if (frameCameraControl != null)
  476. {
  477. frameCameraControl.ResetCameraParamModel(Startup.instance.cameraParamModel);
  478. }
  479. if (commonCameraControl != null)
  480. {
  481. commonCameraControl.ResetCameraParamModel(Startup.instance.cameraParamModel);
  482. }
  483. }
  484. private void CameraSettingDialog_Activated(object sender, EventArgs e)
  485. {
  486. m_cameraParamModel = Startup.instance.cameraParamModel;
  487. lblParamName.Text = CameraConfigs.GetInstance().CurrentConfigFileName;
  488. adjustCameraControl.ResetCameraParamModel(m_cameraParamModel);
  489. frameCameraControl.ResetCameraParamModel(m_cameraParamModel);
  490. commonCameraControl.ResetCameraParamModel(m_cameraParamModel);
  491. }
  492. }
  493. }