Revision 20f6c393f92e248dba873ba2081f74f701088124 (click the page title to view the current version)

Corner Detection

Briefing

Corners and Feature Points

Differentiation

Harris Feature Detector

Exercises

Setup

We will use scipy for a small part of the exercises, if you haven’t already, run ‘pip install scipy’.

We need an image to work with, you can either load an image from disk or capture a new image from the webcam with the below code.

As we will be working with a lot of different images in this exercise, it is recommended to save it to disk, we can do that with

Now we convert the image to gray-scale

Exercise 1

The first exercise is to implement a Sobel-filter. Recall from the theory that we need to implement two 3x3 kernels to convolve with the original image.

This can be done using scipy.signal.convolve2d ( https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve2d.html )

(Note: For a larger challenge you can also try implementing your own algorithm for the convolve function using numpy)

Code answers from here on will be collapsed, we recommend that you try to implement them yourself before reading an answer.

Hint (Click to expand)   Note: Use scipy.signal.convolve2d(<image>, <filter>, boundary='symm', mode='same')

 

Solution (Click to expand)

 

You should then show the images using cv.imshow or save using cv.imwrite, as we did earlier. Discuss the results.

You can compare the results of your implementation with the built in function cv.Sobel (the cv.CV_64F

Compute the magnitude and orientation of the derivatives using

Show/Save the images, and discuss.

Exercise 2

TODO: ?

As this is not a trivial step, we will calculate the EigenVals and Vecs using cv.cornerEigenValsAndVecs, note that this function also calculates applies a sobel-filter so we will call this function using the grayscale image.

The cornerEigenValsAndVecs algorithm will

  1. Calculate the derivatives \(dI/dx\) and \(dI/dy\) using the sobel filter (as we did in exercise 1)
  2. For each pixel \(p\), take a neighborhood \(S(p)\) of blockSize bsize*bsize, Calculate the covariation matrix \(M\) of the derivatives over the neighborhood as:

\[M = \begin{bmatrix} \sum_{S(p)}(dI/dx)^2 & \sum_{S(p)}(dI/dx)(dI/dy) \\ \sum_{S(p)}(dI/dx)(dI/dy) & \sum_{S(p)}(dI/dy)^2 \end{bmatrix} \]

After this the eigenvectors and eigenvalues of M are calculated and returned.

This should result in a h*w*6 array with values (\(\lambda_1,\lambda_2,x_1,y_1,x_2,y_2\)) where

  • \(\lambda_1\),\(\lambda_2\) are the non-sorted eigenvalues of M
  • \(x_1,y_1\) are the eigenvectors corresponding to \(\lambda_1\)
  • \(x_2,y_2\) are the eigenvectors corresponding to \(\lambda_2\)

Analyze? TODO: How can students learn from this exercise?

Exercise 3

Recall from the theory that we can calculate the harris-criterion with: \[\lambda_1 * \lambda_2 - k * (\lambda_1 + \lambda_1)^2\]

Debrief