Simone Bianchi
Simone Bianchi

Reputation: 143

Image polar transformation in R

I have a picture like this, taken with Google Street View app and cropped to the top half:

Google Street View image

I use Gimp polar coordinates filter to obtain this new image: Hemispherical picture

I would like to do the same within R, keeping all the pixel values information.

Here I found a similar question but the results is not exactly what I need (the center of the transformation is a corner). The adjusted code (there was an issue with radians) is:

# Load the image
library(png)
library(RCurl)
d <- readPNG( getBinaryURL( "https://i.sstatic.net/rMR3C.png" ) )
image(d)

x0 = as.vector(col(d))
y0 = as.vector(row(d))

r = sqrt( (x0^2) + (y0^2)  ) #x
a = atan(y0/x0) * 215 / (pi/2) #y
m = as.matrix(data.frame(y=a, x=r))

m = round(m)
m[m>215] = NA
m[m==0] = NA

xn = d[m]
xn = matrix(xn, 215, 215)

image(xn)

The answer to that question expands on the issue and show a code to do a polar to cartesian transformation but I cannot do the reverse.

Upvotes: 1

Views: 316

Answers (1)

Simone Bianchi
Simone Bianchi

Reputation: 143

The magick package does the job.

library(magick)

# load sample image
z <- image_read("https://i.sstatic.net/MYIeg.jpg")
 
# do polar distortion
zp <- image_distort(z, # source image
                    "Polar", # type, check availability at distort_types()
                    c(0), # coordinates, I could not understand fully but this work
                    bestfit=T # crop to the image to a square
)

plot(zp)

Upvotes: 1

Related Questions