import cv2 as cv
import numpy as np
def twirl(img,xc,yc,alpha,r_max):
height=img.shape[0]
width=img.shape[1]
transformed=np.copy(img)
for x_prime in range(img.shape[0]):
for y_prime in range(img.shape[1]):
dx=x_prime-xc
dy=y_prime-yc
r=np.sqrt(dx**2+dy**2)
beta=np.arctan(dy/dx)+alpha*((r_max-r)/r_max)
print('hello')
if r<r_max:
x=xc+r*np.cos(beta)
y=yc+r*np.cos(beta)
if 0 <= x < height - 1 and 0 <= y < width - 1:
j=int(x)
k=int(y)
# j1=j+1
# k1=k+1
a=abs(x-j)
b=abs(y-k)
transformed[x_prime,y_prime]=(1-b)*(1-a)*img[j,k]+(1-b)*a*img[j+1,k]+\
b*(1-a)*img[j,k+1]+b*a*img[j+1,k+1]
return transformed
img=cv.imread('twirl.png')
xc = img.shape[0] // 2
yc= img.shape[1] // 2
r_max = xc- 10
alpha = np.deg2rad(90)
out=twirl(img,xc,yc,alpha,r_max)
cv.imshow("Output",out)
cv.waitKey(0)
cv.destroyAllWindows()
I'm trying to apply twirl effect using bilinear interpolation. My code is given here. It gives the following error on 28th line.
if 0 <= x < height - 1 and 0 <= y < width - 1:
UnboundLocalError: local variable 'x' referenced before assignment
I searched for the same kind of errors but it's not helping.