Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy import ndimage as ndi
  4. import math
  5. from skimage import feature
  6. from skimage import io
  7. from skimage.filters import roberts, sobel, scharr, prewitt
  8. from matplotlib import pyplot as plt
  9. from skimage import data
  10. from skimage.feature import corner_harris, corner_subpix, corner_peaks
  11. from skimage.transform import warp, AffineTransform
  12. from skimage.draw import ellipse
  13. from skimage.draw import circle
  14. from skimage.draw import line
  15. from matplotlib import cm
  16. from skimage.transform import (hough_line, hough_line_peaks,
  17.                                probabilistic_hough_line)
  18. from numpy import ones,vstack
  19. from numpy.linalg import lstsq
  20.  
  21.  
  22. def line_form(point1,point2):
  23.     points = [point1,point2]
  24.     x_coords, y_coords = zip(*points)
  25.     A = vstack([x_coords,ones(len(x_coords))]).T
  26.     m, c = lstsq(A, y_coords)[0]
  27.     return m,c
  28.  
  29. line_form((0,1),(0,2))
  30.  
  31. def is_line(point1, point2, matrix, r=5):
  32.     a, b = line_form(point1, point2)
  33.     start, end = point1, point2
  34.     if (point2[0] == point1[0] and point2[1] < point1[1]) or (point2[0] < point1[0]):
  35.         start, end = end, start
  36.     current = start
  37.     while current[0] < end[0] or current[1] < end[1]:
  38.         pass
  39.    
  40. image = io.imread("C:/Users/Wojtek/PycharmProjects/paper/set6/21.png")
  41. edges = scharr(image)
  42.  
  43.  
  44. coords = corner_peaks(corner_harris(edges), min_distance=4)
  45. coords_subpix = corner_subpix(edges, coords, window_size=15)
  46.  
  47. print(coords)
  48.  
  49. for coord1 in range(len(coords)):
  50.     rr, cc = circle(coords[coord1][0], coords[coord1][1], 5)
  51.     edges[rr, cc] = 0.5
  52.     #for coord2 in range(coord1+1, len(coords)):
  53.         #image[rr, cc] = 127
  54.         #rr, cc = line(coords[coord1][0],  coords[coord1][1], coords[coord2][0], coords[coord2][1])
  55.         #image[rr, cc] = 25
  56.         #edges[rr, cc] = 0.5
  57.  
  58.  
  59.  
  60. fig, ax = plt.subplots()
  61. ax.imshow(edges, interpolation='nearest', cmap=plt.cm.gray)
  62. ax.plot(coords[:, 1], coords[:, 0], '.b', markersize=3)
  63. ax.plot(coords_subpix[:, 1], coords_subpix[:, 0], '+r', markersize=15)
  64.  
  65.  
  66.    
  67. plt.show()
  68.  
  69.  
  70. # Classic straight-line Hough transform
  71. h, theta, d = hough_line(edges)
  72.  
  73. # Generating figure 1
  74. fig2, axes = plt.subplots(1, 3, figsize=(15, 6))
  75. ax = axes.ravel()
  76. ax[2].imshow(edges, cmap=cm.gray)
  77. for _, angle, dist in zip(*hough_line_peaks(h, theta, d)):
  78.     y0 = (dist - 0 * np.cos(angle)) / np.sin(angle)
  79.     y1 = (dist - edges.shape[1] * np.cos(angle)) / np.sin(angle)
  80.     ax[2].plot((0, edges.shape[1]), (y0, y1), '-r')
  81. ax[2].set_xlim((0, edges.shape[1]))
  82. ax[2].set_ylim((edges.shape[0], 0))
  83. ax[2].set_axis_off()
  84. ax[2].set_title('Detected lines')
  85. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement