Robets 算子是一种微分算子
设输入图像为f(i,j) 算子输出图像为 g(i,j)
则Roberts算子表示为差分形式:
g(x) = f(i,j)-f(i+1,j)
g(y) = f(i+1,j) - f(i,j+1)
g(i,j) = abs(gx)+ abs(gy)
这是一个Winform的代码
private void button1_Click(object sender, EventArgs e)
{
Color c1 = new Color();
Color c2 = new Color();
Color c3 = new Color();
Color c4 = new Color();
int rr, gg, bb, r1, r2, r3, r4, fxr, fyr, i, j;
int g1, g2, g3, g4, fxg, fyg, b1, b2, b3, b4, fxb, fyb;
Bitmap box1 = new Bitmap(pictureBox1.Image);
Bitmap box2 = new Bitmap(pictureBox2.Image);
for (i = 0; i < pictureBox1.Image.Width - 2; i++)
{
for (j = 0; j < pictureBox1.Image.Height - 2; j++)
{
c1 = box1.GetPixel(i, j);
c2 = box1.GetPixel(i + 1, j + 1);
c3 = box1.GetPixel(i + 1, j);
c4 = box1.GetPixel(i, j + 1);
r1 = c1.R;
r2 = c2.R;
r3 = c3.R;
r4 = c4.R;
fxr = r1 - r2;
fyr = r3 - r4;
rr = Math.Abs(fxr) + Math.Abs(fyr) + 128;
if (rr < 0)
rr = 0;
if (rr > 255)
rr = 255;
g1 = c1.G;
g2 = c2.G;
g3 = c3.G;
g4 = c4.G;
fxg = g1 - g2;
fyg = g3 - g4;
gg = Math.Abs(fxg) + Math.Abs(fyg) + 128;
if (gg < 0)
gg = 0;
if (gg > 255)
gg = 255;
b1 = c1.B;
b2 = c2.B;
b3 = c3.B;
b4 = c4.B;
fxb = b1 - b2;
fyb = b3 - b4;
bb = Math.Abs(fxb) + Math.Abs(fyb);
if (bb < 0)
bb = 0;
if (bb > 255)
bb = 255;
Color cc = Color.FromArgb(rr, gg, bb);
box2.SetPixel(i, j, cc);
}
pictureBox2.Refresh();
pictureBox2.Image = box2;
}