Harris Corner Detection Highlighting all edges not corners - c#

I need to implement Harris corner detection, which should only have positive R values. If I use threshold of R>0, it highlights nothing, if R < 0 however, from even -0.00001 to -1000000 it outputs perfect edge detection, but no corners.
Input and output:
http://imgur.com/a/TtQSD\
Main Code:
Bitmap b = (Bitmap)pictureBox1.Image;
var i = IntensityMat(b);
var n = Mask(i,-0.00000001);
var ty = ToBit(n);
pictureBox1.Image = ty;
Functions:
public double Intensity(Color c)
{
int x = c.R;
int y = c.G;
int z = c.B;
return Math.Sqrt(x * x + y * y + z * z);
}
public double Trace(double[,] m)
{
double sum = 0;
for (int i = 0; i < m.GetLength(0); i++)
{
sum += m[i, i];
}
return sum;
}
public double[,] M(double ix,double iy)
{
double[,] m = new double[2, 2];
m[0, 0] = ix*ix;
m[1, 1] = iy*iy;
m[0, 1] = ix * iy;
m[1, 0] = ix * iy;
return m;
}
public double Det(double[,] m)
{
return m[1, 1] * m[0, 0] - m[0, 1] * m[1, 0];
}
public double R(double[,] m,double k=0.04)
{
var t = Trace(m);
return Det(m) - k * t * t;
}
int[,] IntensityMat(Bitmap b)
{
int[,] n = new int[b.Width, b.Height];
for (int i = 0; i < b.Width; i++)
{
for (int j = 0; j < b.Height; j++)
{
Color c = b.GetPixel(i, j);
n[i, j] = (int)Intensity(c);
}
}
return n;
}
Bitmap ToBit(int[,] bd)
{
Bitmap b = new Bitmap(bd.GetLength(0), bd.GetLength(1));
for (int i = 0; i < b.Width; i++)
{
for (int j = 0; j < b.Height; j++)
{
var t = bd[i, j];
b.SetPixel(i, j, Color.FromArgb(t, t, t));
}
}
return b;
}
int[,] Mask(int[,] m,double thresh)// m matrix of I
{
int[,] n = new int[m.GetLength(0), m.GetLength(1)];
for (int i = 1; i < m.GetLength(0); i++)
{
for (int j = 1; j < m.GetLength(1); j++)
{
double ix = Math.Abs(m[i-1,j]-m[i,j]);
double iy = Math.Abs(m[i , j-1] - m[i, j]);
var lap = M(ix, iy);
var r = R(lap);
if (r > thresh)
{
n[i, j] = 255;
}
}
}
return n;
}

Related

mix two two-dimensional arrays into 1 two-dimensional array

