Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# Harris Corner Detection Highlighting all edges not corners

  1. Mar 10, 2017 #1
    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, ANY help appreciated.

    Input and output:http://imgur.com/a/TtQSD\

    Main Code:

    Code (Text):

    Bitmap b = (Bitmap)pictureBox1.Image;
     var i = IntensityMat(b);
     var n = Mask(i,-0.00000001);
     var ty = ToBit(n);
     pictureBox1.Image = ty;
    [/NOPARSE]
     
    Functions:


    Code (Text):

    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;
       }
    [/NOPARSE]
     
     
  2. jcsd
  3. Mar 10, 2017 #2
    So turns out i forgot to do windowing xP, added code beneath, but now it either gets too many points, or not enough, http://imgur.com/a/7I899
    it works for a checkerboard, but not those squares o_O

    Code (Text):

     int[,] MaskWindow(int[,] m, double thresh,int window,out List<Point> p)// m matrix of I
            {
                List<Point> ff = new List<Point>();
                int[,] n = new int[m.GetLength(0), m.GetLength(1)];
               
                for (int i = 1; i < m.GetLength(0)-window; i+=window)
                {
                    for (int j = 1; j < m.GetLength(1)-window; j+=window)
                    {
                       
                        double[,] sumM = new double[2, 2];
                        for(int k = 0; k < window; k++)
                        {
                            for (int k2 = 0; k2 < window; k2++)
                            {
                                double ix = Math.Abs(m[i - 1+k, j+k2] - m[i+k, j+k2]);
                                double iy = Math.Abs(m[i+k, j - 1+k2] - m[i+k, j]+k2);
                                sumM = SumMat(sumM, M(ix, iy));
                            }
                        }
                        var lap = sumM;
                        var r = R(lap);

                        if (r > thresh)
                        {
                            ff.Add(new Point(i, j));
                            n[i, j] = 255;
                        }


                    }
                }
                p = ff;
                return n;
            }

     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Harris Corner Detection Highlighting all edges not corners
Loading...