|
| 1 | +# --- |
| 2 | +# cover: assets/corner.png |
| 3 | +# title: Detecting Corners |
| 4 | +# --- |
| 5 | + |
| 6 | +# Corner Detection, which is a subset of Interest Point Detection, tries to detect |
| 7 | +# points in the image which have a well-defined position and can be robustly detected |
| 8 | +# in multiple images of the same scene. Very often, these points lie along the corners |
| 9 | +# or edges of objects in the image - hence the name. |
| 10 | + |
| 11 | +# Corner detection is useful in several computer vision tasks - such as Image Registration, |
| 12 | +# Motion Detection and Panaroma Stitching. This is because if the locations of the same |
| 13 | +# points are known in two different images, it gives a reference to align those images. Corners, |
| 14 | +# with their well-defined positions serve as good candidates for such points. |
| 15 | + |
| 16 | +using Images, TestImages |
| 17 | +img = Gray.(testimage("house")) |
| 18 | + |
| 19 | +# We use this image of a house, with numerous edges and corners. |
| 20 | + |
| 21 | +# The `imcorner` function can be used to detect corners in the image - and it returns an |
| 22 | +# array of booleans, where a `true` value denotes that the corresponding pixel may be a |
| 23 | +# corner. We use this to mark those pixels in red on a copy of the image. |
| 24 | + |
| 25 | +corners = imcorner(img) |
| 26 | +img_copy = RGB.(img) |
| 27 | +img_copy[corners] .= RGB(1.0, 0.0, 0.0) |
| 28 | +img_copy |
| 29 | + |
| 30 | +# As you can see, several points which lie in the interior of the object (the house) |
| 31 | +# have also been detected as corners. We can fix this by specifying a higher threshold percentile. |
| 32 | + |
| 33 | +corners = imcorner(img, Percentile(98.5)) |
| 34 | +img_copy2 = RGB.(img) |
| 35 | +img_copy2[corners] .= RGB(1.0, 0.0, 0.0) |
| 36 | +img_copy2 |
| 37 | +save("assets/corner.png", img_copy2) #src |
| 38 | + |
| 39 | +# This seems much better. A detailed documentation of the function parameters can be found in |
| 40 | +# the documentation of [`imcorner`](@ref). |
| 41 | + |
| 42 | +# Internally, `imcorner` uses one of three algorithms: *Harris*, *Shi Tomasi* or |
| 43 | +# *Kitchen Rosenfield* to detect corners. Which one to use can be specified using |
| 44 | +# the `method` parameter to `imcorner`. Each algorithm also has a separate method. |
| 45 | + |
| 46 | +detection_methods = [harris, shi_tomasi, kitchen_rosenfeld] |
| 47 | +img_copies = [RGB.(img) for i in 1:length(detection_methods)] |
| 48 | +for i in 1:length(detection_methods) |
| 49 | + corners = imcorner(img, Percentile(98.5); method=detection_methods[i]) |
| 50 | + img_copies[i] = RGB.(img) |
| 51 | + img_copies[i][corners] .= RGB(1.0, 0.0, 0.0) |
| 52 | +end |
| 53 | +mosaicview(img_copies; nrow=1) |
| 54 | + |
| 55 | +# These algorithms use the gradient of the image to identify corners, because intensities |
| 56 | +# change abruptly at corner points, giving rise to large gradients. However, this makes them |
| 57 | +# computationally expensive. |
| 58 | + |
| 59 | +# The *FAST (Features from Accelarated Segment Test) Corner Detector* is a feature |
| 60 | +# detection algorithm which is designed to be computationally cheaper, and hence much faster. |
| 61 | +# It classifies a pixel $P$ as a corner if at least $n$ contiguous points out of the 16 |
| 62 | +# points in a circle around it have intensities either higher (or lower) than that of $P$ |
| 63 | +# by a certain threshold $t$. |
| 64 | + |
| 65 | +corners = fastcorners(img, 11, 0.1) # fastcorners(img, n, t) where n and t are optional |
| 66 | +img_copy3 = RGB.(img) |
| 67 | +img_copy3[corners] .= RGB(1.0, 0.0, 0.0) |
| 68 | +img_copy3 |
0 commit comments