Witam
Przechwytuje obraz za pomocą kamery i biblioteki Aforge. Wszytko jest ok dopóki nie użyje jakiegoś filtru na przechwyconym obrazie. Kod:
Przechwytywanie:
private void video_NewFrame(object sender, NewFrameEventArgs args)
{
Bitmap obraz = (Bitmap)args.Frame.Clone();
ekran.Image = obraz;
}
ekran to picturebox na którym się wyświetla obraz.
Po dodaniu filtru:
public static Bitmap SNN(Bitmap zrodlo, int rozmiar)
{
Bitmap temp = zrodlo;
Bitmap nowa = new Bitmap(temp.Width, temp.Height);
Graphics grafika = Graphics.FromImage(nowa);
grafika.DrawImage(temp, new Rectangle(0, 0, temp.Width, temp.Height), new Rectangle(0, 0, temp.Width, temp.Height), GraphicsUnit.Pixel);
grafika.Dispose();
int ApetureMinX = -(rozmiar / 2);
int ApetureMaxX = (rozmiar / 2);
int ApetureMinY = -(rozmiar / 2);
int ApetureMaxY = (rozmiar / 2);
for (int i = 0; i < nowa.Width; ++i)
{
for (int j = 0; j < nowa.Height; ++j)
{
int R = 0;
int G = 0;
int B = 0;
int liczPix = 0;
for (int i2 = ApetureMinX; i2 < ApetureMaxX; ++i2)
{
int TempX1 = i + i2;
int TempX2 = i - i2;
if(TempX1 >= 0 && TempX1 < nowa.Width && TempX2 >= 0 && TempX2 <nowa.Width)
{
for (int j2 = ApetureMinY; j2 < ApetureMaxY; ++j2)
{
int TempY1 = j + j2;
int TempY2 = j - j2;
if(TempY1 >= 0 && TempY1 < nowa.Height && TempY2 >= 0 && TempY2 < nowa.Height)
{
Color tempKolor = nowa.GetPixel(i, j);
Color tempKolor2 = nowa.GetPixel(TempX1, TempY1);
Color tempKolor3 = nowa.GetPixel(TempX2, TempY2);
if(Distance(tempKolor.R,tempKolor2.R,tempKolor.G,tempKolor2.G,tempKolor.B,tempKolor2.B) < Distance(tempKolor.R,tempKolor3.R,tempKolor.G,tempKolor3.G,tempKolor.B,tempKolor3.B))
{
R += tempKolor2.R;
G += tempKolor2.G;
B += tempKolor2.B;
}
else
{
R += tempKolor3.R;
G += tempKolor3.G;
B += tempKolor3.B;
}
++liczPix;
}
}
}
}
Color nowyPixel = Color.FromArgb(R / liczPix, G / liczPix, B / liczPix);
nowa.SetPixel(i, j, nowyPixel);
}
}
return nowa;
}
i dodaniu tego do new_frame np tak:
private void video_NewFrame(object sender, NewFrameEventArgs args)
{
Bitmap obraz = (Bitmap)args.Frame.Clone();
Bitmap tmp = Obraz.SNN(obraz, 8);
ekran.Image = obraz;
}
w picturebox nie ma nic. W ogóle nawet nie dochodzi do return w funkcji SNN a wszystkie warunki w pętli są ok. Może wiek ktoś co tu jest nie tak ?