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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
Bitmap myBitmap = new Bitmap(pictureBox1.Image);
int width = myBitmap.Width;
int height = myBitmap.Height;
unsafe
{
BitmapData bmpData = myBitmap.LockBits(new Rectangle(0, 0, width,
height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte* newPixel = (byte*)(void*)bmpData.Scan0;
//syntaxe equivalente
//byte* pixel = (byte*)bmpData.Scan0.ToPointer();
//creation de la matrice de NDG.
byte[,] image = new byte[width, height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
image[x, y] = newPixel[0];
newPixel += 4;
}
myBitmap.UnlockBits(bmpData);
// une cpoier de la matrice de ND.
byte[,]image1 =new byte[width,height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
image1[x, y] = image[x,y];
}
int npix;
// calcule de filtre de soble Gx
/* _______
* -1|0|1
* -------
* -2|0|2
* -------
* -1|0|1
* _______
*/
for (int j = 1; j < height-1; j++)
for (int i = 1; i < width-1; i++)
{
npix = image[i - 1, j - 1] * (-1) + image[i + 1, j - 1] + image[i - 1, j] * (-2) +
image[i + 1, j] * (2) + image[i - 1, j + 1] * (-1) + image[i + 1, j + 1];
if (npix > 255) npix = 255;
if (npix < 0) npix = 0;
image[i, j] = (byte)npix;
}
// calcule de filtre de soble Gy
/* ________
* 1|2|1
* --------
* 0|0|0
* --------
* -1|-2|-1
* ________
*/
for (int j = 1; j < height - 1; j++)
for (int i = 1; i < width - 1; i++)
{
npix =image1[i - 1, j - 1]+ image1[i, j - 1]*(2)+image1[i + 1, j - 1] + image1[i - 1, j+1]*(-1)
+image1[i,j+1] * (-2) + image1[i +1, j + 1] * (-1);
if (npix > 255) npix = 255;
if (npix < 0) npix = 0;
image1[i, j] = (byte)npix;
}
// Construction de l'image filtrer
BitmapData bmp= myBitmap.LockBits(new Rectangle(0, 0, width,
height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte* Pixel = (byte*)(void*)bmp.Scan0;
int x1;
byte x2;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
x1 = (int)Math.Sqrt((image1[x, y] *image1[x, y]) + (image[x, y]*image[x, y]));
if (x1 > 255) x1 = 255;
if (x1 < 0 ) x1 = 0;
x2 = (byte)x1;
Pixel[0] = x2;
Pixel[1] = x2;
Pixel[2] = x2;
Pixel += 4;
}
myBitmap.UnlockBits(bmp);
}
pictureBox2.Image = myBitmap; |
Partager