Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer of BRIEF,ORB,BRISK,FREAK, HOG #205

Merged
merged 8 commits into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/examples/image_features/assets/boxes.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/examples/image_features/assets/hog.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/examples/image_features/assets/human1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/examples/image_features/assets/human2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/examples/image_features/assets/human3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/examples/image_features/assets/humans.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/examples/image_features/assets/scores.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions docs/examples/image_features/brief.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# ---
# cover: assets/brief.gif
# title: BRIEF Descriptor
# description: This demo shows BRIEF descriptor
# author: Anchit Navelkar; Ashwani Rathee
# date: 2021-07-12
# ---

# `BRIEF` (Binary Robust Independent Elementary Features) is an efficient feature point descriptor.
# It is highly discriminative even when using relatively few bits and is computed using simple
# intensity difference tests. BRIEF does not have a sampling pattern thus pairs can be chosen
# at any point on the `SxS` patch.

# To build a BRIEF descriptor of length `n`, we need to determine `n` pairs `(Xi,Yi)`.
# Denote by `X` and `Y` the vectors of point `Xi` and `Yi`, respectively.

# In ImageFeatures.jl we have five methods to determine the vectors `X` and `Y` :

# - `random_uniform` : `X` and `Y` are randomly uniformly sampled
# - `gaussian` : `X` and `Y` are randomly sampled using a Gaussian distribution, meaning that locations that are closer to the center of the patch are preferred
# - `gaussian_local` : `X` and `Y` are randomly sampled using a Gaussian distribution where first `X` is sampled with a standard deviation of `0.04*S^2` and then the `Yi’s` are sampled using a Gaussian distribution – Each `Yi` is sampled with mean `Xi` and standard deviation of `0.01 * S^2`
# - `random_coarse` : `X` and `Y` are randomly sampled from discrete location of a coarse polar grid
# - `center_sample` : For each `i`, `Xi` is `(0, 0)` and `Yi` takes all possible values on a coarse polar grid

# As with all the binary descriptors, BRIEF’s distance measure is the number of
# different bits between two binary strings which can also be computed as the sum
# of the XOR operation between the strings.

# BRIEF is a very simple feature descriptor and does not provide scale or rotation
# invariance (only translation invariance). To achieve those, see ORB, BRISK,
# FREAK examples

# ## Example

# Let us take a look at a simple example where the BRIEF descriptor is used to match
# two images where one has been translated by `(100, 200)` pixels. We will use the
# `lighthouse` image from the [TestImages](https://github.com/timholy/TestImages.jl)
# package for this example.


# Now, let us create the two images we will match using BRIEF.

using ImageFeatures, TestImages, Images, ImageDraw, CoordinateTransformations

img = testimage("sudoku")
img1 = Gray.(img)
trans = Translation(-50, -50)
img2 = warp(img1, trans, axes(img1))

# To calculate the descriptors, we first need to get the keypoints. For this tutorial,
# we will use the FAST corners to generate keypoints (see `fastcorners`).

keypoints_1 = Keypoints(fastcorners(img1, 12, 0.4))
keypoints_2 = Keypoints(fastcorners(img2, 12, 0.4))


# To create the BRIEF descriptor, we first need to define the parameters by calling
# the `BRIEF` constructor.

brief_params = BRIEF(size = 256, window = 10, seed = 123)

# Now pass the image with the keypoints and the parameters to the
# `create_descriptor` function.

desc_1, ret_keypoints_1 = create_descriptor(img1, keypoints_1, brief_params)
desc_2, ret_keypoints_2 = create_descriptor(img2, keypoints_2, brief_params)

# The obtained descriptors can be used to find the matches between the two images
# using the `match_keypoints` function.

matches = match_keypoints(ret_keypoints_1, ret_keypoints_2, desc_1, desc_2, 0.1)

# We can use the [ImageDraw.jl](https://github.com/JuliaImages/ImageDraw.jl) package
# to view the results.

grid = hcat(img1, img2)
offset = CartesianIndex(0, size(img1, 2))
map(m -> draw!(grid, LineSegment(m[2] + offset,m[1] )), matches)
grid


# `grid` shows the results

save("assets/brief.gif", cat(img1, img2, grid[1:512,1:512], grid[1:512,513:1024]; dims=3); fps=1) #src
89 changes: 89 additions & 0 deletions docs/examples/image_features/brisk.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# ---
# cover: assets/brisk.gif
# title: BRISK Descriptor
# description: This demo shows BRISK descriptor
# author: Anchit Navelkar; Ashwani Rathee
# date: 2021-07-12
# ---

# The *BRISK* (Binary Robust Invariant Scalable Keypoints) descriptor has a predefined
# sampling pattern as compared to `BRIEF` or `ORB`.
# Pixels are sampled over concentric rings. For each sampling point, a small patch
# is considered around it. Before starting the algorithm, the patch is smoothed
# using gaussian smoothing.

# Two types of pairs are used for sampling, short and long pairs.
# Short pairs are those where the distance is below a set threshold distmax while the
# long pairs have distance above distmin. Long pairs are used for orientation and
# short pairs are used for calculating the descriptor by comparing intensities.

# BRISK achieves rotation invariance by trying the measure orientation of the keypoint
# and rotating the sampling pattern by that orientation. This is done by first
# calculating the local gradient `g(pi,pj)` between sampling pair `(pi,pj)` where
# `I(pj, pj)` is the smoothed intensity after applying gaussian smoothing.

# `g(pi, pj) = (pi - pj) . I(pj, j) -I(pj, j)pj - pi2`

