Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 10:44, 17 December 2013 by somnathbanik (Talk | contribs)

Edge detection filter in Windows Phone

From Wiki
Jump to: navigation, search

Note.pngNote: This is an entry in the Nokia Imaging and Big UI Wiki Competition 2013Q4.

This article demonstrates how to create an edge detection filter in Windows Phone.

WP Metro Icon Multimedia.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
Article Metadata
Code ExampleTested with
Devices(s): Nokia Lumia 820,920
Windows Phone 8
Created: somnathbanik (11 Dec 2013)
Last edited: somnathbanik (17 Dec 2013)



Edge detection is used to identify the borders of different objects in an image. There are a number of mathematical techniques for edge detection, most of which work by identifying the points at which the image brightness changes sharply.

This article shows how to create an edge detection filter that marks edges with black points. The code example app allows the user to load an image from their gallery and then apply the filter in order to view the result.

Note.pngNote: This filter is not intended for use with the Nokia Imaging SDK.


Create a standard Windows Phone Project

Let’s create a standard Windows Phone Application Project

  • Launch Visual Studio
  • Click on File
  • Click on New Project
  • Select Windows Phone App (Visual C# Template)
  • Add Name and Location of the project
  • Click OK to create the project.

Creating UI

  • Once you are done creating the standard project let’s add two Buttons and two Image controls in the MainPage.xaml. These controls are added to select an original image from the picture hub and apply edge filter on to it.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical">
<Image x:Name="imageOriginal" Height="300" Stretch="Uniform" Margin="12,0,12,0" VerticalAlignment="Top" HorizontalAlignment="Left"></Image>
<Button Content="Select Image" Click="Button_Click_1" ></Button>
<Image x:Name="imageProcessed" Height="300" Stretch="Uniform" Margin="12,0,12,0" VerticalAlignment="Top" HorizontalAlignment="Left"></Image>
<Button Content="Detect Edge" Click="Button_Click_2" ></Button>

Code Behind

  • Use the Photo Chooser Task to select an image from the gallery and store it to a BitmapImage
public MainPage()
//select picture from picture hub
photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
void photoChooserTask_Completed(object sender, PhotoResult e)
if (e.TaskResult == TaskResult.OK)
bmp = new BitmapImage();
imageOriginal.Source = bmp;
  • We add the WriteableBitmapEx library to our project reference to get RGB value of a pixel. The EdgeDetection() method calculates the edges of an image and returns the final image to the called function.
private WriteableBitmap EdgeDetection(WriteableBitmap bmp)
WriteableBitmap bitmap = new WriteableBitmap(bmp.PixelWidth, bmp.PixelHeight);
for (int i = 1; i < bmp.PixelWidth-1; i++)
for (int j = 1; j < bmp.PixelHeight-1; j++)
Color cx1 = bmp.GetPixel(i - 1, j);
Color cx2 = bmp.GetPixel(i, j);
Color cx3 = bmp.GetPixel(i + 1, j);
Color cy1 = bmp.GetPixel(i, j - 1);
Color cy2 = bmp.GetPixel(i, j);
Color cy3 = bmp.GetPixel(i, j + 1);
int lx = (cx1.R + cx1.G + cx1.B) + (cx3.R + cx3.G + cx3.B) - (2 * (cx2.R + cx2.G + cx2.B));
int ly = (cy1.R + cy1.G + cy1.B) + (cy3.R + cy3.G + cy3.B) - (2 * (cy2.R + cy2.G + cy2.B));
double root = Math.Sqrt(lx * lx / 4 + ly * ly / 4);
if (root > 15)
bitmap.SetPixel(i, j, Colors.Black);
bitmap.SetPixel(i, j, Colors.White);
return bitmap;
  • Finally we initiate the edge detection process with a Button clicked event and display the output in an Image control.
private void Button_Click_2(object sender, RoutedEventArgs e)
WriteableBitmap writeableBmp = new WriteableBitmap(bmp);
WriteableBitmap newWBmp = EdgeDetection(writeableBmp);
imageProcessed.Source = newWBmp;


Based on the feedback and comments we have updated the article using Nokia Imaging SDK. In the latest source code we have used CustomEffectBase to detect the edge of the image. The DoubleEffect.cs class extended from CustomEffectBase class is responsible to detect the edges and return the final image.

 public class DoubleEffect : CustomEffectBase
public DoubleEffect(IImageProvider source)
: base(source)
protected override void OnProcess(PixelRegion sourcePixelRegion, PixelRegion targetPixelRegion)
sourcePixelRegion.ForEachRow((index, width, pos) =>
for (int x = 0; x < width; ++x, ++index)
Color a = ToColor(sourcePixelRegion.ImagePixels[index]);
Color b = ToColor(sourcePixelRegion.ImagePixels[index + 1]);
Color black = Color.FromArgb(1, 1, 1, 1);
Color white = Color.FromArgb(255,255,255,255);
bool color = CompareColors(a, b);
if (color)
targetPixelRegion.ImagePixels[index] = FromColor(white);
targetPixelRegion.ImagePixels[index] = FromColor(black);
catch (Exception e)
Deployment.Current.Dispatcher.BeginInvoke(() =>
public static bool CompareColors(Color a, Color b)
if (a.R.Equals(b.R) || a.G.Equals(b.G) || a.B.Equals(b.B))
//no edge
return true;
if (Math.Abs(a.R - b.R) > 9 || Math.Abs(a.G - b.G) > 9 || Math.Abs(a.B - b.B) > 9)
return false;
return true;

Finally we select an image from picture hub and call the DoubleEffect() constructor to apply edge detection in the image.

                e.ChosenPhoto.Position = 0;
var imageStream = new StreamImageSource(e.ChosenPhoto);
CustomEffectBase referenceEffect = new DoubleEffect(imageStream);
var renderer = new WriteableBitmapRenderer(referenceEffect, ImageBitmap);
ImageBitmap = await renderer.RenderAsync();
imageProcessed.Source = ImageBitmap;

Source Code

77 page views in the last 30 days.