# Kernel density demonstration animation # Michael Minn # 26 March 2022 # https://unix.stackexchange.com/questions/415430/h-264-mpeg-4-from-png-frames-how-to-and-how-to-tune-compression # ffmpeg -r 24 -f image2 -s 600x400 -start_number 0 -i temp/frame%03d.png -vcodec libx264 -crf 15 -pix_fmt yuv420p output.mp4 width=15 height=10 kernel.size = 1 set.seed(0) points.x = c(rnorm(4, 2, 2), rnorm(4, 11, 2), rnorm(4, 5, 3)) points.y = c(rnorm(4, 7, 2), rnorm(4, 6, 2), rnorm(4, 2, 2)) for (cell in 0:((width*height) - 1)) { png(sprintf("temp/frame%03d.png", cell), width=600, height=400) par(mar=c(0,0,0,0)) plot(0, 0, col="white", xlim = c(0, width), ylim=c(0,height)) for (z in 0:cell) { xleft = z %% width ybottom = floor(z / width) xright = xleft + 1 ytop = ybottom + 1 k.left = xleft - 1 k.right = xleft + 2 k.bottom = ybottom - 1 k.top = ybottom + 2 point.count = sum((points.x >= k.left) & (points.x <= k.right) & (points.y >= k.bottom) & (points.y <= k.top)) col = rgb(colorRamp(c("white","navy"))(point.count/4)/255) rect(xleft, ybottom, xright, ytop, col=col, border="lightgray") } points(points.x, points.y, pch=16, col="red", cex=2) kernel.x = cell %% width kernel.y = floor(cell / width) rect(kernel.x - 1, kernel.y - 1, kernel.x + 2, kernel.y + 2, lwd=3, border="navy") dev.off() # Sys.sleep(0.5) }