i am trying the results which stored int previous arrays (1 and 2) in a new array 3 can show each result from array 1 with with each array 2, when i show the results from each one separately they are okay but when i try to mix them in array 3 i get the correct answers for the first value in array 1 with all values in array 2 then a lot of zeros appears.
here is the code for the array 3
for (int i = 0; i < result1.GetLength(0); i++)
{
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[i, j] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[i, j]);
counter++;
}
}
}
}
and here is the whole code
double[,] Cranelocations = { { -12.3256, 0.5344 }, { -12.3256, -0.4656 }, { -12.3256, -1.4656 }, { -12.3256, -2.4656 } };
double[,] Picklocation = { { -0.3256, -3.4656 }, { 0.6744, -3.4656 }, { 1.6744, -3.4656 }, { 2.6744, -3.4656 }, { 3.6744, -3.4656 }, { 4.6744, -3.4656 }, { 5.6744, -3.4656 } };
double[,] Setlocation = { { 20.62, 5.03 }, { 24.28, 5.03 }, { 28.40, 5.03 }, { 32.11, 5.03 }, { 35.99, 5.26 }, { 40.18, 5.26 } };
double[] Weights = { 11.7865, 14.7335, 15.1015, 10.7465 };
double[,] result1 = new double[Weights.Length * Cranelocations.GetLength(0), Picklocation.GetLength(0)];
double[,] result2 = new double[Weights.Length * Cranelocations.GetLength(0), Setlocation.GetLength(0)];
object[,] result3 = new object[result1.GetLength(0) * result1.GetLength(1), result2.GetLength(0) * result2.GetLength(1)];
int counter = 0;
for (int m = 0; m < Weights.Length; m++)
{
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Picklocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Picklocation[j, 0];
double y = Cranelocations[i, 1] - Picklocation[j, 1];
result1[i, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
Console.WriteLine("-----------------------------------------------------------------");
for (int m = 0; m < Weights.Length; m++)
{
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Setlocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Setlocation[j, 0];
double y = Cranelocations[i, 1] - Setlocation[j, 1];
result2[i, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
for (int i = 0; i < result1.GetLength(0); i++)
{
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[i, j] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[i, j]);
counter++;
}
}
}
}
}
For each m you reset i to 0 and start writing at the beginning of your array again.
You need to keep moving the index forward when you increment m.
The same is when you are combining the two indexes. When you are combining two iterations to get an index you generally multiply the first by the length of the second, then add them together.
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Picklocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Picklocation[j, 0];
double y = Cranelocations[i, 1] - Picklocation[j, 1];
result1[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
Console.WriteLine("-----------------------------------------------------------------");
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Setlocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Setlocation[j, 0];
double y = Cranelocations[i, 1] - Setlocation[j, 1];
result2[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
for (int i = 0; i < result1.GetLength(0); i++)
{
int iOffset = i * result1.GetLength(1);
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
int kOffset = k * result2.GetLength(1);
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[iOffset + j, kOffset + m] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[iOffset + j, kOffset + m]);
counter++;
}
}
}
}

fit a line to 3D data with weighted regression

I am trying to calculate a 3D linear regression LINE using the Singular Value Decomposition method (SVD).
This works fine. Now, I'd like to generalise the method for weighted regression.
how to calculate the optimale beta with SVD? Below is my C# code which uses the CSML package. The LinearRegression() function works just fine. WeightedLinearRegression() does not and I assume this is because I cannot just simply define
weighted_mult_mat = M_tr * W * M;
CODE:
public static Vector3D WeightedLinearRegression(List<Point3D> pts, List<double> weights) {
// normalisation
double sum = weights.Sum();
if (sum != weights.Count) {
for (int i = 0; i < weights.Count; i++ ) {
weights[i] = weights[i] / sum;
}
}
Point3D avg = pts.average();
// populate Matrix M
CSML.Matrix M = new CSML.Matrix(pts.Count, 3); // init
// populate matrix M
for (int i = 1; i < pts.Count + 1; i++) {
M[i, 1] = new Complex(pts[i - 1].X - avg.X);
M[i, 2] = new Complex(pts[i - 1].Y - avg.Y);
M[i, 3] = new Complex(pts[i - 1].Z - avg.Z);
}
CSML.Matrix M_tr = M.Transpose();
// populate weights matrix
CSML.Matrix W = new CSML.Matrix(pts.Count, pts.Count); // init
for (int i = 1; i < pts.Count + 1; i++) {
W[i, i] = new Complex(weights[i-1]);
}
// compute matrix
CSML.Matrix weighted_mult_mat = new CSML.Matrix();
weighted_mult_mat = M_tr * W;
weighted_mult_mat = weighted_mult_mat * M;
var weighted_dense_mat = new DenseMatrix(3, 3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
weighted_dense_mat[i, j] = weighted_mult_mat[i + 1, j + 1].Re;
}
}
var weighted_svd = weighted_dense_mat.Svd(true);
var weighted_vt = weighted_svd.VT;
Vector3D weighted_dirVect = new Vector3D(weighted_vt[0, 0], weighted_vt[0, 1], weighted_vt[0, 2]);
weighted_dirVect.Normalize();
return weighted_dirVect;
}
public static Vector3D LinearRegression(List<Point3D> pts) {
Point3D avg = pts.average();
// populate Matrix M
CSML.Matrix M = new CSML.Matrix(pts.Count, 3); // init
// populate matrix M
for (int i = 1; i < pts.Count + 1; i++) {
M[i, 1] = new Complex(pts[i - 1].X - avg.X);
M[i, 2] = new Complex(pts[i - 1].Y - avg.Y);
M[i, 3] = new Complex(pts[i - 1].Z - avg.Z);
}
CSML.Matrix M_tr = M.Transpose();
CSML.Matrix mult_mat = new CSML.Matrix();
mult_mat = M_tr * M;
var dense_mat = new DenseMatrix(3, 3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
dense_mat[i, j] = mult_mat[i + 1, j + 1].Re ;
}
}
var svd = dense_mat.Svd(true);
var vt = svd.VT;
Vector3D dirVect = new Vector3D(vt[0, 0], vt[0, 1], vt[0, 2]);
dirVect.Normalize();
return dirVect;
}

