how to change an image from cartesian to polar coordinates in idl

231 views
Skip to first unread message

sid

unread,
Oct 2, 2020, 10:56:32 AM10/2/20
to idl-pvwave
Hi all,
If we consider a source image with concentric circles drawn in Cartesian coordinates, how  to change them to straight lines in polar coordinates when using the center of the circles as origin.
thanking you in advance,

markus.sc...@gmail.com

unread,
Oct 4, 2020, 5:54:48 PM10/4/20
to idl-pvwave
You might want to consider how much angular resolution you want, and whether you want to cut off the corner of the image, but otherwise the following code should be suitable. Markus

; n x m array, origin=[xc, yc]
function carth2rad, arr, origin
  sz=size(arr,/dim)
  n=sz[0]
  m=sz[1]
  xc=origin[0]
  yc=origin[1]
  xe=[0,0,n-1,n-1]
  ye=[0,m-1,0,m-1]
  nr=floor(max(sqrt((xe-xc)^2+(ye-yc)^2)))
  nang=round(nr/4*!pi)*8
  rr=rebin(indgen(nr),nr,nang)
  aa=rebin(indgen(1,nang)*2*!pi/nang,nr,nang)
  xx=rr*sin(aa)+xc
  yy=rr*cos(aa)+yc
  in_rad_az=interpolate( arr,xx,yy,missing=!values.f_nan )
return, in_rad_az
end

; test
r=sqrt(rebin((indgen(20)-5)^2,20,15)+rebin((indgen(1,15)-3)^2,20,15))
in_rad_az=carth2rad(r,[5,3])
print,in_rad_az,format='(17f6.2)' 
end

sid

unread,
Oct 5, 2020, 10:40:31 PM10/5/20
to idl-pvwave
Thank you very much

Andrew Cool

unread,
Dec 11, 2020, 7:48:43 PM12/11/20
to idl-pvwave
Hi Marcus,

Your code seems to be almost what I need to transform a fisheye image to altaz. But not quite...
Here's an image I took of the Hayabusa 2 re-entry last Sunday:-


I'd like to convert that to an altaz image. Can I tempt you to modify your code to do that, for the sake of a free pretty picture of the re-entry...?

TIA,

Andrew

markus.sc...@gmail.com

unread,
Dec 12, 2020, 9:23:33 AM12/12/20
to idl-pvwave
 Hi Andrew,

if you do the coordinate transformation in RGB, you can use my function as is, if in CMYK you need to add the missing keyword to get the missing pixels in black instead of white. But if you intend to cut that off anyhow, it does not matter.
I'm not sure which color space results in a better result, that may also depend on the data compression scheme used.

You may have to fiddle a bit with the origin to get the horizon flat, but otherwise the following code should work.

Best wishes, Markus

; n x m array, origin=[xc, yc]
function carth2rad, arr, origin, missing=missing
if n_elements(missing) eq 0 then missing=!values.f_nan

sz=size(arr,/dim)
n=sz[0]
m=sz[1]
xc=origin[0]
yc=origin[1]
xe=[0,0,n-1,n-1]
ye=[0,m-1,0,m-1]
nr=floor(max(sqrt((xe-xc)^2+(ye-yc)^2)))
nang=round(nr/4*!pi)*8
rr=rebin(indgen(nr),nr,nang)
aa=rebin(indgen(1,nang)*2*!pi/nang,nr,nang)
xx=rr*sin(aa)+xc
yy=rr*cos(aa)+yc
in_rad_az=interpolate( arr,xx,yy,missing=missing )
return, in_rad_az
end

read_jpeg, 'Hayabusa_2_combined_images.jpg',img
;IMG BYTE = Array[3, 3648, 3648]
n=(size(img,/dim))[1]
origin=[1,1]*.5*n
nr=floor(sqrt(2)*origin[0])
nang=round(nr/4*!pi)*8

img2=bytarr(3,nr,nang)
for j=0,2 do img2[j,*,*]=carth2rad(reform(img[j,*,*]),origin)
img2=transpose(img2[*,-1:0:-1,*],[0,2,1])

CMYK_CONVERT, C, M, Y, K, reform(img[0,*,*]), reform(img[1,*,*]), reform(img[2,*,*]), /TO_CMYK
c=carth2rad(C,origin)
m=carth2rad(M,origin)
y=carth2rad(Y,origin)
k=carth2rad(K,origin,missing=255b)
CMYK_CONVERT, c, m, y, k, r, g, b
img3=[reform(transpose(r),[1,nang,nr]),reform(transpose(g),[1,nang,nr]),reform(transpose(b),[1,nang,nr])]
img3=img3[*,*,-1:0:-1]

write_png,'Hayabusa_2_combined_images_altAzRGB.png',img2
write_png,'Hayabusa_2_combined_images_altAzCMYK.png',img3 
Reply all
Reply to author
Forward
0 new messages