PageRenderTime 88ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/TextureRecognition/TeachTextureClass.cs

https://bitbucket.org/Bloov/texture-recognition
C# | 329 lines | 294 code | 33 blank | 2 comment | 30 complexity | bb50b0d93326bf2f942cee4586b57e07 MD5 | raw file
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using ImageRecognition;
  12. namespace TextureRecognition
  13. {
  14. public partial class TeachTextureClass : Form
  15. {
  16. private const int CP_NOCLOSE_BUTTON = 0x200;
  17. private static TeachTextureClass instance;
  18. private TextureRecognition recognition;
  19. private Image classColor;
  20. private List<string> filesToTeach;
  21. private Dictionary<string, Bitmap> images;
  22. private ImageList imageList;
  23. public TeachTextureClass()
  24. {
  25. instance = this;
  26. InitializeComponent();
  27. recognition = TextureRecognition.Instance;
  28. filesToTeach = new List<string>();
  29. images = new Dictionary<string, Bitmap>();
  30. imageList = new ImageList();
  31. imageList.ImageSize = new System.Drawing.Size(40, 40);
  32. imageList.ColorDepth = ColorDepth.Depth16Bit;
  33. lwImages.LargeImageList = imageList;
  34. classColor = new Bitmap(16, 16);
  35. }
  36. protected override CreateParams CreateParams
  37. {
  38. get
  39. {
  40. CreateParams myCp = base.CreateParams;
  41. myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
  42. return myCp;
  43. }
  44. }
  45. internal static TeachTextureClass Instance
  46. {
  47. get
  48. {
  49. return instance;
  50. }
  51. }
  52. private void TeachTextureClass_VisibleChanged(object sender, EventArgs e)
  53. {
  54. if (Visible)
  55. {
  56. UpdateTextureClass();
  57. this.Focus();
  58. pbTeach.Value = 0;
  59. }
  60. }
  61. private void UpdateTextureClass()
  62. {
  63. if (recognition.CurrentClass == null)
  64. {
  65. MessageBox.Show("Указанный текстурный класс не найден.");
  66. ManageTextureClasses.Instance.Show();
  67. Hide();
  68. return;
  69. }
  70. filesToTeach.Clear();
  71. tsmiClass.Text = "Класс: " + recognition.CurrentClass.Name;
  72. pbImage.Image = null;
  73. UpdateClassData();
  74. UpdateClassColor();
  75. }
  76. private void UpdateClassData()
  77. {
  78. lwImages.Items.Clear();
  79. imageList.Images.Clear();
  80. images.Clear();
  81. for (int i = 0; i < recognition.CurrentClass.KnownSamplesNumber(TextureFeatures.GLCM); ++i)
  82. {
  83. var file = recognition.CurrentClass.GetKnownSample(i);
  84. var image = new Bitmap(file);
  85. images.Add(file, image);
  86. imageList.Images.Add(file, image);
  87. var item = lwImages.Items.Add(Path.GetFileNameWithoutExtension(file), file);
  88. item.Group = lwImages.Groups["lvgKnownSamples"];
  89. }
  90. }
  91. private void UpdateClassColor()
  92. {
  93. var render = Graphics.FromImage(classColor);
  94. render.FillRectangle(
  95. new SolidBrush(recognition.CurrentClass.RegionColor),
  96. 0, 0, classColor.Width, classColor.Height);
  97. tsmiClass.Image = classColor;
  98. }
  99. private void tsmiClose_Click(object sender, EventArgs e)
  100. {
  101. //ManageTextureClasses.Instance.Show();
  102. Hide();
  103. }
  104. private void tsmiExit_Click(object sender, EventArgs e)
  105. {
  106. if (MessageBox.Show("Выйти из приложения?", "Внимание", MessageBoxButtons.YesNo) == DialogResult.Yes)
  107. {
  108. MainForm.Instance.Close();
  109. }
  110. }
  111. private void tsmiPrev_Click(object sender, EventArgs e)
  112. {
  113. var index = recognition.Core.GetTextureClassIndex(recognition.CurrentClass);
  114. var newClass = recognition.Core.GetTextureClass(index - 1);
  115. if (newClass == null)
  116. {
  117. newClass = recognition.Core.GetTextureClass(recognition.Core.TextureClassCount - 1);
  118. }
  119. recognition.CurrentClass = newClass;
  120. UpdateTextureClass();
  121. }
  122. private void tsmiNext_Click(object sender, EventArgs e)
  123. {
  124. var index = recognition.Core.GetTextureClassIndex(recognition.CurrentClass);
  125. var newClass = recognition.Core.GetTextureClass(index + 1);
  126. if (newClass == null)
  127. {
  128. newClass = recognition.Core.GetTextureClass(0);
  129. }
  130. recognition.CurrentClass = newClass;
  131. UpdateTextureClass();
  132. }
  133. private void tsmiChangeName_Click(object sender, EventArgs e)
  134. {
  135. ChangeName.Instance.ShowDialog();
  136. tsmiClass.Text = "Класс: " + recognition.CurrentClass.Name;
  137. }
  138. private void tsmiChandeColor_Click(object sender, EventArgs e)
  139. {
  140. if (colorDialog.ShowDialog() == DialogResult.OK)
  141. {
  142. recognition.CurrentClass.RegionColor = colorDialog.Color;
  143. tsmiClass.Image = null;
  144. UpdateClassColor();
  145. }
  146. }
  147. private void btnSelectFiles_Click(object sender, EventArgs e)
  148. {
  149. if (openImagesDialog.ShowDialog() == DialogResult.OK)
  150. {
  151. pbTeach.Value = 0;
  152. //filesToTeach.Clear();
  153. foreach (var file in openImagesDialog.FileNames)
  154. {
  155. if (recognition.CurrentClass.IsKnowSample(file))
  156. {
  157. continue;
  158. }
  159. filesToTeach.Add(file);
  160. var image = new Bitmap(file);
  161. images.Add(file, image);
  162. imageList.Images.Add(file, image);
  163. var item = lwImages.Items.Add(Path.GetFileNameWithoutExtension(file), file);
  164. item.Group = lwImages.Groups["lvgUnknownSamples"];
  165. item.ForeColor = Color.Red;
  166. }
  167. }
  168. }
  169. private void btnDeleteSelected_Click(object sender, EventArgs e)
  170. {
  171. if (MessageBox.Show("Удалить выбраные образцы?", "Внимание", MessageBoxButtons.YesNo) == DialogResult.Yes)
  172. {
  173. foreach (ListViewItem item in lwImages.SelectedItems)
  174. {
  175. var file = item.ImageKey;
  176. if (recognition.CurrentClass.IsKnowSample(file))
  177. {
  178. recognition.CurrentClass.RemoveSample(file);
  179. }
  180. imageList.Images.RemoveByKey(file);
  181. var image = images[file];
  182. if (image == pbImage.Image)
  183. {
  184. pbImage.Image = null;
  185. }
  186. images.Remove(file);
  187. filesToTeach.Remove(file);
  188. lwImages.Items.Remove(item);
  189. }
  190. }
  191. }
  192. private void btnDeleteAll_Click(object sender, EventArgs e)
  193. {
  194. if (MessageBox.Show("Удалить все образцы?", "Внимание", MessageBoxButtons.YesNo) == DialogResult.Yes)
  195. {
  196. recognition.CurrentClass.RemoveAllSamples();
  197. filesToTeach.Clear();
  198. lwImages.Items.Clear();
  199. images.Clear();
  200. imageList.Images.Clear();
  201. pbImage.Image = null;
  202. }
  203. }
  204. private void lwImages_SelectedIndexChanged(object sender, EventArgs e)
  205. {
  206. if (lwImages.SelectedItems.Count > 0)
  207. {
  208. var file = lwImages.SelectedItems[0].ImageKey;
  209. pbImage.Image = images[file];
  210. }
  211. else
  212. {
  213. pbImage.Image = null;
  214. }
  215. }
  216. private void btnTeachingControl_Click(object sender, EventArgs e)
  217. {
  218. if (recognition.CurrentClass.IsTeaching)
  219. {
  220. recognition.CurrentClass.AborTeaching();
  221. return;
  222. }
  223. pbTeach.Value = 0;
  224. btnDeleteAll.Enabled = false;
  225. btnDeleteSelected.Enabled = false;
  226. btnSelectFiles.Enabled = false;
  227. btnTeachingControl.Text = "Прервать обучение";
  228. btnTeachingControl.Image = resources.pause;
  229. tsmiClose.Enabled = false;
  230. tsmiExit.Enabled = false;
  231. tsmiNext.Enabled = false;
  232. tsmiPrev.Enabled = false;
  233. var sourceImages = GetImagesToTeach(filesToTeach);
  234. recognition.CurrentClass.Teach(filesToTeach, sourceImages);
  235. ThreadPool.QueueUserWorkItem(new WaitCallback(MonitorTeaching));
  236. }
  237. private List<Bitmap> GetImagesToTeach(List<string> files)
  238. {
  239. var result = new List<Bitmap>();
  240. for (int i = 0; i < filesToTeach.Count; ++i)
  241. {
  242. result.Add(images[filesToTeach[i]]);
  243. }
  244. return result;
  245. }
  246. private void MonitorTeaching(object parameter)
  247. {
  248. while (recognition.CurrentClass.IsTeaching)
  249. {
  250. this.BeginInvoke(
  251. new Action(delegate()
  252. {
  253. var value = (int)(pbTeach.Maximum * recognition.CurrentClass.TeachingProgress);
  254. if (value > pbTeach.Maximum)
  255. {
  256. value = pbTeach.Maximum;
  257. }
  258. pbTeach.Value = value;
  259. }));
  260. Thread.Sleep(150);
  261. }
  262. this.BeginInvoke(
  263. new Action(delegate()
  264. {
  265. pbTeach.Value = pbTeach.Maximum;
  266. btnDeleteAll.Enabled = true;
  267. btnDeleteSelected.Enabled = true;
  268. btnSelectFiles.Enabled = true;
  269. btnTeachingControl.Text = "Начать обучение";
  270. btnTeachingControl.Image = resources.refresh;
  271. tsmiClose.Enabled = true;
  272. tsmiExit.Enabled = true;
  273. tsmiNext.Enabled = true;
  274. tsmiPrev.Enabled = true;
  275. UpdateListViewItems();
  276. }));
  277. }
  278. private void UpdateListViewItems()
  279. {
  280. foreach (ListViewItem item in lwImages.Items)
  281. {
  282. if (item.ForeColor == Color.Black)
  283. {
  284. continue;
  285. }
  286. var file = item.ImageKey;
  287. if (recognition.CurrentClass.IsKnowSample(file))
  288. {
  289. filesToTeach.Remove(file);
  290. item.ForeColor = Color.Black;
  291. item.Group = lwImages.Groups["lvgKnownSamples"];
  292. }
  293. }
  294. }
  295. }
  296. }