class Prewitt
{
public Bitmap apply(Bitmap b)
{
int[,] gx = new int[,] { { 1, 1, 1 }, { 0, 0, 0 }, { -1, -1, -1 } }; // The matrix Gx
int[,] gy = new int[,] { { -1, 0, 1 }, { -1, 0, 1 }, { -1, 0, 1 } }; // The matrix Gy
Bitmap b1 = new Bitmap(b.Width,b.Height);
for (int i = 1; i < b.Height – 1; i++) // loop for the image pixels height
{
for (int j = 1; j < b.Width – 1; j++) // loop for image pixels width
{
float new_x = 0, new_y = 0;
float c;
for (int hw = -1; hw < 2; hw++) //loop for cov matrix
{
for (int wi = -1; wi < 2; wi++)
{
c = (b.GetPixel(j + wi, i + hw).B + b.GetPixel(j + wi, i + hw).R + b.GetPixel(j + wi, i + hw).G) / 3;

new_x += gx[hw + 1, wi + 1] * c;
new_y += gy[hw + 1, wi + 1] * c;
}
}
if (new_x * new_x + new_y * new_y > 128 * 100)
b1.SetPixel(j, i, Color.Black);
else
b1.SetPixel(j, i, Color.White);
}
}

return b1;
}
}

Advertisements