Guest User

Untitled

a guest
Oct 23rd, 2019
97
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import matplotlib as mpl
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import numpy as np
  4. from PIL import Image, ImageDraw
  5. import matplotlib.pyplot as plt
  6. import cv2
  7. import progressbar
  8.  
  9. N_MAX = 4
  10. INPUT_FILE = 'curve.png'
  11. # INPUT_FILE = 'hough-lane-detect-python/canny_output/cannylane_test.jpg'
  12.  
  13. def forceAspect(ax,aspect):
  14. xleft, xright = ax.get_xlim()
  15. ybottom, ytop = ax.get_ylim()
  16. ax.set_aspect(abs((xright-xleft)/(ybottom-ytop))*aspect)
  17.  
  18.  
  19. def plot_accumulator(accumulator):
  20. # Create the x, y, and z coordinate arrays. We use
  21. # numpy's broadcasting to do all the hard work for us.
  22. # We could shorten this even more by using np.meshgrid.
  23. x = np.arange(accumulator.shape[0])[:, None, None]
  24. y = np.arange(accumulator.shape[1])[None, :, None]
  25. z = np.arange(accumulator.shape[2])[None, None, :]
  26. x, y, z = np.broadcast_arrays(x, y, z)
  27. c = np.tile(accumulator.ravel()[:, None], [1, 3])
  28.  
  29. ax = plt.axes(projection='3d')
  30. ax.scatter(x.ravel(), y.ravel(), z.ravel(), c=accumulator.ravel());
  31. plt.show()
  32.  
  33.  
  34. def plot_curve(img,k,beta,v,ax_img):
  35. h,w = img.shape
  36. x_vals = np.linspace(-w,w,2*w)
  37. y_vals = k/x_vals + beta*x_vals + v
  38. ax_img.plot(x_vals,y_vals,'k',color='firebrick',alpha=0.5)
  39. return ax_img
  40.  
  41. def do_hough(img):
  42.  
  43. print("-------------------------------------")
  44.  
  45. small_to_large_image_size_ratio = 0.3
  46. img = cv2.resize(img,
  47. (0,0), # set fx and fy, not the final size
  48. fx=small_to_large_image_size_ratio,
  49. fy=small_to_large_image_size_ratio,
  50. interpolation=cv2.INTER_LINEAR)
  51.  
  52. img = np.flipud(img) # vertical flip
  53.  
  54. h,w = img.shape
  55. diag = np.ceil(np.hypot(h,w))
  56. print(f"IMG dimensions: {img.shape} max. intensity: {np.max(img)}")
  57.  
  58. beta_min = 0
  59. beta_max = 10
  60. k_min = -2000
  61. k_max = 0
  62. betas = np.linspace(0,3,21)
  63. # betas = [2,3]
  64. # print(betas)
  65. ks = np.linspace(k_min,k_max,101)
  66. # ks = [-1000,-1200,-1500]
  67.  
  68. vs = np.arange(0,2*h+1)
  69. accumulator = np.zeros((len(betas),len(ks),len(vs)), dtype=np.uint64)
  70.  
  71. for y in progressbar.progressbar(range(1,h-1)):
  72. for x in range(1,w-1):
  73. if img[y,x] > 0: # if we're on an edge
  74. # print(i,j)
  75. for beta_i in range(len(betas)):
  76. for k_i in range(len(ks)):
  77. v = np.int64(np.round(y - ks[k_i]/x - betas[beta_i]*x))
  78. # print(f"x:{x} | y:{y} | beta:{betas[beta_i]} | k:{ks[k_i]} | v:{v}")
  79. if v >= -h and v < h:
  80. accumulator[beta_i,k_i,v+h] += 1 # increment accumulator for this coordinate pair
  81.  
  82.  
  83.  
  84. fig = plt.figure()
  85.  
  86. ax_img = fig.add_subplot(111) # original image
  87. ax_img.imshow(np.flipud(img),cmap='gray',extent=[0, w, 0, h])
  88.  
  89.  
  90. for i in range(N_MAX):
  91. max = np.unravel_index(accumulator.argmax(), accumulator.shape)
  92. print(np.max(accumulator),"at",max)
  93. print("k",ks[max[1]]," | beta",betas[max[0]]," | v",vs[max[2]])
  94.  
  95. # plot_accumulator(accumulator)
  96. ax_img = plot_curve(np.flipud(img),ks[max[1]],betas[max[0]],max[2]-h,ax_img)
  97.  
  98. accumulator[max[0]][max[1]][max[2]] = 0
  99.  
  100.  
  101. ax_img.set_ylim(0,h)
  102. ax_img.set_xlim(0,w)
  103. forceAspect(ax_img,0.5)
  104.  
  105. # plt.savefig('curve.png')
  106. # ax_img.invert_yaxis()
  107. plt.show()
  108.  
  109.  
  110. # open test img as grayscale array
  111. img = np.array(Image.open(INPUT_FILE).convert("L"))
  112.  
  113. # img = np.zeros((250,250))
  114. # img[120,10] = 255
  115. # img[190,20] = 255
  116. # img[210,25] = 255
  117. # img[91,8] = 255
  118. do_hough(img)
RAW Paste Data