1: /// <summary>
2: /// Does basic edge detection on an image
3: /// </summary>
4: /// <param name="OriginalImage">Image to do edge detection on</param>
5: /// <param name="Threshold">Decides what is considered an edge</param>
6: /// <param name="EdgeColor">Color of the edge</param>
7: /// <returns>A bitmap which has the edges drawn on it</returns>
8: public static Bitmap EdgeDetection(Bitmap OriginalImage, float Threshold, Color EdgeColor)
9: {
10: Bitmap NewBitmap = new Bitmap(OriginalImage, OriginalImage.Width, OriginalImage.Height);
11: BitmapData NewData = Image.LockImage(NewBitmap);
12: BitmapData OldData = Image.LockImage(OriginalImage);
13: int NewPixelSize = Image.GetPixelSize(NewData);
14: int OldPixelSize = Image.GetPixelSize(OldData);
15: for (int x = 0; x < NewBitmap.Width; ++x)
16: {
17: for (int y = 0; y < NewBitmap.Height; ++y)
18: {
19: Color CurrentColor = Image.GetPixel(OldData, x, y, OldPixelSize);
20: if (y < NewBitmap.Height - 1 && x < NewBitmap.Width - 1)
21: {
22: Color TempColor = Image.GetPixel(OldData, x + 1, y + 1, OldPixelSize);
23: if (Distance(CurrentColor.R, TempColor.R, CurrentColor.G, TempColor.G, CurrentColor.B, TempColor.B) > Threshold)
24: {
25: Image.SetPixel(NewData, x, y, EdgeColor, NewPixelSize);
26: }
27: }
28: else if (y < NewBitmap.Height - 1)
29: {
30: Color TempColor = Image.GetPixel(OldData, x, y + 1, OldPixelSize);
31: if (Distance(CurrentColor.R, TempColor.R, CurrentColor.G, TempColor.G, CurrentColor.B, TempColor.B) > Threshold)
32: {
33: Image.SetPixel(NewData, x, y, EdgeColor, NewPixelSize);
34: }
35: }
36: else if (x < NewBitmap.Width - 1)
37: {
38: Color TempColor = Image.GetPixel(OldData, x + 1, y, OldPixelSize);
39: if (Distance(CurrentColor.R, TempColor.R, CurrentColor.G, TempColor.G, CurrentColor.B, TempColor.B) > Threshold)
40: {
41: Image.SetPixel(NewData, x, y, EdgeColor, NewPixelSize);
42: }
43: }
44: }
45: }
46: Image.UnlockImage(NewBitmap, NewData);
47: Image.UnlockImage(OriginalImage, OldData);
48: return NewBitmap;
49: }