Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Edge detection in an image using sobel filter in VB.net - C#.net.

Author: Shehzad Hemani
Download Source Code : 1074_source.zip

In this simple article you will learn that how we can detect edges in an image using sobel filter in VB and C#.net.

 To detect the edges in an image we can apply sobel filter on an image. To apply a filter we need a mask. This mask can be of 3*3 matrix.

To apply the sobel filter we make a mask of 3*3. In this mask we initialize the entire matrix with different values as mentioned below. We map the center pixel of matrix on the pixel of image and multiply each matrix pixel with corresponding image pixel and add them. Set that value on the image pixel for that we map the center pixel of matrix. Do this process for each pixel of image. For pixels that are outside the bounds of the image we use zero padding.

First convert image into grayscale then apply median filter to remove noise and then apply sobel filter.

Sobel filter gives you two type of edge detection. One is horizontal edge detection means to detect the edges which are horizontal in an image and second is vertical edge detection.

Horizontal edge detection:

To detect the horizontal edges in an image we have a mask.

1          2          1

0          0          0

-1         -2         -1

Apply this mask on an image.   

To demonstrate make a window application. Drag button and a picture box on form.

Now write the following code on Button click event:

C#

private void btn_Horizontal_Click(object sender, EventArgs e)

        {

            Bitmap img = new Bitmap(pictureBox1.Image);

            Bitmap img1 = new Bitmap(pictureBox1.Image);

            Color c;

            for (int ii = 0; ii < img.Width; ii++)

            {

                for (int jj = 0; jj < img.Height; jj++)

                {

 

                    if (ii - 1 >= 0 && jj - 1 >= 0)

                    {

                        c = img.GetPixel(ii - 1, jj - 1);

                        mask[0] = Convert.ToInt16(c.R);

                    }

                    else

                    {

                        mask[0] = 0;

                    }

 

                    if (jj - 1 >= 0 && ii + 1 < img.Width)

                    {

                        c = img.GetPixel(ii + 1, jj - 1);

                        mask[1] = Convert.ToInt16(c.R);

                    }

                    else

                        mask[1] = 0;

 

                    if (jj - 1 >= 0)

                    {

                        c = img.GetPixel(ii, jj - 1);

                        mask[2] = Convert.ToInt16(c.R)*2;

                    }

                    else

                        mask[2] = 0;

 

                    if (ii + 1 < img.Width)

                    {

                        c = img.GetPixel(ii + 1, jj);

                        mask[3] = 0;

                    }

                    else

                        mask[3] = 0;

 

                    if (ii - 1 >= 0)

                    {

                        c = img.GetPixel(ii - 1, jj);

                        mask[4] = 0;

                    }

                    else

                        mask[4] = 0;

 

                    if (ii - 1 >= 0 && jj + 1 < img.Height)

                    {

                        c = img.GetPixel(ii - 1, jj + 1);

                        mask[5] = Convert.ToInt16(c.R)*-1;

                    }

                    else

                        mask[5] = 0;

 

                    if (jj + 1 < img.Height)

                    {

                        c = img.GetPixel(ii, jj + 1);

                        mask[6] = Convert.ToInt16(c.R)*-2;

                    }

                    else

                        mask[6] = 0;

                    if (ii + 1 < img.Width && jj + 1 < img.Height)

                    {

                        c = img.GetPixel(ii + 1, jj + 1);

                        mask[7] = Convert.ToInt16(c.R)*-1;

                    }

                    else

                        mask[7] = 0;

                    c = img.GetPixel(ii, jj);

                    mask[8] = Convert.ToInt16(c.R) * 0;

                    int sum = 0;

                    for (int u = 0; u < 9; u++)

                        sum = sum + mask[u];

                    long r1 = Convert.ToInt64(sum);

                    long g1 = Convert.ToInt64(sum);

                    long b1 = Convert.ToInt64(sum);

                    g1 = Convert.ToInt64(g1 << 8);

                    b1 = Convert.ToInt64(b1 << 16);

                    int ans = Convert.ToInt16(((byte)r1 | (byte)g1 | (byte)b1));

                    img1.SetPixel(ii, jj, Color.FromArgb(ans, ans, ans));

                }

            }

            pictureBox1.Image = img1;

            MessageBox.Show("successfully Done");

        }

VB

Private Sub btn_Horizontal_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim img As Bitmap = New Bitmap(pictureBox1.Image)
        
Dim img1 As Bitmap = New Bitmap(pictureBox1.Image)
        
Dim As Color
        
Dim ii As Integer = 0
        
Do While (ii < img.Width)
            
Dim jj As Integer = 0
            
Do While (jj < img.Height)
                
If (((ii - 1)  _
                            >
0)  _
                            
AndAlso ((jj - 1)  _
                            >
0)) Then
                    
