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 yc = dist_center 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, yc/w - rd]) v = v*w v = v.astype(int) dst[i][j] = src[v,v]