Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. def read_img(image):
  2.  
  3. img = ndimage.imread(image) #this would return a 4-d array: [R,G,B,255]
  4. img_shape = img.shape
  5. print(img_shape)
  6.  
  7. #get the pixel coordinate
  8. w = img_shape[1] #the width
  9. # print(w)
  10. h= img_shape[0] #the height
  11. # print(h)
  12. uv_coord = []
  13. for u in range(w):
  14. for v in range(h):
  15. uv_coord.append([float(u),float(v)]) #this records the coord in the fashion of [x1,y1],[x1, y2], [x1, y3]....
  16. return np.array(uv_coord)
  17.  
  18. def add_distortion(sourceUV, dmatrix,Kmatrix):
  19. '''This function is programmed to remove the pixel of the given original image coords
  20. input arguments:
  21. dmatrix -- the intrinsic matrix [k1,k2,k3,k4] for tweaking purposes
  22. Kmatrix -- [fx, fy, cx, cy, s]'''
  23. u = sourceUV[:,0] #width in x
  24. v = sourceUV[:,1] #height in y
  25.  
  26. rho = np.sqrt(u**2 + v**2)
  27.  
  28. #get theta
  29. theta = np.arctan(rho,np.full_like(u,1))
  30.  
  31. # rho_mat = np.array([rho, rho**3, rho**5, rho**7])
  32. rho_mat = np.array([theta,theta**3, theta**5, theta**7])
  33.  
  34. #get the: rho(theta) = k1*theta + k2*theta**3 + k3*theta**5 + k4*theta**7
  35. rho_d = dmatrix@rho_mat
  36.  
  37. #get phi
  38. phi = np.arctan2((v - Kmatrix[3]), (u - Kmatrix[2]))
  39. xd = rho_d * np.cos(phi)
  40. yd = rho_d * np.sin(phi)
  41.  
  42. #converting the coords from image plane back to pixel coords
  43. ud = Kmatrix[0] * (xd + Kmatrix[4] * yd) + Kmatrix[2]
  44. vd = Kmatrix[1] * yd + Kmatrix[3]
  45. return np.column_stack((ud,vd))
  46.  
  47. def main():
  48. image_name = "original.png"
  49. img = cv2.imread(image_name)
  50. img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) #the cv2 read the image as BGR
  51.  
  52. w = img.shape[1]
  53. h = img.shape[0]
  54. uv_coord = read_img(image_name)
  55.  
  56. #for adding distortion
  57. dmatrix = [-0.391942708316175,0.012746418822063 ,-0.001374061848026 ,0.005349692659231]
  58.  
  59. #the Intrinsic matrix of the original picture's
  60. Kmatrix = np.array([9.842439e+02,9.808141e+02 , 1392/2, 2.331966e+02, 0.000000e+00])
  61.  
  62. # Kmatrix = np.array([2234.23470710156 ,2223.78349134123, 947.511596277837, 647.103139639432,-3.20443253476976]) #the distorted intrinsics
  63. uv = add_distortion(uv_coord,dmatrix,Kmatrix)
  64.  
  65. i = 0
  66. dstimg = np.zeros_like(img)
  67.  
  68. for x in range(w): #tthe coo
  69. for y in range(h):
  70. if i > (512 * 1392 -1):
  71. break
  72.  
  73. xu = uv[i][0] #x, y1, y2, y3
  74. yu = uv[i][1]
  75. i +=1
  76.  
  77. # if new pixel is in bounds copy from source pixel to destination pixel
  78. if 0 <= xu and xu < img.shape[1] and 0 <= yu and yu < img.shape[0]:
  79. dstimg[int(yu)][int(xu)] = img[int(y)][int(x)]
  80.  
  81. img = Image.fromarray(dstimg, 'RGB')
  82. img.save('my.png')
  83. img.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement