I want to optimize this for loop for correcting coordinates of an image, it takes too long which is not suited for my system. I have done some profiling, the numpy roots is taking most of the time (near to 90%). Could someone suggest some optimization or vectorization of the code? Or a better alternative?
src = cv2.imread('distorted_JJ.bmp')
dist_center = np.array([512, 224])
k1 = 0.15
k2 = 0.52
h,w,_ = src.shape
xc = dist_center[0]
yc = dist_center[1]
dst = np.zeros([h,w,3],dtype=np.uint8)
dst[::]=((255,0,0))
for i in range(h):
for j in range(w):
ru = np.array([j-xc, yc-i])/w
p = [k2 , 0, k1, 0, 1, ru]
abs_rd = np.roots(p)
if i == yc and j == xc:
rd = np.array([0,0])
else:
rd = ru * (p/abs_rd)
v = np.array([xc/w + rd[0], yc/w - rd[1]])
v = v*w
v = v.astype(int)
dst[i][j] = src[v[1],v[0]]