public Vector SobelY(Vector L) { if (L.Dim != 3) throw new Exception("sobel dim error"); Vector Ly = new Vector(3); Ly[2] = L[0] + 2 * L[1] + L[2]; Ly[1] = 0; Ly[0] = -Ly[2]; return Ly; }
public Vector SobelX(Vector L) { if (L.Dim != 3) throw new Exception("sobel dim error"); Vector Lx = new Vector(3); Lx[0] = L[2] - L[0]; Lx[1] = 2 * Lx[0]; Lx[2] = Lx[0]; return Lx; }
public static Vector operator -(Vector one, Vector other) { if (one.Dim != other.Dim) throw new Exception("Vector lengths aren't the same"); Vector newV = new Vector(one); for (int i = 0; i < other.Dim; i++) newV[i] -= other[i]; return newV; }
public static Vector operator /(Vector one, double param) { Vector newV = new Vector(one); for (int i = 0; i < one.Dim; i++) newV[i] /= param; return newV; }
public void SetRow(int position, Vector row) { if (Width != row.Dim) throw new Exception("Dim Error"); for (int i = 0; i < Width; i++) field[i, position] = row[i]; }
public Vector(Vector other) { field = new double[other.Dim]; for (int i = 0; i < other.Dim; i++) field[i] = other[i]; }
public void SetColumn(int position, Vector column) { if (Height != column.Dim) throw new Exception("Dim Error"); for (int i = 0; i < Height; i++) field[position, i] = column[i]; }
public Vector Row(int position) { Vector newV = new Vector(Width); for (int i = 0; i < Width; i++) newV[i] = field[i, position]; return newV; }
public Matrix GaussianElimination() { //if (Height != Width) // throw new Exception(); Matrix newM = new Matrix(this); int position = -1; double param = 0; int count = 0; Vector positionV = new Vector(Width); for (int i = 0; i < Width; i++) { if (position != -1) { newM.SwitchRow(position, count); count++; position = -1; } for (int j = count; j < Height; j++) { if (newM.field[i, j] != 0) { if (position != -1) newM.SetRow(j, newM.Row(j) - positionV * newM[i, j] / param); else { position = j; param = newM[i, j]; newM.SetRow(j, newM.Row(j)); positionV = newM.Row(j); } } } } return newM; }
public Vector Column(int position) { Vector newV = new Vector(Height); for (int i = 0; i < Height; i++) newV[i] = field[position, i]; return newV; }
public static Vector operator *(Matrix one, Vector other) { if (one.Width != other.Dim) throw new Exception("Dim Error"); Vector newV = new Vector(one.Height); for (int i = 0; i < newV.Dim; i++) newV[i] = other * one.Row(i); return newV; }
public WriteableBitmap EdgeDetection(WriteableBitmap bmap, double bound) { int height = bmap.PixelHeight - 1; int width = bmap.PixelWidth - 1; Matrix thetaM = new Matrix(width, height); Matrix greyM = new Matrix(width + 1, height + 1); for (int i = 0; i < greyM.Width; i++) for (int j = 0; j < greyM.Height; j++) greyM[i, j] = Greyscale(bmap.GetPixel(i, j).R, bmap.GetPixel(i, j).G, bmap.GetPixel(i, j).B); for (int i = 1; i < width; i++) for (int j = 1; j < height; j++) { Vector lx = new Vector(3); Vector ly = new Vector(3); lx[0] = greyM[i - 1, j]; lx[1] = greyM[i, j]; lx[2] = greyM[i + 1, j]; ly[0] = greyM[i, j - 1]; ly[1] = greyM[i, j]; ly[2] = greyM[i, j + 1]; Vector slx = SobelX(lx); Vector sly = SobelY(ly); double gx = Math.Pow(slx * slx, 0.5); double gy = Math.Pow(sly * sly, 0.5); thetaM[i - 1, j - 1] = Math.Atan2(gy, gx); } WriteableBitmap plot = BitmapFactory.New(width, height); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) { if (thetaM[i, j] > bound) plot.SetPixel(i, j, Colors.White); else plot.SetPixel(i, j, Colors.Black); } return plot; }