Fisheye effect with R
The fisheye
function below distorts a bitmap image with a fisheye effect or an anti-fisheye effect (rho > 0.5
or rho < 0.5
).
function(x, y, rho, stick) {
fisheye_xy <- c(x, y)
p <-if(rho == 0.5) {
return(p)
} c(0.5, 0.5)
m <- p - m
d <- sqrt(c(crossprod(d)))
r <- d / r
dnormalized <- sqrt(c(crossprod(m)))
mnorm <- pi / mnorm * (rho - 0.5)
power <-if(power > 0) {
if(stick == "corners") mnorm else m[2L]
bind <- m + dnormalized * tan(r*power) * bind / tan(bind*power)
uv <-else {
} m[2L]
bind <- m + dnormalized * atan(-10*r*power) * bind / atan(-10*bind*power)
uv <-
}
uv
}
library(imager)
library(cooltools)
#' @importFrom imager load.image add.color squeeze R G B
#' @importFrom cooltools approxfun2
#' @importFrom grDevices col2rgb rgb
#' @param bitmapFile path to a bitmap file (jpg, png, ...)
#' @param rho amount of effect; no effect if 0.5, fisheye if >0.5,
#' antifisheye if <0.5
#' @param stick where to stick the image when rho>0.5, to the
#' corners or to the borders; if you stick to the corners, a
#' part of the image is lost
#' @param bkg background color; it appears only if rho>0.5 and
#' stick="borders"
function(bitmapFile, rho, stick = "corners", bkg = "black") {
fisheye <-stopifnot(rho > 0, rho < 1)
match.arg(stick, c("borders", "corners"))
stick <-# load the image
load.image(bitmapFile)
img <- dim(img)
dims <- dims[1L]
nx <- dims[2L]
ny <- dims[4L]
nchannels <-# if the image is gray, add colors
if(nchannels == 1L) {
add.color(img)
img <-else if(nchannels != 3L) {
} stop("Cannot process this image.")
}# fisheye matrix
matrix(NA_complex_, nrow = nx, ncol = ny)
PSI <-for(i in 1L:nx) {
(i-1L) / (nx-1L)
x <-for(j in 1L:ny) {
(j-1L) / (ny-1L)
y <- fisheye_xy(x, y, rho, stick)
uv <- complex(real = uv[1L], imaginary = uv[2L])
PSI[i, j] <-
}
}# take the r, g, b channels
squeeze(R(img))
r <- squeeze(G(img))
g <- squeeze(B(img))
b <-# interpolation
seq(0, 1, length.out = nx)
x_ <- seq(0, 1, length.out = ny)
y_ <- approxfun2(x_, y_, r)
f_r <- approxfun2(x_, y_, g)
f_g <- approxfun2(x_, y_, b)
f_b <- f_r(Re(PSI), Im(PSI))
M_r <- f_g(Re(PSI), Im(PSI))
M_g <- f_b(Re(PSI), Im(PSI))
M_b <-# set outside color
col2rgb(bkg)[, 1L] / 255
RGB <-is.na(M_r)] <- RGB[1L]
M_r[is.na(M_g)] <- RGB[2L]
M_g[is.na(M_b)] <- RGB[3L]
M_b[# convert to hex codes
rgb(M_r, M_g, M_b)
rstr <-dim(rstr) <- c(nx, ny)
# rotate
t(rstr)
}
Let’s take for example this picture of Dilbert, named dilbert512x512.png:
It has a transparent background. We firstly transform this background it to a gray color (#aaaaaa
) with the help of ImageMagick. The command to do that is:
convert in.png -background '#aaaaaa' -alpha remove -alpha off out.png
(magick convert
if you use Windows).
We can run this command from R:
"dilbert512x512.png"
dilbert_transparent <- "dilbert_gray.png"
dilbert_gray <- "#aaaaaa"
gray_color <- sprintf(
cmd <-"convert %s -background '%s' -alpha remove -alpha off %s",
dilbert_transparent, gray_color, dilbert_gray
)system(cmd)
Here is dilbert_gray.png:
Now let’s perform a fisheye distortion of this image with rho=0.95
:
fisheye(dilbert_gray, rho = 0.95, stick = "borders", bkg = gray_color)
img <-# plot
par(mar = c(0, 0, 0, 0))
opar <-plot(c(-100, 100), c(-100, 100), type = "n", asp = 1,
xlab = NA, ylab = NA, axes = FALSE, xaxs = "i", yaxs = "i")
rasterImage(img, -100, -100, 100, 100)
par(opar)
The anti-fisheye effect is obtained by setting rho<0.5
. We will do it, with something more: we will get a transparent background at the end.
To do so, first transform the transparent background to a color, for example green (#00ff00
):
"dilbert512x512.png"
dilbert_transparent <- "dilbert_green.png"
dilbert_green <- "#00ff00"
green_color <- sprintf(
cmd <-"convert %s -background '%s' -alpha remove -alpha off %s",
dilbert_transparent, green_color, dilbert_green
)system(cmd)
Here is dilbert_green.png:
Now perform the anti-fisheye effect, and use the same green color as background:
fisheye(dilbert_green, rho = 0.45, bkg = green_color)
img <-# save image
png("dilbert_antifisheye_green.png", width = 512, height = 512)
par(mar = c(0, 0, 0, 0))
opar <-plot(c(-100, 100), c(-100, 100), type = "n", asp = 1,
xlab = NA, ylab = NA, axes = FALSE, xaxs = "i", yaxs = "i")
rasterImage(img, -100, -100, 100, 100)
par(opar)
dev.off()
Here is dilbert_antifisheye_green.png:
Finally, using ImageMagick, transform the green color to transparent:
sprintf(
cmd <-"convert -fuzz 30% -transparent '%s' %s %s",
"dilbert_antifisheye_green.png", "dilbert_antifisheye.png"
green_color,
)system(cmd)