xcorr3d: Normalized Cross Correlation of Images

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/xcorr3d.r

Description

Input two images (matrices) and perform normalized cross correlation by multiplication in the frequency domain. Return the maximum normalized cross correlation value, its associated shift vector (x and y), and the correlation matrix.

Usage

1
xcorr3d(img1,img2)

Arguments

img1

Image (matrix) 1

img2

Image (matrix) 2 with same dimensions of img1

Details

Correlation calculated in the frequency domain as a multiplication. The dimensions of img1 and img2 must match. If xcorr3d is used to apply a match filter, it is logical to input the image to be searched over as img1 and the match filter as img2. Similarly, if tracking relative motion between images, it is logical to input the first image at time t=n as img1 and the second image at time t=n+1 as img2, otherwise motions will backwards.

Value

List whose values correspond to:

max.shifts

Vector of length two whose values are the x and y indices associated with the highest correlation value. Note these values are shifted according to the zero frequency which changes depending on the dimensions of img1 and/or img2.

max.corr

Highest normalized correlation value in the correlation matrix

corr.mat

Normalized correlation matrix

Author(s)

Alex J.C. Witsil

See Also

pcorr3d

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
############################
### Optical Flow Example ###
############################
## track movement between images

## set up the image domain
xdim = 256
ydim = 256

## shift vectors accounting for movement between the two images
x.vec = 100
y.vec = 100

## declare the indices where the Gaussian peak will be in the first image
gaus1.x = 50
gaus1.y = 50

## shift the Gaussian according to the shift vector
gaus2.x <- gaus1.x + x.vec
gaus2.y <- gaus1.y + y.vec

## create the first synthetic image
img1 = build.gaus(xdim,ydim,sig.x=10,x.mid=gaus1.x,y.mid=gaus1.y)

## create the second synthetic image
img2 = build.gaus(xdim,ydim,sig.x=10,x.mid=gaus1.x+x.vec,y.mid=gaus1.y+y.vec)

## now find the shift vector by using the cross correlation function 
shifts <- xcorr3d(img1,img2)

#############################
### Plotting Optical Flow ###
#############################

split.screen(c(1,2))
screen(1)
image(1:xdim,1:ydim,img1)

## add arrows indicating how the image shifted
arrows(gaus1.x,gaus1.y,gaus1.x+shifts$max.shifts[1],gaus1.y+shifts$max.shifts[2])

## add a point where the arrow is
points(gaus1.x+shifts$max.shifts[1],gaus1.y+shifts$max.shifts[2],pch=21,bg='green')

screen(2)
image(1:xdim,1:ydim,img2)

## add the point showing where the arrow is pointing
points(gaus1.x+shifts$max.shifts[1],gaus1.y+shifts$max.shifts[2],pch=21,bg='green')

## close the screen
close.screen(all.screens=TRUE)

imagefx documentation built on Feb. 14, 2020, 1:07 a.m.