(C# tutorial) How to create edge detection function


/ Published in: C#
Save to your folder(s)

I am really interested in Computer Vision technology, so I started to dig deeper in this topic. This is how I found the code below for edge detection, a method belonging to object detection.
If you are interested in implementing edge detection in C#, too, here you can find the code I tried. The code is from a prewritten C# camera library you can all access.

To develop the edge detection function you only need to have a Visual C# WPF Application created in Visual Studio and the VOIPSDK.dll and NVA.dll files (from www.camera-sdk.com ) added to the references.

Creating the user interface is the first step. It will help you to use edge detection by providing an easy-to-use interface. You will have two fields to display the original image and the processed image of the camera, you can set values for Canny Threshold and Canny Threshold Linking and you can set whether you want the edges of the detected elements to be white or colorized. You can find the code of the GUI under Form1.Designer.cs.

Under Form1.cs there is the code for the edge detection function. You can see how to code is built up and what you should to create this function. There will be the different methods you have to call and all the configurations are described.

Trust me, guys, this source code will help you a lot, it made my life easier. Good luck!


Copy this code and paste it in your HTML
  1. // Form1.Designer.cs
  2.  
  3. namespace EdgeDetection
  4. {
  5. partial class Form1
  6. {
  7. /// <summary>
  8. /// Required designer variable.
  9. /// </summary>
  10. private System.ComponentModel.IContainer components = null;
  11.  
  12. /// <summary>
  13. /// Clean up any resources being used.
  14. /// </summary>
  15. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  16. protected override void Dispose(bool disposing)
  17. {
  18. if (disposing && (components != null))
  19. {
  20. components.Dispose();
  21. }
  22. base.Dispose(disposing);
  23. }
  24.  
  25. #region Windows Form Designer generated code
  26.  
  27. /// <summary>
  28. /// Required method for Designer support - do not modify
  29. /// the contents of this method with the code editor.
  30. /// </summary>
  31. private void InitializeComponent()
  32. {
  33. this.label1 = new System.Windows.Forms.Label();
  34. this.label2 = new System.Windows.Forms.Label();
  35. this.btn_Set = new System.Windows.Forms.Button();
  36. this.tb_CannyThreshold = new System.Windows.Forms.TextBox();
  37. this.groupBox1 = new System.Windows.Forms.GroupBox();
  38. this.chk_Colorized = new System.Windows.Forms.CheckBox();
  39. this.label3 = new System.Windows.Forms.Label();
  40. this.tb_CannyThresholdLinking = new System.Windows.Forms.TextBox();
  41. this.label4 = new System.Windows.Forms.Label();
  42. this.groupBox1.SuspendLayout();
  43. this.SuspendLayout();
  44. //
  45. // label1
  46. //
  47. this.label1.AutoSize = true;
  48. this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
  49. this.label1.Location = new System.Drawing.Point(30, 265);
  50. this.label1.Name = "label1";
  51. this.label1.Size = new System.Drawing.Size(87, 13);
  52. this.label1.TabIndex = 0;
  53. this.label1.Text = "Original image";
  54. //
  55. // label2
  56. //
  57. this.label2.AutoSize = true;
  58. this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
  59. this.label2.Location = new System.Drawing.Point(370, 265);
  60. this.label2.Name = "label2";
  61. this.label2.Size = new System.Drawing.Size(103, 13);
  62. this.label2.TabIndex = 1;
  63. this.label2.Text = "Processed image";
  64. //
  65. // btn_Set
  66. //
  67. this.btn_Set.Location = new System.Drawing.Point(182, 188);
  68. this.btn_Set.Name = "btn_Set";
  69. this.btn_Set.Size = new System.Drawing.Size(58, 23);
  70. this.btn_Set.TabIndex = 2;
  71. this.btn_Set.Text = "Set";
  72. this.btn_Set.UseVisualStyleBackColor = true;
  73. this.btn_Set.Click += new System.EventHandler(this.btn_Set_Click);
  74. //
  75. // tb_CannyThreshold
  76. //
  77. this.tb_CannyThreshold.Location = new System.Drawing.Point(153, 31);
  78. this.tb_CannyThreshold.Name = "tb_CannyThreshold";
  79. this.tb_CannyThreshold.Size = new System.Drawing.Size(87, 20);
  80. this.tb_CannyThreshold.TabIndex = 4;
  81. //
  82. // groupBox1
  83. //
  84. this.groupBox1.Controls.Add(this.chk_Colorized);
  85. this.groupBox1.Controls.Add(this.label3);
  86. this.groupBox1.Controls.Add(this.tb_CannyThresholdLinking);
  87. this.groupBox1.Controls.Add(this.label4);
  88. this.groupBox1.Controls.Add(this.btn_Set);
  89. this.groupBox1.Controls.Add(this.tb_CannyThreshold);
  90. this.groupBox1.Location = new System.Drawing.Point(677, 12);
  91. this.groupBox1.Name = "groupBox1";
  92. this.groupBox1.Size = new System.Drawing.Size(258, 230);
  93. this.groupBox1.TabIndex = 12;
  94. this.groupBox1.TabStop = false;
  95. this.groupBox1.Text = "Settings";
  96. //
  97. // chk_Colorized
  98. //
  99. this.chk_Colorized.AutoSize = true;
  100. this.chk_Colorized.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
  101. this.chk_Colorized.Location = new System.Drawing.Point(98, 115);
  102. this.chk_Colorized.Name = "chk_Colorized";
  103. this.chk_Colorized.Size = new System.Drawing.Size(69, 17);
  104. this.chk_Colorized.TabIndex = 15;
  105. this.chk_Colorized.Text = "Colorized";
  106. this.chk_Colorized.UseVisualStyleBackColor = true;
  107. //
  108. // label3
  109. //
  110. this.label3.AutoSize = true;
  111. this.label3.Location = new System.Drawing.Point(27, 76);
  112. this.label3.Name = "label3";
  113. this.label3.Size = new System.Drawing.Size(121, 13);
  114. this.label3.TabIndex = 14;
  115. this.label3.Text = "CannyThresholdLinking:";
  116. //
  117. // tb_CannyThresholdLinking
  118. //
  119. this.tb_CannyThresholdLinking.Location = new System.Drawing.Point(153, 73);
  120. this.tb_CannyThresholdLinking.Name = "tb_CannyThresholdLinking";
  121. this.tb_CannyThresholdLinking.Size = new System.Drawing.Size(87, 20);
  122. this.tb_CannyThresholdLinking.TabIndex = 13;
  123. //
  124. // label4
  125. //
  126. this.label4.AutoSize = true;
  127. this.label4.Location = new System.Drawing.Point(61, 34);
  128. this.label4.Name = "label4";
  129. this.label4.Size = new System.Drawing.Size(87, 13);
  130. this.label4.TabIndex = 11;
  131. this.label4.Text = "CannyThreshold:";
  132. //
  133. // MainForm
  134. //
  135. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  136. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  137. this.ClientSize = new System.Drawing.Size(947, 307);
  138. this.Controls.Add(this.groupBox1);
  139. this.Controls.Add(this.label2);
  140. this.Controls.Add(this.label1);
  141. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  142. this.MaximizeBox = false;
  143. this.Name = "MainForm";
  144. this.Text = "Edge Detection";
  145. this.Load += new System.EventHandler(this.MainForm_Load);
  146. this.groupBox1.ResumeLayout(false);
  147. this.groupBox1.PerformLayout();
  148. this.ResumeLayout(false);
  149. this.PerformLayout();
  150.  
  151. }
  152.  
  153. #endregion
  154.  
  155. private System.Windows.Forms.Label label1;
  156. private System.Windows.Forms.Label label2;
  157. private System.Windows.Forms.Button btn_Set;
  158. private System.Windows.Forms.TextBox tb_CannyThreshold;
  159. private System.Windows.Forms.GroupBox groupBox1;
  160. private System.Windows.Forms.Label label4;
  161. private System.Windows.Forms.Label label3;
  162. private System.Windows.Forms.TextBox tb_CannyThresholdLinking;
  163. private System.Windows.Forms.CheckBox chk_Colorized;
  164. }
  165. }
  166.  
  167. // Form1.cs
  168.  
  169. using System;
  170. using System.Drawing;
  171. using System.Windows.Forms;
  172. using Ozeki.Media.MediaHandlers;
  173. using Ozeki.Media.MediaHandlers.Video;
  174. using Ozeki.Media.MediaHandlers.Video.CV;
  175. using Ozeki.Media.MediaHandlers.Video.CV.Processer;
  176. using Ozeki.Media.Video.Controls;
  177.  
  178. namespace EdgeDetection
  179. {
  180. public partial class Form1 : Form
  181. {
  182. WebCamera _webCamera;
  183. MediaConnector _connector;
  184.  
  185. ImageProcesserHandler _imageProcesserHandler;
  186. IEdgeDetector _edgeDetector;
  187. FrameCapture _frameCapture;
  188.  
  189. VideoViewerWF _originalView;
  190. VideoViewerWF _processedView;
  191. DrawingImageProvider _originalImageProvider;
  192. DrawingImageProvider _processedImageProvider;
  193.  
  194. public Form1()
  195. {
  196. InitializeComponent();
  197. }
  198.  
  199. void MainForm_Load(object sender, EventArgs e)
  200. {
  201. Init();
  202.  
  203. SetVideoViewers();
  204.  
  205. InitDetectorFields();
  206.  
  207. ConnectWebcam();
  208.  
  209. Start();
  210. }
  211.  
  212. void Init()
  213. {
  214. _frameCapture = new FrameCapture();
  215. _frameCapture.SetInterval(5);
  216.  
  217. _webCamera = WebCamera.GetDefaultDevice();
  218. _connector = new MediaConnector();
  219. _originalImageProvider = new DrawingImageProvider();
  220. _processedImageProvider = new DrawingImageProvider();
  221.  
  222. _edgeDetector = ImageProcesserFactory.CreateEdgeDetector();
  223.  
  224. _imageProcesserHandler = new ImageProcesserHandler();
  225. _imageProcesserHandler.AddProcesser(_edgeDetector);
  226. }
  227.  
  228. void SetVideoViewers()
  229. {
  230. _originalView = new VideoViewerWF
  231. {
  232. BackColor = Color.Black,
  233. Location = new Point(10, 20),
  234. Size = new Size(320, 240)
  235. };
  236.  
  237. _originalView.SetImageProvider(_originalImageProvider);
  238. Controls.Add(_originalView);
  239.  
  240. _processedView = new VideoViewerWF
  241. {
  242. BackColor = Color.Black,
  243. Location = new Point(350, 20),
  244. Size = new Size(320, 240)
  245. };
  246.  
  247. _processedView.SetImageProvider(_processedImageProvider);
  248. Controls.Add(_processedView);
  249. }
  250.  
  251. void InitDetectorFields()
  252. {
  253. InvokeGUIThread(() =>
  254. {
  255. tb_CannyThreshold.Text = _edgeDetector.CannyThreshold.ToString();
  256. tb_CannyThresholdLinking.Text = _edgeDetector.CannyThresholdLinking.ToString();
  257. chk_Colorized.Checked = _edgeDetector.Colorized;
  258. });
  259. }
  260.  
  261. void ConnectWebcam()
  262. {
  263. _connector.Connect(_webCamera, _originalImageProvider);
  264.  
  265. _connector.Connect(_webCamera, _frameCapture);
  266. _connector.Connect(_frameCapture, _imageProcesserHandler);
  267. _connector.Connect(_imageProcesserHandler, _processedImageProvider);
  268. }
  269.  
  270. void Start()
  271. {
  272. _originalView.Start();
  273. _processedView.Start();
  274.  
  275. _frameCapture.Start();
  276. _webCamera.Start();
  277. }
  278.  
  279. void btn_Set_Click(object sender, EventArgs e)
  280. {
  281. InvokeGUIThread(() =>
  282. {
  283. _edgeDetector.CannyThreshold = Double.Parse(tb_CannyThreshold.Text);
  284. _edgeDetector.CannyThresholdLinking = Double.Parse(tb_CannyThresholdLinking.Text);
  285. _edgeDetector.Colorized = chk_Colorized.Checked;
  286. });
  287. }
  288.  
  289. void InvokeGUIThread(Action action)
  290. {
  291. BeginInvoke(action);
  292. }
  293. }
  294. }

URL: http://www.camera-sdk.com/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.