title: How do I perform an image warp or transformation from one coordinate system to another for a barrel distorted image?
body: <p>I have used this <a href="https://stackoverflow.com/questions/25159962/correcting-barrel-lens-distortion-in-an-x-y-series-in-r">question</a> to help me come up with an undistorted coordinated system of an image. Now, I'm not sure how to implement the new coordinate system into the image so I can be able to produce an undistorted image. </p>
  
  <p>I'm having trouble finding answers that don't involve Matlab, OpenCV, or <a href="https://stackoverflow.com/questions/12620025/barrel-distortion-correction-algorithm-to-correct-fisheye-lens-failing-to-impl">C++</a> <strong>as I am using R</strong>. </p>
  
  <p>The answer I'm using from the cited question has given me the following transformed xy coordinate :</p>
  
  <pre><code>1 -19.50255239, -19.50255239
  2 -18.26735544, -18.26735544
  3 -17.03391152, -17.03391152
  4 -15.80221494, -15.80221494
  5 -14.57225998, -14.57225998
  6 -13.34404095, -13.34404095
  ...
  </code></pre>
  
  <p>and so on for 512 pixels in a 512 x 512 image.</p>
  
  <p>How to apply this back to the original 512 x 512 image is what I'm struggling with. I've seen some mention on pages like the Open CV page <a href="https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html?highlight=warp" rel="nofollow noreferrer">here</a> and specific <a href="https://rdrr.io/cran/imager/man/imwarp.html#heading-1" rel="nofollow noreferrer">pre-defined shifts</a>, or <a href="https://cran.r-project.org/web/packages/proj4/proj4.pdf" rel="nofollow noreferrer">latitudinal/longitudinal shifts</a>, use <a href="https://www.rdocumentation.org/packages/rgdal/versions/1.3-6/topics/spTransform-methods" rel="nofollow noreferrer">SpatialObjectsDataFrame</a>s, but not from one user-defined list of xy coordinates to another. </p>
  
  <p>-An example of getting the source image coordinates:</p>
  
  <pre><code>im_coords &lt;- RSAGA::grid.to.xyz(as.matrix(as.raster(im)))
  </code></pre>
  
  <p>(note, I don't actually want to raster the images, its just what I <a href="https://stackoverflow.com/questions/39562605/how-do-i-save-the-xy-coordinates-of-a-binary-image-in-r">found</a> at the time)</p>
  
  <p>-Code I'm using to get the transformed coordinates:</p>
  
  <pre><code>undistort &lt;- function(X, Y, a, b, c, d = 1, imWidth = 512, imHeight = 512) {
  
      #radial barrel distortion
      normX &lt;- X - (imWidth / 2)
      normY &lt;- Y - (imHeight / 2)
  
      #rmax
      radius_u &lt;- sqrt(imWidth^2 + imHeight^2)
  
      #normalize r so that its between 0 and 1
      r &lt;- sqrt(normX^2 + normY^2) /  radius_u
  
      #Barrel distorition equation: where "r" is the destination radius and "Rsrc" is the source pixel to get the pixel color from
      Rsrc &lt;- r * (a*r^3 + b*r^2 + c*r + d)
  
      theta &lt;- ifelse(Rsrc == 0, 1, 1 / atan(Rsrc) * Rsrc)
  
      newX &lt;- (imWidth / 2) + theta * normX
      newY &lt;- (imHeight / 2) + theta * normY
  
      return(data.frame(X = newX, Y = newY))
  }
  </code></pre>
  
  <p>Here is a sample sample 512x512 .png barrel distorted image: <a href="https://imgur.com/a/W9Qz70W" rel="nofollow noreferrer">https://imgur.com/a/W9Qz70W</a></p>
  
  <p><a href="https://i.stack.imgur.com/v1ivs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/v1ivs.png" alt="enter image description here"></a></p>
  
  <p>I'm wondering if <a href="https://cran.r-project.org/web/packages/gstat/vignettes/gstat.pdf" rel="nofollow noreferrer">kriging</a> could be useful? Or <a href="https://www.rdocumentation.org/packages/gdalUtils/versions/2.0.1.14/topics/gdalwarp" rel="nofollow noreferrer">gdalwarp</a> or <a href="https://proj4.org/usage/transformation.html" rel="nofollow noreferrer">proj4string</a>? Not sure how to implement these.</p>
  
  <p>UPDATE:
  Using Rohit's suggestions I was able to <em>distort</em> a rainbow grid from:</p>
  
  <p><a href="https://i.stack.imgur.com/MSjfi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MSjfi.png" alt="enter image description here"></a></p>
  
  <p>to this:</p>
  
  <p><a href="https://i.stack.imgur.com/yUdPA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yUdPA.png" alt="enter image description here"></a></p>
  
  <p>When I try it with the barrel image I get this weird superimposed image:</p>
  
  <p><a href="https://i.stack.imgur.com/PQyrW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PQyrW.png" alt="enter image description here"></a></p>
  
  <p>Ok, I think it is down to what coefficients you use as seen here:</p>
  
  <p><a href="https://i.stack.imgur.com/FPwy5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FPwy5.png" alt="enter image description here"></a></p>