尝试在R中重新创建图像的桶形失真

时间:2019-03-15 15:54:07

标签: r image distortion fisheye warp

我正在基于以下示例example 1example 2重新创建R 中图像的桶形失真。

我制作了此示例图片以用于:

Rainbowgrid.png

期望的结果将是像这样的彩色版本: enter image description here

我当前正在使用的代码如下:

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>

enter image description here

enter image description here

整洁,但不是我想要的。

我认为Rsc方程在给定各种来源(123)的情况下似乎是正确的,但是我对正确的实现存在疑问这些信息特别是在R中。

不幸的是,大多数相关示例似乎是writtenMatlabPythonetc

0 个答案:

没有答案