index outside bounds of the array adaptive median

I'm trying to output a new image after applying the adaptive median filter but it only works if the maximum window size is 3*3, but it should work for all odd window sizes, and it does so if the image is so small for example 10*10 pixels, but if the image is for example 440*445 pixels it only works if the max window size is 3*3 otherwise it says index outside bounds of the array, here is my code using counting sort to sort the pixels:
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace ImageFilters
{
public class Class2
{
public byte[,] median(byte[,] array, int width, int height, int msize)
{
byte[,] marr = new byte[height + 1, width + 1];
int size = 3;
for (int i = 0; i <height+1; i++)
{
marr[i, 0] = 255;
}
for (int j = 0; j < width+1; j++)
{
marr[0, j] = 255;
}
int n=0;
int z=0;
for (int k = 0; k < (height) * width; k++)
{
repeat:
byte[] farr = new byte[size * size];
int I=0;
for (int i = n; i < n+size; i++)
{
for (int j = z; j < z+size; j++)
{
if (j < width && i < height)
{
farr[I] = array[i, j];
I++;
}
else
{
farr[I] = 0;
I++;
}
}
}
int zxy = farr[(size*size)/2];
byte[] check = new byte[size * size];
check=counting(farr,size);
int median = 0;
int lennn;
lennn = check.Length;
int min = 99999;
int maxi = -1;
int a1, a2;
min = check.Min();
maxi = check.Max();
median = check[lennn / 2];
a1 = median - min;
a2 = maxi - median;
int b1, b2;
b1 = zxy - min;
b2 = maxi - zxy;
if (a1 > 0 && a2 > 0)
{
if (b1 > 0 && b2 > 0)
{
marr[n + 1, z + 1] = Convert.ToByte(zxy);
z++;
if (z + (size - 1) > (width + 1))
{
n++;
z = 0;
}
}
else
{
marr[n + 1, z + 1] = Convert.ToByte(median);
z++;
if (z + (size - 1) > (width + 1))
{
n++;
z = 0;
}
}
}
else
{
size = size + 2;
if (size <= msize)
goto repeat;
else
{
marr[n +1, z +1] = Convert.ToByte(median);
z++;
if (size > 3)
size = 3;
if (z + (size - 1) > (width + 1))
{
n++;
z = 0;
}
}
}
}
return marr;
}
public static byte[] counting(byte[] array1D, int siz)
{
int max = -10000000;
byte[] SortedArray = new byte[siz * siz];
max = array1D.Max();
byte[] Array2 = new byte[max + 1];
//for (int i = 0; i < Array2.Length; i++)
// Array2[i] = 0; // create new array ( Array2) with zeros in every index .
for (int i = 0; i < (siz*siz); i++)
{
Array2[array1D[i]] += 1; //take the element in the index(i) of(array1d) AS the index of (Array2)
// and increment the element in this index by 1 .
}
for (int i = 1; i <= max; i++)
{
Array2[i] += Array2[i - 1]; // Count every element in (array1d) and put it in (Array2).
}
for (int i = (siz*siz) - 1; i >= 0; i--)
{
SortedArray[Array2[array1D[i]] - 1] = array1D[i]; // transfer the element in index (i) of (array1d) to (SortedArray
Array2[array1D[i]]--;
}
return SortedArray;
}
}
}
Any help will be appreciated, thank you.

Unstable calculation error

