我正在基于以下示例example 1和example 2重新创建R 中图像的桶形失真。
我制作了此示例图片以用于:
我当前正在使用的代码如下:
library("imager")
library("OpenImageR")
library("EBImage")
image <- readImage("rainbowgrid.png")
# parameters for correction
a = 0.3 #-0.007715 # affects only the outermost pixels of the image
b = 0.3 #0.026731 # most cases only require b optimization
c = 0.3 # most uniform correction
d = 1.0 - a - b - c # describes the linear scaling of the image
width <- 512
height <- 512
x <- (1:width)
y <- (1:height)
D <- min(width, height) / 2 # radius of the circle
# center of dst image, origin
centerX <- (width - 1) / 2
centerY <- (height - 1) / 2
# cartesian coordinates of the destination point (relative to the centre of the image)
deltaX <- (x - centerX) / D
deltaY <- (y - centerY) / D
# distance or radius of dst image
dstR <- sqrt(deltaX^2 + deltaY^2)
# distance or radius of src image (with formula)
srcR <- (a * dstR^3 + b * dstR^2 + c * dstR + d) * dstR
# comparing old and new distance to get factor
factor <- abs(dstR / srcR)
# coordinates in source image
srcX <- centerX + (deltaX * factor * d)
srcY <- centerY + (deltaY * factor * d)
dstPos <- y * width + x
q <- srcY * width + srcX
#I scaled these because they weren't the right scale for the warpfield below
scaleq <- q/512
scaledp <- dstPos/512
#In the original example, it ended with "image[dstPos] -> image_copy[q]"
#but the example was for matlab, not R, so I had to find a new way to warp the image.
#The warpfield below is from https://rdrr.io/cran/imager/man/warp.html
#Assemble warpfield components
warp.x <- scaledp
warp.y <- scaleq
#transpose y vector
warp.y <- t(warp.y)
warp.x <- replicate(512,warp.x)
warp.y <- matrix(rep(warp.y,each=512),nrow=512)
warp.x<- raster(warp.x)
warp.y<- raster(warp.y)
warp.x <- as.cimg(warp.x)
warp.y <- as.cimg(warp.y)
warpfield<-list(warp.x,warp.y) %>% imappend("c")
warp(image,warpfield,mode=1) %>% plot
因此,通过更改warp.x和warp.y应用于warp字段的方式,我得到了各种变形的图像,但是它们都不是我要寻找的正确的桶形失真/变形: / p>
整洁,但不是我想要的。