Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. from matplotlib import cm
  2. from skimage.transform import (hough_line, hough_line_peaks)
  3. from skimage import data
  4. from skimage import io
  5.  
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8.  
  9. img = io.imread('star2.bmp')
  10. image = np.zeros((img.shape[0], img.shape[1]))
  11. for y in range(img.shape[1]):
  12.     for x in range(img.shape[0]):
  13.         if img[x][y][0] == 255:
  14.             image[x][y] = 255
  15.  
  16. h, theta, d = hough_line(image)
  17.  
  18. fig, (ax0, ax1, ax2) = plt.subplots(1, 3)
  19. plt.tight_layout()
  20.  
  21. ax0.imshow(image, cmap=cm.gray)
  22. ax0.set_title('Input image')
  23. ax0.set_axis_off()
  24.  
  25. ax1.imshow(np.log(1 + h), extent=[np.rad2deg(theta[-1]), np.rad2deg(theta[0]), d[-1], d[0]], cmap=cm.gray, aspect=1/1.5)
  26. ax1.set_title('Hough transform')
  27. ax1.set_xlabel('Angles (degrees)')
  28. ax1.set_ylabel('Distance (pixels)')
  29. ax1.axis('image')
  30. ax1.set_axis_off()
  31.  
  32. ax2.imshow(image, cmap=cm.gray)
  33. row1, col1 = image.shape
  34. for _, angle, dist in zip(*hough_line_peaks(h, theta, d)):
  35.     # y0 = int((dist -    0 * np.cos(angle)) / np.sin(angle))
  36.     # y1 = int((dist - col1 * np.cos(angle)) / np.sin(angle))
  37.     # print("---------------------")
  38.     y = []
  39.     p0 = None
  40.     p1 = None
  41.     first_point = True
  42.     for x in range(col1):
  43.         yy = int(round((dist - x*np.cos(angle)) / np.sin(angle)))
  44.  
  45.         if first_point:
  46.             for i in range(-1, 2):
  47.                 for j in range(-1, 2):
  48.                     if x+j < col1 and yy + i < row1 and x+j >= 0 and yy + i >= 0 and image[yy+i][x+j] == 255:
  49.                         p0 = (x+j, yy+i)
  50.                         first_point = False
  51.         else:
  52.             for i in range(-1, 2):
  53.                 for j in range(-1, 2):
  54.                     if x+j < col1 and yy + i < row1 and x+j >= 0 and yy + i >= 0 and image[yy+i][x+j] == 255:
  55.                         p1 = (x+j, yy+i)
  56.         y.append(yy)
  57.  
  58.     fp = True
  59.     for x in range(min(p0[0], p1[0]), max(p0[0], p1[0])):
  60.         y_x = int(round((dist - x*np.cos(angle)) / np.sin(angle)))
  61.         if fp:
  62.             xx = x
  63.             yy = y_x
  64.             fp = False
  65.             continue
  66.         print(np.sqrt((x - xx)**2 + (y_x - yy)**2))
  67.         if np.sqrt((x - xx)**2 + (y_x - yy)**2) <= 3:
  68.             ax2.plot((xx, x), (yy, y_x), '-r', linewidth=3)
  69.         else:
  70.             ax2.plot((xx, x), (yy, y_x), '-b', linewidth=3)  
  71.         xx = x
  72.         yy = y_x
  73.  
  74.  
  75.     ax2.plot([x for x in range(col1)], y, '-g', linewidth=1)
  76.     # ax2.plot((0, col1), (y0, y1), '-r', linewidth=2)
  77.     # ax2.plot((p0[0], p1[0]), (p0[1], p1[1]), '-r', linewidth=3)
  78.  
  79. ax2.axis((0, col1, row1, 0))
  80. ax2.set_title('Detected lines')
  81. ax2.set_axis_off()
  82.  
  83. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement