12,251,027 members (42,042 online)
alternative version

28.7K views
11 bookmarked
Posted

# New Method of Edge Detection

, 17 Sep 2010 CPOL
 Rate this:
This is a flexible and scalable way of edge detection.

## Introduction

This is a new method of edge detection and it is flexible and scalable.

## Background

I had the need for a flexible and scalable edge detection. So, I could use sobel, prewitt or canny. In sobel & prewitt, it is hard to change the edge thickness. But my edge detection has only a threshold value, if you change it, output will be different.

## Why is it New

Sobel & Prewitt edge detection perform a 3X3 matrix multiplication operation upon all the pixels on the image and their complexity is width * height * 3 * 3. But my method of edge detection finds the difference among the neighbouring pixels and if any difference is greater or equal than the threshold value (normally 90), then consider them as an edge and its complexity is width * height. If we increase the threshold value, then edge will decrease, and if threshold value decreases, then the edge will increase. So, there is an inverse relation among the edge and the threshold value. But in sobel & prewitt, we cannot increase or decrease the edges. For different image, sometime, we need to increase or decrease the edges.

## Concept

The color difference of two neighbouring pixels in opposite direction of an edge is large. Without edge, color difference of neighbour pixels is small.

## How Does it Work?

Each pixel has 8 neighbours. If pixel co-ordinate is x & y. Then its neighbouring pixels are (x+1,y+1), (x,y+1), (x+1,y), (x-1,y-1), (x,y-1), (x-1,y), (x-1,y+1) & (x+1,y-1). The color difference of two pixels means RGB difference among the two pixels. If two pixels are (x,y) & (a,b), then the color difference will be:

```int total = Math.Abs(b.GetPixel(a, b).B - b.GetPixel(x, y).B) +
Math.Abs(b.GetPixel(a, b).R - b.GetPixel(x, y).R) +
Math.Abs(b.GetPixel(a, b).G - b.GetPixel(x, y).G);```

In my method of edge detection, 1st takes a bitmap whose height & width is similar to the original image and the color of all its pixels is white.

```int n, m;
n = b.Height;
m = b.Width;
int total = 0;
Bitmap B1 = new Bitmap(m, n);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
{
B1.SetPixel(i, j, Color.White);
}```

Then for each pixel of an image, it determines the color difference, of its neighbour's pixels. If any neighbour's color difference is greater or equal than the threshold value, then it is an edge & gives the pixel value Black. Normally threshold value is 90. But if we are increasing the threshold value, then the edge will decrease and if the threshold value decreases then the edge will increase.

```for (int i = 1; i < m - 1; i++)
for (int j = 1; j < n - 1; j++)
{
total = Math.Abs(b.GetPixel(i + 1, j + 1).B - b.GetPixel(i, j).B) +
Math.Abs(b.GetPixel(i + 1, j + 1).R - b.GetPixel(i, j).R) +
Math.Abs(b.GetPixel(i + 1, j + 1).G - b.GetPixel(i, j).G);
if (total > _proposedThresoldValue)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i, j + 1).B - b.GetPixel(i, j).B) +
Math.Abs(b.GetPixel(i, j + 1).R - b.GetPixel(i, j).R) +
Math.Abs(b.GetPixel(i, j + 1).G - b.GetPixel(i, j).G);
if (total > _proposedThresoldValue)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i + 1, j).B - b.GetPixel(i, j).B) +
Math.Abs(b.GetPixel(i + 1, j).R - b.GetPixel(i, j).R) +
Math.Abs(b.GetPixel(i + 1, j).G - b.GetPixel(i, j).G);
if (total > _proposedThresoldValue)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i - 1, j - 1).B - b.GetPixel(i, j).B) +
Math.Abs(b.GetPixel(i - 1, j - 1).R - b.GetPixel(i, j).R) +
Math.Abs(b.GetPixel(i - 1, j - 1).G - b.GetPixel(i, j).G);
if (total > _proposedThresoldValue)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i, j - 1).B - b.GetPixel(i, j).B) +
Math.Abs(b.GetPixel(i, j - 1).R - b.GetPixel(i, j).R) +
Math.Abs(b.GetPixel(i, j - 1).G - b.GetPixel(i, j).G);
if (total > _proposedThresoldValue)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i - 1, j).B - b.GetPixel(i, j).B) +
Math.Abs(b.GetPixel(i - 1, j).R - b.GetPixel(i, j).R) +
Math.Abs(b.GetPixel(i - 1, j).G - b.GetPixel(i, j).G);
if (total > _proposedThresoldValue)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i - 1, j + 1).B - b.GetPixel(i, j).B) +
Math.Abs(b.GetPixel(i - 1, j + 1).R - b.GetPixel(i, j).R) +
Math.Abs(b.GetPixel(i - 1, j + 1).G - b.GetPixel(i, j).G);
if (total > _proposedThresoldValue)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i + 1, j - 1).B - b.GetPixel(i, j).B) +
Math.Abs(b.GetPixel(i + 1, j - 1).R - b.GetPixel(i, j).R) +
Math.Abs(b.GetPixel(i + 1, j - 1).G - b.GetPixel(i, j).G);
if (total > _proposedThresoldValue)
{ B1.SetPixel(i, j, Color.Black); continue; }
}```

