I tried to do anamorphosis on an image by following this link
https://github.com/aydal/Cylinderical-Anamorphosis/blob/master/anamorph.py
It gives an anamorphic image but it gives that image in the half circle. But I want the output in a full circle size. I tried with
warp[c-j, i-1] = img[p-1, q-1]
warp[c+j, i-1] = img[p-1, q-1]
Instead of warp[c-j, i-1] = img[p-1, q-1]
But it doesn't give one image in the full circle rather creates same output twice!
Can anyone please help me.
Complete Code:
import math
from cv2 import *
import numpy as np
img = imread("test.jpg")
(rows, cols) = (img.shape[0], img.shape[1])
r = 0 #offset-gives space to keep cylinder and height of the image from bottom: original: math.trunc(.25*rows)
c = rows #this will be the decisive factor in size of output image-maximum radius of warped image: original: c = r+rows
warp = np.zeros([c,2*c,3], dtype=np.uint8)
def convert(R, b):
return math.trunc(b*rows/(2*math.asin(1))), math.trunc(c-R)
for i in range(0, 2*c):
for j in range(1, c):
b = math.atan2(j, i-c)
R = math.sqrt(j*j+math.pow(i-c, 2))
if R>=r and R<=c:
(q, p) = convert(R, b)
warp[c-j, i-1] = img[p-1, q-1]
#warp[c+j, i-1] = img[p-1, q-1]
imshow("Output", warp)
waitKey()
Similar to the column offset you should include an offset for the rows as well when computing
b
andR
. Since the warped image hasc
rows the offset isc//2
:Note that the warped image is not a perfect circle as you specified it be double as wide as high. If you want a full circle you should also adjust the upper boundary check for
R
to bec//2
as this is the max. radius along the rows:And similarly you need to adjust the computation in
convert
:But then, in any case, you could just use a square image right from the beginning, i.e. specify
warp.shape == (c, c)
.Edit
Updated code, uses original dimensions for warped image:
And output image: