|
| 1 | +# --- |
| 2 | +# cover: assets/brisk.gif |
| 3 | +# title: BRISK Descriptor |
| 4 | +# description: This demo shows BRISK descriptor |
| 5 | +# author: Anchit Navelkar; Ashwani Rathee |
| 6 | +# date: 2021-07-12 |
| 7 | +# --- |
| 8 | + |
| 9 | +# The *BRISK* (Binary Robust Invariant Scalable Keypoints) descriptor has a predefined |
| 10 | +# sampling pattern as compared to `BRIEF` or `ORB`. |
| 11 | +# Pixels are sampled over concentric rings. For each sampling point, a small patch |
| 12 | +# is considered around it. Before starting the algorithm, the patch is smoothed |
| 13 | +# using gaussian smoothing. |
| 14 | + |
| 15 | +# Two types of pairs are used for sampling, short and long pairs. |
| 16 | +# Short pairs are those where the distance is below a set threshold distmax while the |
| 17 | +# long pairs have distance above distmin. Long pairs are used for orientation and |
| 18 | +# short pairs are used for calculating the descriptor by comparing intensities. |
| 19 | + |
| 20 | +# BRISK achieves rotation invariance by trying the measure orientation of the keypoint |
| 21 | +# and rotating the sampling pattern by that orientation. This is done by first |
| 22 | +# calculating the local gradient `g(pi,pj)` between sampling pair `(pi,pj)` where |
| 23 | +# `I(pj, pj)` is the smoothed intensity after applying gaussian smoothing. |
| 24 | + |
| 25 | +# `g(pi, pj) = (pi - pj) . I(pj, j) -I(pj, j)pj - pi2` |
| 26 | + |
| 27 | +# All local gradients between long pairs and then summed and the `arctangent(gy/gx)` |
| 28 | +# between `y` and `x` components of the sum is taken as the angle of the keypoint. |
| 29 | +# Now, we only need to rotate the short pairs by that angle to help the descriptor |
| 30 | +# become more invariant to rotation. |
| 31 | +# The descriptor is built using intensity comparisons. For each short pair if the |
| 32 | +# first point has greater intensity than the second, then 1 is written else 0 is |
| 33 | +# written to the corresponding bit of the descriptor. |
| 34 | + |
| 35 | +# ## Example |
| 36 | + |
| 37 | +# Let us take a look at a simple example where the BRISK descriptor is used to |
| 38 | +# match two images where one has been translated by `(50, 40)` pixels and then |
| 39 | +# rotated by an angle of 75 degrees. We will use the `lighthouse` image from the |
| 40 | +# [TestImages](https://github.com/timholy/TestImages.jl) package for this example. |
| 41 | + |
| 42 | +# First, let us create the two images we will match using BRISK. |
| 43 | + |
| 44 | +using ImageFeatures, TestImages, Images, ImageDraw, CoordinateTransformations, Rotations |
| 45 | +using MosaicViews |
| 46 | + |
| 47 | +img = testimage("lake_color") |
| 48 | + |
| 49 | +# Original Image |
| 50 | + |
| 51 | +img1 = Gray.(img) |
| 52 | +rot = recenter(RotMatrix(5pi/6), [size(img1)...] .÷ 2) # a rotation around the center |
| 53 | +tform = rot ∘ Translation(-50, -40) |
| 54 | +img2 = warp(img1, tform, axes(img1)) |
| 55 | +mosaicview(img, img1, img2; nrow=1) |
| 56 | + |
| 57 | +# To calculate the descriptors, we first need to get the keypoints. For this |
| 58 | +# tutorial, we will use the FAST corners to generate keypoints (see `fastcorners`). |
| 59 | + |
| 60 | + |
| 61 | +features_1 = Features(fastcorners(img1, 12, 0.35)) |
| 62 | +features_2 = Features(fastcorners(img2, 12, 0.35)) |
| 63 | + |
| 64 | +# To create the BRISK descriptor, we first need to define the parameters by |
| 65 | +# calling the `BRISK` constructor. |
| 66 | + |
| 67 | + |
| 68 | +brisk_params = BRISK() |
| 69 | + |
| 70 | + |
| 71 | +# Now pass the image with the keypoints and the parameters to the |
| 72 | +# `create_descriptor` function. |
| 73 | + |
| 74 | +desc_1, ret_features_1 = create_descriptor(img1, features_1, brisk_params) |
| 75 | +desc_2, ret_features_2 = create_descriptor(img2, features_2, brisk_params) |
| 76 | + |
| 77 | +# The obtained descriptors can be used to find the matches between the two |
| 78 | +# images using the `match_keypoints` function. |
| 79 | + |
| 80 | +matches = match_keypoints(Keypoints(ret_features_1), Keypoints(ret_features_2), desc_1, desc_2, 0.1) |
| 81 | + |
| 82 | +# We can use the [ImageDraw.jl](https://github.com/JuliaImages/ImageDraw.jl) package to view the results. |
| 83 | + |
| 84 | +grid = hcat(img1, img2) |
| 85 | +offset = CartesianIndex(0, size(img1, 2)) |
| 86 | +map(m -> draw!(grid, LineSegment(m[1], m[2] + offset)), matches) |
| 87 | +grid |
| 88 | + |
| 89 | +save("assets/brisk.gif", cat(img, img2, grid[1:512,1:512], grid[1:512,513:1024]; dims=3); fps=1) #src |
0 commit comments