# All local gradients between long pairs and then summed and the `arctangent(gy/gx)`
# between `y` and `x` components of the sum is taken as the angle of the keypoint.
# Now, we only need to rotate the short pairs by that angle to help the descriptor
# become more invariant to rotation.
# The descriptor is built using intensity comparisons. For each short pair if the
# first point has greater intensity than the second, then 1 is written else 0 is
# written to the corresponding bit of the descriptor.

# ## Example

# Let us take a look at a simple example where the BRISK descriptor is used to
# match two images where one has been translated by `(50, 40)` pixels and then
# rotated by an angle of 75 degrees. We will use the `lighthouse` image from the
# [TestImages](https://github.com/timholy/TestImages.jl) package for this example.

# First, let us create the two images we will match using BRISK.

using ImageFeatures, TestImages, Images, ImageDraw, CoordinateTransformations, Rotations
using MosaicViews

img = testimage("lake_color")

# Original Image

img1 = Gray.(img)
rot = recenter(RotMatrix(5pi/6), [size(img1)...] .÷ 2) # a rotation around the center
tform = rot ∘ Translation(-50, -40)
img2 = warp(img1, tform, axes(img1))
mosaicview(img, img1, img2; nrow=1)

# To calculate the descriptors, we first need to get the keypoints. For this
# tutorial, we will use the FAST corners to generate keypoints (see `fastcorners`).


features_1 = Features(fastcorners(img1, 12, 0.35))
features_2 = Features(fastcorners(img2, 12, 0.35))

# To create the BRISK descriptor, we first need to define the parameters by
# calling the `BRISK` constructor.


brisk_params = BRISK()


# Now pass the image with the keypoints and the parameters to the
# `create_descriptor` function.

desc_1, ret_features_1 = create_descriptor(img1, features_1, brisk_params)
desc_2, ret_features_2 = create_descriptor(img2, features_2, brisk_params)

# The obtained descriptors can be used to find the matches between the two
# images using the `match_keypoints` function.

matches = match_keypoints(Keypoints(ret_features_1), Keypoints(ret_features_2), desc_1, desc_2, 0.1)

# We can use the [ImageDraw.jl](https://github.com/JuliaImages/ImageDraw.jl) package to view the results.

grid = hcat(img1, img2)
offset = CartesianIndex(0, size(img1, 2))
map(m -> draw!(grid, LineSegment(m[1], m[2] + offset)), matches)
grid

save("assets/brisk.gif", cat(img, img2, grid[1:512,1:512], grid[1:512,513:1024]; dims=3); fps=1) #src
76 changes: 76 additions & 0 deletions docs/examples/image_features/freak.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# ---
# cover: assets/freak.gif
# title: FREAK Descriptor
# description: This demo shows FREAK descriptor
# author: Anchit Navelkar; Ashwani Rathee
# date: 2021-07-12
# ---

# `FREAK` (Fast Retina Keypoint) has a defined sampling pattern like `BRISK`.
# It uses a retinal sampling grid with more density of points near the centre
# with the density decreasing exponentially with distance from the centre.

# FREAK’s measure of orientation is similar to `BRISK` but instead of using
# long pairs, it uses a set of predefined 45 symmetric sampling pairs. The set of
# sampling pairs is determined using a method similar to `ORB`, by finding
# sampling pairs over keypoints in standard datasets and then extracting the most
# discriminative pairs. The orientation weights over these pairs are summed and the
# sampling window is rotated by this orientation to some canonical orientation to
# achieve rotation invariance.

# The descriptor is built using intensity comparisons of a predetermined set of 512
# sampling pairs. This set is also obtained using a method similar to the one described
# above. For each pair if the first point has greater intensity than the second,
# then 1 is written else 0 is written to the corresponding bit of the descriptor.

# ## Example

# Let us take a look at a simple example where the FREAK descriptor is used to
# match two images where one has been translated by `(50, 40)` pixels and then
# rotated by an angle of 75 degrees. We will use the `lighthouse` image from
# the [TestImages](https://github.com/timholy/TestImages.jl) package for this example.

# First, let us create the two images we will match using FREAK.

using ImageFeatures, TestImages, Images, ImageDraw, CoordinateTransformations, Rotations

img = testimage("peppers_color")

# Original

img1 = Gray.(img)
rot = recenter(RotMatrix(5pi/6), [size(img1)...] .÷ 2) # a rotation around the center
tform = rot ∘ Translation(-50, -40)
img2 = warp(img1, tform, axes(img1))

# To calculate the descriptors, we first need to get the keypoints. For this
# tutorial, we will use the FAST corners to generate keypoints (see `fastcorners`).

keypoints_1 = Keypoints(fastcorners(img1, 12, 0.35))
keypoints_2 = Keypoints(fastcorners(img2, 12, 0.35))


# To create the FREAK descriptor, we first need to define the parameters
# by calling the `FREAK` constructor.

freak_params = FREAK()

# Now pass the image with the keypoints and the parameters to the `create_descriptor` function.

desc_1, ret_keypoints_1 = create_descriptor(img1, keypoints_1, freak_params)
desc_2, ret_keypoints_2 = create_descriptor(img2, keypoints_2, freak_params)

# The obtained descriptors can be used to find the matches between the two
# images using the `match_keypoints` function.

matches = match_keypoints(ret_keypoints_1, ret_keypoints_2, desc_1, desc_2, 0.1)

# We can use the [ImageDraw.jl](https://github.com/JuliaImages/ImageDraw.jl)
# package to view the results.

grid = hcat(img1, img2)
offset = CartesianIndex(0, size(img1, 2))
map(m -> draw!(grid, LineSegment(m[1], m[2] + offset)), matches)
grid

save("assets/freak.gif", cat(img, img2, grid[1:512,1:512], grid[1:512,513:1024]; dims=3); fps=1) #src
Loading