# NOT RUN {
############################
### 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)
# }
Run the code above in your browser using DataLab