DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Curious about the future of data-driven systems? Join our Data Engineering roundtable and learn how to build scalable data platforms.

Data Engineering: The industry has come a long way from organizing unstructured data to adopting today's modern data pipelines. See how.

Threat Detection: Learn core practices for managing security risks and vulnerabilities in your organization — don't regret those threats!

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

Related

  • Hardware-Accelerated OpenGL Rendering in a Linux Container
  • React, Angular, and Vue.js: What’s the Technical Difference?
  • What Is API-First?
  • In-Depth Guide to Using useMemo() Hook in React

Trending

  • 4 Essential Strategies for Enhancing Your Application Security Posture
  • Wow, pnpm, You’re Really Fast
  • Data Governance Essentials: Glossaries, Catalogs, and Lineage (Part 5)
  • Accelerating Connection Handshakes in Trusted Network Environments
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. (C# tutorial) How to create edge detection function

(C# tutorial) How to create edge detection function

By 
Mahendra Gadhavi user avatar
Mahendra Gadhavi
·
Feb. 06, 15 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
3.1K Views

Join the DZone community and get the full member experience.

Join For Free

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!

// Form1.Designer.cs

namespace EdgeDetection
{
    partial class Form1
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.btn_Set = new System.Windows.Forms.Button();
            this.tb_CannyThreshold = new System.Windows.Forms.TextBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.chk_Colorized = new System.Windows.Forms.CheckBox();
            this.label3 = new System.Windows.Forms.Label();
            this.tb_CannyThresholdLinking = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.label1.Location = new System.Drawing.Point(30, 265);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(87, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Original image";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.label2.Location = new System.Drawing.Point(370, 265);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(103, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "Processed image";
            // 
            // btn_Set
            // 
            this.btn_Set.Location = new System.Drawing.Point(182, 188);
            this.btn_Set.Name = "btn_Set";
            this.btn_Set.Size = new System.Drawing.Size(58, 23);
            this.btn_Set.TabIndex = 2;
            this.btn_Set.Text = "Set";
            this.btn_Set.UseVisualStyleBackColor = true;
            this.btn_Set.Click += new System.EventHandler(this.btn_Set_Click);
            // 
            // tb_CannyThreshold
            // 
            this.tb_CannyThreshold.Location = new System.Drawing.Point(153, 31);
            this.tb_CannyThreshold.Name = "tb_CannyThreshold";
            this.tb_CannyThreshold.Size = new System.Drawing.Size(87, 20);
            this.tb_CannyThreshold.TabIndex = 4;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.chk_Colorized);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.tb_CannyThresholdLinking);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.btn_Set);
            this.groupBox1.Controls.Add(this.tb_CannyThreshold);
            this.groupBox1.Location = new System.Drawing.Point(677, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(258, 230);
            this.groupBox1.TabIndex = 12;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Settings";
            // 
            // chk_Colorized
            // 
            this.chk_Colorized.AutoSize = true;
            this.chk_Colorized.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.chk_Colorized.Location = new System.Drawing.Point(98, 115);
            this.chk_Colorized.Name = "chk_Colorized";
            this.chk_Colorized.Size = new System.Drawing.Size(69, 17);
            this.chk_Colorized.TabIndex = 15;
            this.chk_Colorized.Text = "Colorized";
            this.chk_Colorized.UseVisualStyleBackColor = true;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(27, 76);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(121, 13);
            this.label3.TabIndex = 14;
            this.label3.Text = "CannyThresholdLinking:";
            // 
            // tb_CannyThresholdLinking
            // 
            this.tb_CannyThresholdLinking.Location = new System.Drawing.Point(153, 73);
            this.tb_CannyThresholdLinking.Name = "tb_CannyThresholdLinking";
            this.tb_CannyThresholdLinking.Size = new System.Drawing.Size(87, 20);
            this.tb_CannyThresholdLinking.TabIndex = 13;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(61, 34);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(87, 13);
            this.label4.TabIndex = 11;
            this.label4.Text = "CannyThreshold:";
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(947, 307);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "MainForm";
            this.Text = "Edge Detection";
            this.Load += new System.EventHandler(this.MainForm_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button btn_Set;
        private System.Windows.Forms.TextBox tb_CannyThreshold;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox tb_CannyThresholdLinking;
        private System.Windows.Forms.CheckBox chk_Colorized;
    }
}

// Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;
using Ozeki.Media.MediaHandlers;
using Ozeki.Media.MediaHandlers.Video;
using Ozeki.Media.MediaHandlers.Video.CV;
using Ozeki.Media.MediaHandlers.Video.CV.Processer;
using Ozeki.Media.Video.Controls;

namespace EdgeDetection
{
    public partial class Form1 : Form
    {
        WebCamera _webCamera;
        MediaConnector _connector;

        ImageProcesserHandler _imageProcesserHandler;
        IEdgeDetector _edgeDetector;
        FrameCapture _frameCapture;

        VideoViewerWF _originalView;
        VideoViewerWF _processedView;
        DrawingImageProvider _originalImageProvider;
        DrawingImageProvider _processedImageProvider;

        public Form1()
        {
            InitializeComponent();
        }

        void MainForm_Load(object sender, EventArgs e)
        {
            Init();

            SetVideoViewers();

            InitDetectorFields();

            ConnectWebcam();

            Start();
        }

        void Init()
        {
            _frameCapture = new FrameCapture();
            _frameCapture.SetInterval(5);

            _webCamera = WebCamera.GetDefaultDevice();
            _connector = new MediaConnector();
            _originalImageProvider = new DrawingImageProvider();
            _processedImageProvider = new DrawingImageProvider();

            _edgeDetector = ImageProcesserFactory.CreateEdgeDetector();

            _imageProcesserHandler = new ImageProcesserHandler();
            _imageProcesserHandler.AddProcesser(_edgeDetector);
        }

        void SetVideoViewers()
        {
            _originalView = new VideoViewerWF
            {
                BackColor = Color.Black,
                Location = new Point(10, 20),
                Size = new Size(320, 240)
            };

            _originalView.SetImageProvider(_originalImageProvider);
            Controls.Add(_originalView);

            _processedView = new VideoViewerWF
            {
                BackColor = Color.Black,
                Location = new Point(350, 20),
                Size = new Size(320, 240)
            };

            _processedView.SetImageProvider(_processedImageProvider);
            Controls.Add(_processedView);
        }

        void InitDetectorFields()
        {
            InvokeGUIThread(() =>
            {
                tb_CannyThreshold.Text = _edgeDetector.CannyThreshold.ToString();
                tb_CannyThresholdLinking.Text = _edgeDetector.CannyThresholdLinking.ToString();
                chk_Colorized.Checked = _edgeDetector.Colorized;
            });
        }

        void ConnectWebcam()
        {
            _connector.Connect(_webCamera, _originalImageProvider);

            _connector.Connect(_webCamera, _frameCapture);
            _connector.Connect(_frameCapture, _imageProcesserHandler);
            _connector.Connect(_imageProcesserHandler, _processedImageProvider);
        }

        void Start()
        {
            _originalView.Start();
            _processedView.Start();

            _frameCapture.Start();
            _webCamera.Start();
        }

        void btn_Set_Click(object sender, EventArgs e)
        {
            InvokeGUIThread(() =>
            {
                _edgeDetector.CannyThreshold = Double.Parse(tb_CannyThreshold.Text);
                _edgeDetector.CannyThresholdLinking = Double.Parse(tb_CannyThresholdLinking.Text);
                _edgeDetector.Colorized = chk_Colorized.Checked;
            });
        }

        void InvokeGUIThread(Action action)
        {
            BeginInvoke(action);
        }
    }
}
Interface (computing) application Trust (business) Dig (command) csharp Computer Library

Opinions expressed by DZone contributors are their own.

Related

  • Hardware-Accelerated OpenGL Rendering in a Linux Container
  • React, Angular, and Vue.js: What’s the Technical Difference?
  • What Is API-First?
  • In-Depth Guide to Using useMemo() Hook in React

Partner Resources


Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: