## ---- include = FALSE---------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ## ----setup--------------------------------------------------------------- library(imagefx) ## ---- eval=FALSE--------------------------------------------------------- # install.packages('jpeg') ## ----eval=FALSE---------------------------------------------------------- # library(imagefx) # library(jpeg) # # ## identify where the image files are located # ##img.dir <- '~/Desktop/optical_flow-master/example_datasets/fluidized_flow/' # ##img.dir <- '~/Desktop/optical_flow-master/example_datasets/granular_flow/' # img.dir <- '~/Desktop/optical_flow-master/example_datasets/traffic_flow/' # # ## list all the files within this directory # img.files <- list.files(img.dir) # # ## choose the first file in the image files # cur.file = img.files[1] # # ## read in the JPEG image # cur.img <- readJPEG(paste(img.dir,cur.file,sep="")) # ## ----fig.width=7,fig.height=5,eval=FALSE--------------------------------- # ## at this point is important to rotate the image 90 degrees clockwise # ## this is necessary because R reads in images with the top left corner as the reference frame. # ## in other words, rotate the image so the origin is located in the bottom right # img.rot <- t(apply(cur.img,2,rev)) # # ## define the domain of this image # xdim <- nrow(img.rot) # ydim <- ncol(img.rot) # # ## choose the number of grids in the x and y direction # num.xgrid = 25 # num.ygrid = 20 # # ## find the grid xs and ys # grid.xs <- seq(1,xdim,length.out=num.xgrid+1) # grid.ys <- seq(1,ydim,length.out=num.ygrid+1) # # ## plot the grid locations # image(1:xdim,1:ydim,img.rot,col=gray.colors(20),asp=1) # abline(v=grid.xs,col='red',lwd=0.5) # abline(h=grid.ys,col='red',lwd=0.5) # ## ----eval=FALSE---------------------------------------------------------- # ## initilize a list to hold all the shifts in each grid over every image sequence pair # shift.list <- list() # # ## loop over all the images # ii=1 # while(ii 0.1 | abs(avg.shift.ys) > 0.1) # # ## limit the average shift xs, ys, and x and y locations to the positive indices # pos.shift.xs <- avg.shift.xs[pos.shift.inds] # pos.shift.ys <- avg.shift.ys[pos.shift.inds] # pos.xs <- avg.xs[pos.shift.inds] # pos.ys <- avg.ys[pos.shift.inds] # # ## what are the useres margins # mar.org=par()$mar # # ## set the margins for this particular plot # par(mar=c(0,0,0,0)) # # ## plot an example image # image(1:xdim,1:ydim,img1,col=gray.colors(20),useRaster=TRUE,asp=1,axes=FALSE,xlab='',ylab='') # # ## return the margins to the users original values # par(mar=mar.org) # # ## define how to scale the arrows (for easier visualization) # arrow.scale = 5 # # ## define the arrow locations # x0=pos.xs # y0=pos.ys # x1=x0+(pos.shift.xs * arrow.scale) # y1=y0+(pos.shift.ys * arrow.scale) # # ## plot the motion indicating the average shift between frames # arrows(x0=x0,y0=y0,x1=x1,y1=y1,length=0.05,lwd=2,angle=25,col='gray30') # arrows(x0=x0,y0=y0,x1=x1,y1=y1,length=0.05,lwd=1,angle=25,col='red') #