img.GetPixel((ii - 1), (jj - 1))
                    mask(
0Convert.ToInt16(c.R)
                
Else
                    
mask(00
                
End If
                If 
(((jj - 1)  _
                            >
0)  _
                            
AndAlso (ii + (< img.Width))) Then
                    
img.GetPixel((ii + 1), (jj - 1))
                    mask(
1Convert.ToInt16(c.R)
                
Else
                    
mask(10
                
End If
                If 
((jj - 1)  _
                            >
0Then
                    
img.GetPixel(ii, (jj - 1))
                    mask(
2(Convert.ToInt16(c.R) * 2)
                
Else
                    
mask(20
                
End If
                If 
(ii + (< img.Width)) Then
                    
img.GetPixel((ii + 1), jj)
                    mask(
30
                
Else
                    
mask(30
                
End If
                If 
((ii - 1)  _
                            >
0Then
                    
img.GetPixel((ii - 1), jj)
                    mask(
40
                
Else
                    
mask(40
                
End If
                If 
(((ii - 1)  _
                            >
0)  _
                            
AndAlso (jj + (< img.Height))) Then
                    
img.GetPixel((ii - 1), (jj + 1))
                    mask(
5(Convert.ToInt16(c.R) * -1)
                
Else
                    
mask(50
                
End If
                If 
(jj + (< img.Height)) Then
                    
img.GetPixel(ii, (jj + 1))
                    mask(
6(Convert.ToInt16(c.R) * -2)
                
Else
                    
mask(60
                
End If
                If 
((ii + (< img.Width))  _
                            
AndAlso (jj + (< img.Height))) Then
                    
img.GetPixel((ii + 1), (jj + 1))
                    mask(
7(Convert.ToInt16(c.R) * -1)
                
Else
                    
mask(70
                
End If
                
img.GetPixel(ii, jj)
                mask(
8(Convert.ToInt16(c.R) * 0)
                
Dim sum As Integer = 0
                
Dim As Integer = 0
                
Do While (u < 9)
                    sum 
(sum + mask(u))
                    u 
(u + 1)
                
Loop
                Dim 
r1 As Long = Convert.ToInt64(sum)
                
Dim g1 As Long = Convert.ToInt64(sum)
                
Dim b1 As Long = Convert.ToInt64(sum)
                g1 
Convert.ToInt64((g1 + 8))
                b1 
Convert.ToInt64((b1 + 16))
                
Dim ans As Integer = Convert.ToInt16((CType(r1,Byte)  _
                                
Or (CType(g1,ByteOr CType(b1,Byte))))
                img1.SetPixel(ii, jj, Color.FromArgb(ans, ans, ans))
                jj 
(jj + 1)
            
Loop
            
ii (ii + 1)
        
Loop
        
pictureBox1.Image img1
        MessageBox.Show(
"successfully Done")
    
End Sub

This is simple code to detect horizontal edges in an image.

Vertical edge detection:

To detect the vertical edges in an image we have a mask.

1          0          -1

2          0          -2

1          0          -1

Apply this mask on an image.   

To demonstrate make a window application. Drag button and a picture box on form.

Now write the following code on Button click event:

C#

private void btn_Vertical_Click(object sender, EventArgs e)

        {

            Bitmap img = new Bitmap(pictureBox1.Image);

            Bitmap img1 = new Bitmap(pictureBox1.Image);

            Color c;

            for (int ii = 0; ii < img.Width; ii++)

            {

                for (int jj = 0; jj < img.Height; jj++)

                {

 

                    if (ii - 1 >= 0 && jj - 1 >= 0)

                    {

                        c = img.GetPixel(ii - 1, jj - 1);

                        mask[0] = Convert.ToInt16(c.R);

                    }

                    else

                    {

                        mask[0] = 0;

                    }

 

                    if (jj - 1 >= 0 && ii + 1 < img.Width)

                    {

                        c = img.GetPixel(ii + 1, jj - 1);

                        mask[1] = Convert.ToInt16(c.R)*-1;

                    }

                    else

                        mask[1] = 0;

 

                    if (jj - 1 >= 0)

                    {

                        c = img.GetPixel(ii, jj - 1);

                        mask[2] = Convert.ToInt16(c.R) * 0;

                    }

                    else

                        mask[2] = 0;

 

                    if (ii + 1 < img.Width)

                    {

                        c = img.GetPixel(ii + 1, jj);

                        mask[3] = Convert.ToInt16(c.R) * -2;

                    }

                    else

                        mask[3] = 0;

 

                    if (ii - 1 >= 0)

                    {

                        c = img.GetPixel(ii - 1, jj);

                        mask[4] = Convert.ToInt16(c.R) * 2;

                    }

                    else

                        mask[4] = 0;

 

                    if (ii - 1 >= 0 && jj + 1 < img.Height)

                    {

                        c = img.GetPixel(ii - 1, jj + 1);

                        mask[5] = Convert.ToInt16(c.R) * 1;

                    }

                    else

                        mask[5] = 0;

 

                    if (jj + 1 < img.Height)

                    {

                        c = img.GetPixel(ii, jj + 1);

                        mask[6] = Convert.ToInt16(c.R) * 0;

                    }

                    else

                        mask[6] = 0;

                    if (ii + 1 < img.Width && jj + 1 < img.Height)

                    {

                        c = img.GetPixel(ii + 1, jj + 1);

                        mask[7] = Convert.ToInt16(c.R) * -1;

                    }

                    else

                        mask[7] = 0;

                    c = img.GetPixel(ii, jj);

                    mask[8] = Convert.ToInt16(c.R) * 0;

                    int sum = 0;

                    for (int u = 0; u < 9; u++)

                        sum = sum + mask[u];

                    long r1 = Convert.ToInt64(sum);

                    long g1 = Convert.ToInt64(sum);

                    long b1 = Convert.ToInt64(sum);

                    g1 = Convert.ToInt64(g1 << 8);

                    b1 = Convert.ToInt64(b1 << 16);

                    int ans = Convert.ToInt16(((byte)r1 | (byte)g1 | (byte)b1));

                    img1.SetPixel(ii, jj, Color.FromArgb(ans, ans, ans));

                }

            }

            pictureBox1.Image = img1;

            MessageBox.Show("successfully Done");

        }

VB

Private Sub btn_Vertical_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim img As Bitmap = New Bitmap(pictureBox1.Image)
        
Dim img1 As Bitmap = New Bitmap(pictureBox1.Image)
        
Dim As Color
        
Dim ii As Integer = 0
        
Do While (ii < img.Width)
            
Dim jj As Integer = 0
            
Do While (jj < img.Height)
                
If (((ii - 1)  _
                            >
0)  _
                            
AndAlso ((jj - 1)  _
                            >
0)) Then
                    
img.GetPixel((ii - 1), (jj - 1))
                    mask(
0Convert.ToInt16(c.R)
                
Else
                    
mask(00
                
End If
                If 
(((jj - 1)  _
                            >
0)  _
                            
AndAlso (ii + (< img.Width))) Then
                    
img.GetPixel((ii + 1), (jj - 1))
                    mask(
1(Convert.ToInt16(c.R) * -1)
                
Else
                    
mask(10
                
End If
                If 
((jj - 1)  _
                            >
0Then
                    
img.GetPixel(ii, (jj - 1))
                    mask(
2(Convert.ToInt16(c.R) * 0)
                
Else
                    
mask(20
                
End If
                If 
(ii + (< img.Width)) Then
                    
img.GetPixel((ii + 1), jj)
                    mask(
3(Convert.ToInt16(c.R) * -2)
                
Else
                    
mask(30
                
End If
                If 
((ii - 1)  _
                            >
0Then
                    
img.GetPixel((ii - 1), jj)
                    mask(
4(Convert.ToInt16(c.R) * 2)
                
Else
                    
mask(40
                
End If
                If 
(((ii - 1)  _
                            >
0)  _
                            
AndAlso (jj + (< img.Height))) Then
                    
img.GetPixel((ii - 1), (jj + 1))
                    mask(
5(Convert.ToInt16(c.R) * 1)
                
Else
                    
mask(50
                
End If
                If 
(jj + (< img.Height)) Then
                    
img.GetPixel(ii, (jj + 1))
                    mask(
6(Convert.ToInt16(c.R) * 0)
                
Else
                    
mask(60
                
End If
                If 
((ii + (< img.Width))  _
                            
AndAlso (jj + (< img.Height))) Then
                    
img.GetPixel((ii + 1), (jj + 1))
                    mask(
7(Convert.ToInt16(c.R) * -1)
                
Else
                    
mask(70
                
End If
                
img.GetPixel(ii, jj)
                mask(
8(Convert.ToInt16(c.R) * 0)
                
Dim sum As Integer = 0
                
Dim As Integer = 0
                
Do While (u < 9)
                    sum 
(sum + mask(u))
                    u 
(u + 1)
                
Loop
                Dim 
r1 As Long = Convert.ToInt64(sum)
                
Dim g1 As Long = Convert.ToInt64(sum)
                
Dim b1 As Long = Convert.ToInt64(sum)
                g1 
Convert.ToInt64((g1 + 8))
                b1 
Convert.ToInt64((b1 + 16))
                
Dim ans As Integer = Convert.ToInt16((CType(r1,Byte)  _
                                
Or (CType(g1,ByteOr CType(b1,Byte))))
                img1.SetPixel(ii, jj, Color.FromArgb(ans, ans, ans))
                jj 
(jj + 1)
            
Loop
            
ii (ii + 1)
        
Loop
        
pictureBox1.Image img1
        MessageBox.Show(
"successfully Done")
    
End Sub

This is simple code to detect vertical edges in an image.

 Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {          

                this.Text = "DEVASP Image APPLICATION";

                for (int i = 0; i < 9; i++)

                {    

                    mask[i] = 1;

                }

        }

VB

 
    
Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)
        
Me.Text "DEVASP Image APPLICATION"
        
Dim As Integer = 0
        
Do While (i < 9)
            mask(i) 
1
            
(i + 1)
        
Loop
    End Sub

This simple article tells that how we can detect edges in an image using sobel filter in VB and C#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Object collision in vb.net – C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net