Postup je - preved obrazok do ciernobielej (metoda vracia dvojrozmerne pole, naplnene hodnotami jasu ciernobieleho obrazku)
Spoiler
Kód: Vybrať všetko
public static byte[,] GreyScaleImageData(Bitmap original)
{
unsafe
{
byte[,] greyScaleImageData = new byte[original.Width, original.Height];
//lock the original bitmap in memory
BitmapData originalData = original.LockBits(
new Rectangle(0, 0, original.Width, original.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
for (int y = 0; y < original.Height; y++)
{
//get the data from the original image
byte* oRow = (byte*)originalData.Scan0 + (y * originalData.Stride);
for (int x = 0; x < original.Width; x++)
{
//create the grayscale version
byte blue = (byte)(*oRow * 0.299); oRow++;
byte green = (byte)(*oRow * 0.587); oRow++;
byte red = (byte)(*oRow * 0.114); oRow++;
byte greyScale = (byte)(red + green + blue );
greyScaleImageData[x, y] = greyScale;
}
}
//unlock the bitmaps
original.UnlockBits(originalData);
return greyScaleImageData;
}
}Spoiler
Kód: Vybrať všetko
public static byte[,] CreateSobel(byte[,] picArray, int direction)
{
int[] maska = new int[9];
if (direction == 0) //X
{
maska[0] = -1;
maska[1] = 0;
maska[2] = 1;
maska[3] = -2;
maska[4] = 0;
maska[5] = 2;
maska[6] = -1;
maska[7] = 0;
maska[8] = 1;
} // if
else if (direction == 1) //Y
{
maska[0] = -1;
maska[1] = -2;
maska[2] = -1;
maska[3] = 0;
maska[4] = 0;
maska[5] = 0;
maska[6] = 1;
maska[7] = 2;
maska[8] = 1;
} // else if
else //Rotace 45
{
maska[0] = 0;
maska[1] = -1;
maska[2] = -2;
maska[3] = 1;
maska[4] = 0;
maska[5] = -1;
maska[6] = 2;
maska[7] = 1;
maska[8] = 0;
} // else
//Vytvářené pole obrazových dat
byte[,] resultArray = new byte[picArray.GetLength(0), picArray.GetLength(1)];
//Pole gradientu
int[,] poleGradient = new int[picArray.GetLength(0), picArray.GetLength(1)];
byte[] poleHodnot = new byte[9];
int koef = 0;
int soucetGradientu = 0;
for (int y = 1; y < picArray.GetLength(1) - 1; y++) // řádky
{
for (int x = 1; x < picArray.GetLength(0) - 1; x++) // sloupce
{
poleHodnot[0] = picArray[x - 1, y - 1];
poleHodnot[1] = picArray[x - 1, y];
poleHodnot[2] = picArray[x - 1, y + 1];
poleHodnot[3] = picArray[x, y - 1];
poleHodnot[4] = picArray[x, y];
poleHodnot[5] = picArray[x, y + 1];
poleHodnot[6] = picArray[x + 1, y - 1];
poleHodnot[7] = picArray[x + 1, y];
poleHodnot[8] = picArray[x + 1, y + 1];
koef = poleHodnot[0] * maska[0] +
poleHodnot[1] * maska[1] +
poleHodnot[2] * maska[2] +
poleHodnot[3] * maska[3] +
poleHodnot[4] * maska[4] +
poleHodnot[5] * maska[5] +
poleHodnot[6] * maska[6] +
poleHodnot[7] * maska[7] +
poleHodnot[8] * maska[8];
soucetGradientu += koef;
poleGradient[x, y] = koef;
} // for
} // for
//Vypocet prahu na zaklade spoctenych koeficientu
int prah = soucetGradientu / picArray.Length;
int gradient = 0;
for (int y = 0; y < picArray.GetLength(1) - 1; y++) // řádky
{
for (int x = 0; x < picArray.GetLength(0) - 1; x++) // sloupce
{
gradient = poleGradient[x, y];
if (gradient >= prah)
resultArray[x, y] = 0; //cerna neptari hrane
else
resultArray[x, y] = 255;
} // for
} // for
return resultArray;
} // CreateSobeledit://
zmenil som kod tak aby kombinoval sobelov operator v smere X aj v smere Y a zacalo to plut hrany podla toho co som cakal