-
Notifications
You must be signed in to change notification settings - Fork 0
/
EdgeDetector.cs
71 lines (59 loc) · 2.04 KB
/
EdgeDetector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace SeamCarving
{
class EdgeDetector : IImageMod
{
public enum EdgeDetectorType { Laplacian };
private EdgeDetectorType edge;
public RawImage Modify(RawImage rim)
{
RawImage tmp = new RawImage(rim.Width, rim.Height);
Edge(ref tmp,rim);
return tmp;
}
private int width, height;
private int MirrorX(int x)
{
if (x < 0)
return Math.Abs(x);
else if (x >= width)
return width - (x - width) - 1;
return x;
}
private int MirrorY(int y)
{
if (y < 0)
return Math.Abs(y);
else if (y >= height)
return height - (y - height) - 1;
return y;
}
public void ModifyInPlace(ref RawImage rim)
{
rim = Modify(rim);
}
private void Edge(ref RawImage rim, RawImage org)
{
width = rim.Width;
height = rim.Height;
int val;
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
val = org[MirrorX(x - 1), y, RawImage.Channel.Red] + org[MirrorX(x + 1), y, RawImage.Channel.Red]
+ org[MirrorX(x - 1), MirrorY(y - 1), RawImage.Channel.Red] + org[MirrorX(x + 1), MirrorY(y - 1), RawImage.Channel.Red]
+ org[MirrorX(x - 1), MirrorY(y + 1), RawImage.Channel.Red] + org[MirrorX(x + 1), MirrorY(y + 1), RawImage.Channel.Red]
+ org[x, MirrorY(y - 1), RawImage.Channel.Red] + org[x, MirrorY(y - 1), RawImage.Channel.Red];
val -= 8 * org[x, y, RawImage.Channel.Red];
val = Utils.Clamp<int>(val, 0, 255);
rim[x, y] = Color.FromArgb(val, val, val);
}
}
}
}
}