We can compare my method of edge detection to sobel or prewitt edge detection. For comparing this, we have to select sobel or prewitt check box and proposed check box. Black pixel indicates that this pixel is common for proposed and sobel/prewitt. Red pixel indicates that this pixel is only for sobel/prewitt. And blue pixel indicates that this pixel is only for proposed.

```if (checkBox_sobel.Checked && checkBox_pro.Checked)
{
Bitmap b1 = (Bitmap)pictureBox2.Image;
Bitmap b2 = (Bitmap)pictureBox4.Image;
Bitmap map = new Bitmap(pictureBox2.Image.Width, pictureBox2.Image.Height);

for (int i = 1; i < b1.Width - 1; i++)
for (int j = 1; j < b1.Height - 1; j++)
{
if (b1.GetPixel(i, j).R == 0 && b2.GetPixel(i, j).R == 0)
map.SetPixel(i, j, Color.Black);
else if (b1.GetPixel(i, j).R == 0)
map.SetPixel(i, j, Color.Red);
else if (b2.GetPixel(i, j).R == 0)
map.SetPixel(i, j, Color.Blue);
}

pictureBox5.Image = map;
}
else if (checkBox_prewitt.Checked && checkBox_pro.Checked)
{
Bitmap b1 = (Bitmap)pictureBox3.Image;
Bitmap b2 = (Bitmap)pictureBox4.Image;
Bitmap map = new Bitmap(pictureBox3.Image.Width, pictureBox3.Image.Height);

for (int i = 1; i < b1.Width - 1; i++)
for (int j = 1; j < b1.Height - 1; j++)
{
if (b1.GetPixel(i, j).R == 0 && b2.GetPixel(i, j).R == 0)
map.SetPixel(i, j, Color.Black);
else if (b1.GetPixel(i, j).R == 0)
map.SetPixel(i, j, Color.Red);
else if (b2.GetPixel(i, j).R == 0)
map.SetPixel(i, j, Color.Blue);
}

pictureBox5.Image = map;
}```

If we decrease the threshold value, than edge will be thicker and if we increase the threshold value, then edge will be wider.

## History

• 10th September, 2010: Initial post

## Share

I am Committed to professionalism, eagerness to learn and has strong passion in software engineering.

## You may also be interested in...

 First Prev Next
 New Method of Edge Detection John Fortier12-Jul-14 13:32 John Fortier 12-Jul-14 13:32
 Re: New Method of Edge Detection shakil03040037-Jun-15 0:32 shakil0304003 7-Jun-15 0:32
 Need of DB connectivity for Emotion Detection shree_ias18-Mar-11 19:25 shree_ias 18-Mar-11 19:25
 Re: Need of DB connectivity for Emotion Detection shakil030400320-Mar-11 6:29 shakil0304003 20-Mar-11 6:29
 Re: Need of DB connectivity for Emotion Detection shakil030400320-Mar-11 7:00 shakil0304003 20-Mar-11 7:00
 Nice!!! s.Crazy1-Feb-11 0:14 s.Crazy 1-Feb-11 0:14
 Re: Nice!!! shakil03040031-Feb-11 21:44 shakil0304003 1-Feb-11 21:44
 My vote of 1 tttdolph20-Sep-10 15:21 tttdolph 20-Sep-10 15:21
 Re: My vote of 1 shakil030400320-Sep-10 17:43 shakil0304003 20-Sep-10 17:43
 SUSAN tttdolph20-Sep-10 15:17 tttdolph 20-Sep-10 15:17
 Re: SUSAN shakil030400320-Sep-10 17:44 shakil0304003 20-Sep-10 17:44
 good Christ Kennedy17-Sep-10 4:57 Christ Kennedy 17-Sep-10 4:57
 Re: good shakil03040037-Oct-10 5:04 shakil0304003 7-Oct-10 5:04
 You've already been told this... Dave Kreskowiak10-Sep-10 2:03 Dave Kreskowiak 10-Sep-10 2:03
 Re: You've already been told this... shakil030400310-Sep-10 6:34 shakil0304003 10-Sep-10 6:34
 Re: You've already been told this... Dave Kreskowiak10-Sep-10 12:50 Dave Kreskowiak 10-Sep-10 12:50
 Last Visit: 31-Dec-99 18:00     Last Update: 4-May-16 8:50 Refresh 1