|
| 1 | +''' |
| 2 | +Written by Abhinav Dhere (abhitechnical41[at]gmail.com). |
| 3 | +NOTE: Currently some part of focussed image is being lost. Bug needs to be fixed. |
| 4 | +''' |
| 5 | +from PIL import Image |
| 6 | +import numpy as np |
| 7 | +import sys |
| 8 | +import pdb |
| 9 | + |
| 10 | +def sphericalTransform(im, xc, yc, rmax, rho ): |
| 11 | + ''' |
| 12 | + Apply spherical transform on image im. |
| 13 | + xc,yc - center of lens ; rmax - radius ; rho - refractive index of lens. |
| 14 | + ''' |
| 15 | + im2 = im1 |
| 16 | + for i in range(0,im2.shape[0]): |
| 17 | + for j in range(1,im2.shape[1]): |
| 18 | + dx=i-xc |
| 19 | + dy=j-yc |
| 20 | + r=(dx**2+dy**2)**0.5 |
| 21 | + z=(rmax**2-r**2)**0.5 |
| 22 | + Bx=(1-(1/rho))*np.arcsin(dx/((dx**2+z**2)**0.5)) |
| 23 | + By=(1-(1/rho))*np.arcsin(dy/((dy**2+z**2)**0.5)) |
| 24 | + if r<rmax: |
| 25 | + t1=int(np.round(i-z*np.tan(Bx))) |
| 26 | + t2=int(np.round(j-z*np.tan(By))) |
| 27 | + else: |
| 28 | + t1=0 |
| 29 | + t2=0 |
| 30 | + if (t1>0 and t1<im1.shape[0]) and (t2>0 and t2<im1.shape[1]): |
| 31 | + im2[i,j,0] = im1[t1,t2,0] |
| 32 | + im2[i,j,1] = im1[t1,t2,1] |
| 33 | + im2[i,j,2] = im1[t1,t2,2] |
| 34 | + return im1 |
| 35 | + |
| 36 | +filename=sys.argv[1] |
| 37 | +xc,yc,rmax,rho=np.ravel(map(float,sys.argv[2].split(','))) |
| 38 | +im1 = np.array(Image.open(filename)) |
| 39 | +print(im1.shape) |
| 40 | +im_trans = sphericalTransform(im1,xc,yc,rmax,rho) |
| 41 | +im_out = Image.fromarray(im_trans) |
| 42 | +im_out.show() |
0 commit comments