title: Finding multiple lines in a point cloud using ransac
body: <p>I have a point cloud stored in a text file in the form of x and y as shown in the image. I need to find multiple lines representing each side for the shown point cloud using ransac so that I can split the points representing inliers for each line. How can I do that using ransac in python so that the result is something like the second image but with better accuracy?</p>
  <p><a href="https://i.stack.imgur.com/gfbcQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gfbcQ.jpg" alt="enter image description here" /></a></p>
  <pre><code>import matplotlib.pyplot as plt
  import numpy as np
  from sklearn import linear_model, datasets
  from skimage.measure import LineModelND, ransac
  import pandas as pd
  import math
  def plot_ransac(segment_data_x, segment_data_y):
      data = np.column_stack([segment_data_x, segment_data_y])
      model_robust, inliers = ransac(data, min_samples=5,residual_threshold=5, max_trials=500)
  def distance(x1,y1,x2,y2):
      return np.sqrt((x1-x2)**2 + (y1-y2)**2)
  x,y = np.loadtxt('D:/Point cloud data/Data mabna 4/Data_without_RGB/Downsampled/Walls_only_downsampled_5cm.txt', unpack=True)
  x_data =  np.array(x)
  y_data =  np.array(y)
  x_segments = []
  y_segments = []
  distances = []
  start = 0
  for i in range(len(x_data)-1):
      distance_to_point = distance(x_data[i], y_data[i], x_data[i+1], y_data[i+1])
      distances.append(distance_to_point)
      if distance_to_point &gt; 5:
          if i-start&gt;10:
              x_segments.append(x_data[start:i])
              y_segments.append(y_data[start:i])
          start = i+1
      if i == len(x_data)-2:
          if i-start&gt;10:
              x_segments.append(x_data[start:i])
              y_segments.append(y_data[start:i])
  
  # plt.plot(x_data, y_data, '.', color = 'grey')
  for x_seg, y_seg in zip(x_segments, y_segments):
      plt.plot(x_seg, y_seg,'.', markersize = 5)
  #     plot_ransac(x_seg, y_seg)
  plt.axis('equal')
  plt.show()
  </code></pre>
  <p><a href="https://i.stack.imgur.com/gEAPz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gEAPz.png" alt="enter image description here" /></a></p>