I need to calculate matrix: ( X^(T) * X )^(-1).
Legend for the code&comments:
x is double[,] array;
xT - transposed matrix
^(-1) - inverted matrix
Every time i generate new random matrix to work with it and i found out that program is very unstable, because it isn't working properly with any input data. I'm sure about that because i need to get Identity matrix in the end if everything's fine, but sometimes i get a totally terrible Ineverted matrix so i don't get an Identity matrix. I'm dissappointes because i always use the same type of data and do not convert anything. Compiler is MVS 2010. Hope You will help me.
Here is my Program.cs:
static void Main(string[] args)
{
Matrix x = new Matrix(5, 4);
//Matrix temp = new Matrix(x.Row, x.Col);
//double[] y = new double[x.Row];
//double[] b = new double[x.Row];
//this data isn't calculated correctly. used for debugging
x.MatrixX[0, 0] = 7; x.MatrixX[0, 1] = 6; x.MatrixX[0, 2] = 5; x.MatrixX[0, 3] = 8;
x.MatrixX[1, 0] = 7; x.MatrixX[1, 1] = 5; x.MatrixX[1, 2] = 8; x.MatrixX[1, 3] = 5;
x.MatrixX[2, 0] = 6; x.MatrixX[2, 1] = 8; x.MatrixX[2, 2] = 6; x.MatrixX[2, 3] = 8;
x.MatrixX[3, 0] = 8; x.MatrixX[3, 1] = 5; x.MatrixX[3, 2] = 8; x.MatrixX[3, 3] = 7;
x.MatrixX[4, 0] = 8; x.MatrixX[4, 1] = 5; x.MatrixX[4, 2] = 6; x.MatrixX[4, 3] = 7;
/*
7,00000 6,00000 5,00000 8,00000
7,00000 5,00000 8,00000 5,00000
6,00000 8,00000 6,00000 8,00000
8,00000 5,00000 8,00000 7,00000
8,00000 5,00000 6,00000 7,00000
*/
//random matrix generation
/*
Random rnd = new Random();
for (int i = 0; i < x.Row; i++)
for (int j = 0; j < x.Col; j++)
x.MatrixX[i, j] = rnd.Next(5, 10);
*/
/*i'm going to calculate: ( X^(T) * X )^(-1)
* 1. transpose X
* 2. multiply X and (1)
* 3. invert matrix (2)
* +4. i wanna check the results: Multilate of (2) and (3) = Identity_matrix.
* */
Matrix.Display(x);
//1
Matrix xt = Matrix.Transpose(x);
Matrix.Display(xt);
//2
Matrix xxt = Matrix.Multiply(x, xt);
Matrix.Display(xxt);
//3
Matrix xxtinv = Matrix.Invert(Matrix.Multiply(x, xt));
Matrix.Display(xxtinv);
//4
Console.WriteLine("Invert(xxt) * xxt. IdentityMatrix:");
Matrix IdentityMatrix = Matrix.Multiply(xxtinv, xxt);
Matrix.Display(IdentityMatrix);
Console.ReadKey();
}
And here is Matrix.cs with all functions:
public class Matrix
{
private double[,] matrix;
private int row;
private int col;
#region constructors
public Matrix(int Row, int Col)
{
this.row = Row;
this.col = Col;
matrix = new double[Row, Col];
}
public Matrix()
{
Random rnd = new Random();
Row = rnd.Next(3, 7);
Col = rnd.Next(3, 7);
matrix = new double[Row, Col];
for (int i = 0; i < Row; i++)
for (int j = 0; j < Col; j++)
matrix[i, j] = rnd.Next(5, 10);
}
public Matrix(Matrix a)
{
this.Col = a.Col;
this.Row = a.Row;
this.matrix = a.matrix;
}
#endregion
#region properties
public int Col
{
get { return col; }
set { col = value; }
}
public int Row
{
get { return row; }
set { row = value; }
}
public double[,] MatrixX
{
get { return matrix; }
set { matrix = value; }
}
#endregion
static public Matrix Transpose(Matrix array)
{
Matrix temp = new Matrix(array.Col, array.Row);
for (int i = 0; i < array.Row; i++)
for (int j = 0; j < array.Col; j++)
temp.matrix[j, i] = array.matrix[i, j];
return temp;
}
static public void Display(Matrix array)
{
for (int i = 0; i < array.Row; i++)
{
for (int j = 0; j < array.Col; j++)
Console.Write("{0,5:f2}\t", array.matrix[i, j]);
Console.WriteLine();
}
Console.WriteLine();
}
static public Matrix Multiply(Matrix a, Matrix b)
{
if (a.Col != b.Row) throw new Exception("multiplication is impossible: a.Col != b.Row");
Matrix r = new Matrix(a.Row, b.Col);
for (int i = 0; i < a.Row; i++)
{
for (int j = 0; j < b.Col; j++)
{
double sum = 0;
for (int k = 0; k < b.Row; k++)
sum += a.matrix[i, k] * b.matrix[k, j];
r.matrix[i, j] = sum;
}
}
return r;
}
static public Matrix Invert(Matrix a)
{
Matrix E = new Matrix(a.Row, a.Col);
double temp = 0;
int n = a.Row;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
E.matrix[i, j] = 0.0;
if (i == j)
E.matrix[i, j] = 1.0;
}
for (int k = 0; k < n; k++)
{
temp = a.matrix[k, k];
for (int j = 0; j < n; j++)
{
a.matrix[k, j] /= temp;
E.matrix[k, j] /= temp;
}
for (int i = k + 1; i < n; i++)
{
temp = a.matrix[i, k];
for (int j = 0; j < n; j++)
{
a.matrix[i, j] -= a.matrix[k, j] * temp;
E.matrix[i, j] -= E.matrix[k, j] * temp;
}
}
}
for (int k = n - 1; k > 0; k--)
{
for (int i = k - 1; i >= 0; i--)
{
temp = a.matrix[i, k];
for (int j = 0; j < n; j++)
{
a.matrix[i, j] -= a.matrix[k, j] * temp;
E.matrix[i, j] -= E.matrix[k, j] * temp;
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
a.matrix[i, j] = E.matrix[i, j];
}
return a;
}
}
In your example, the determinant of x * transpose(x) is zero. As a result, there is no inverse, which is probably why you're getting strange results.
I also note that your Inverse function modifies the matrix passed to it. This should probably be modified to avoid that.

C# Array index out of bounds error

Full disclosure: This is for a homework assignment.
This is driving me nuts. I'm writing a Discrete Cosine Transform function and have it basically complete, but I'm running into an IndexOutOfRange exception.
Code is below:
static int[][] DiscreteCosineTransform(int[][] pIn)
{
int[][] cosP = pIn;
double SumCosP = 0;
double Cx = 0;
double Cy = 0;
Console.WriteLine("Discrete Cosine Transformed Array:");
for(int i = 0; i < 8; i++)
{
if (i == 0)
Cx = 1 / Math.Sqrt(2);
else
Cx = 1;
for(int j = 0; j < 8; i++)
{
if (j == 0)
Cy = 1 / Math.Sqrt(2);
else
Cy = 1;
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
SumCosP += cosP[x][y] * Math.Cos(((2 * x + 1) * i * Math.PI) / 16) * Math.Cos(((2 * y + 1) * j * Math.PI) / 16);
}
}
pIn[i][j] = (int)(0.25 * Cx * Cy * SumCosP);
Console.Write(" " + pIn[i][j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
return pIn;
}
Where pIn is:
int[][] P = new int[][]
{
new int[]{10,10,10,10,10,10,10,10},
new int[]{10,20,20,20,20,20,20,10},
new int[]{10,20,30,30,30,30,20,10},
new int[]{10,20,30,40,40,30,20,10},
new int[]{10,20,30,40,40,30,20,10},
new int[]{10,20,30,30,30,30,20,10},
new int[]{10,20,20,20,20,20,20,10},
new int[]{10,10,10,10,10,10,10,10}
};
This line
for(int j = 0; j < 8; i++)
should read
for(int j = 0; j < 8; j++)
^
You did:
for(int j = 0; j < 8; i++)
And most likely meant:
for(int j = 0; j < 8; j++)
(You did i++, not j++.)
change i to j at this line
for (int j = 0; j < 8; i++)

Categories

Resources