Revision 09824acb7a3f524af5850b2105d1919a9882a1db (click the page title to view the current version)
Changes from 09824acb7a3f524af5850b2105d1919a9882a1db to 30c1737950c14e02f59bd97946d56a08b48716e9
---
title: Corner Detection
categories: session
---
# Briefing
# A collapsible section with markdown
## Corners and Feature Points
## Differentiation
## Harris Feature Detector
# Exercises
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.
```python
# Imports
import cv2 as cv
# Capture a single frame
vid = cv.VideoCapture(0)
image = None
# Capture frames until we click the space button, use that image
while True:
_, frame = vid.read()
# Display the resulting frame
cv.imshow("frame", frame)
k = cv.waitKey(1)
if k % 256 == 32:
# SPACE pressed
image = frame
break
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv.destroyAllWindows()
```
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
```python
from pathlib import Path
# First we create a path to an images directory
p = Path.cwd() / "images" # <--- current working directory + /images
if not p.is_dir():
p.mkdir()
# Then we save the image to the directory with name "frame.jpg"
cv.imwrite(str(p / "frame.jpg"), image)
```
As we will be working with gray-scale images, we need to convert the image
```python
# Convert frame to grayscale
image_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# Save gray to images
cv.imwrite(str((p / "gray.jpg")), image_gray)
```
The first part of the exercise is to implement a Sobel-filter, we 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)
If you haven't already, run 'pip install scipy'.
<details>
<summary>Click to expand!</summary>
<summary>Hint (Click to expand)</summary>
Note: Use signal.convolve2d(<image>, <filter>, boundary='symm', mode='same')
</details>
## Heading
Code answers from here on will be collapsed, we recommend that you try to
implement them yourself before reading an answer.
1. A numbered
2. list
* With some
* Sub bullets
<details>
<summary>Answer (Click to expand)</summary>
```python
#!/usr/bin/env python3
from scipy import signal
print("Hello World!")
sobel_x = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
sobel_y = np.array([[-1, -2, -1],
[0, 0, 0],
[1, 2, 1]])
grad_x = signal.convolve2d(image_gray, sobel_x, boundary='symm', mode='same')
grad_y = signal.convolve2d(image_gray, sobel_y, boundary='symm', mode='same')
```
</details>
## Corners and Feature Points
You should then show the images using cv.imshow or save using cv.imwrite, as we did earlier
Discuss what you see.
## Differentiation
You can compare the results of your implementation with the built in function
cv.Sobel
## Harris Feature Detector
```python
grad_x_cv = cv.Sobel(image_gray, cv.CV_64F, 1, 0, ksize=3) # gradient along x axis,
grad_y_cv = cv.Sobel(image_gray, cv.CV_64F, 0, 1, ksize=3) # gradient along y axis,
```
# Exercises
# Debrief