title: OpenCV - Extracting lines on a graph
body: <p>I would like to create a program that is able to extract lines from a graph.</p>
  
  <p>For example, if a graph like this is inputted, I would just want the red line to be outputted.
  <a href="https://i.stack.imgur.com/Gdz9N.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gdz9N.png" alt="enter image description here"></a></p>
  
  <p>Below I have tried to do this using a hough line transformation, however, I do not get very promising results.</p>
  
  <pre><code>import cv2
  import numpy as np
  
  graph_img = cv2.imread("/Users/2020shatgiskessell/Desktop/Graph1.png")
  gray = cv2.cvtColor(graph_img, cv2.COLOR_BGR2GRAY)
  
  kernel_size = 5
  #grayscale image
  blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
  
  #Canny edge detecion
  edges = cv2.Canny(blur_gray, 50, 150)
  
  #Hough Lines Transformation
  
  #distance resoltion of hough grid (pixels)
  rho = 1 
  #angular resolution of hough grid (radians)
  theta = np.pi/180 
  #minimum number of votes
  threshold = 15 
  
  #play around with these
  min_line_length = 25
  max_line_gap = 20
  
  #make new image
  line_image = np.copy(graph_img)
  
  #returns array of lines
  lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                      min_line_length, max_line_gap)
  
  for line in lines:
      for x1,y1,x2,y2 in line:
          cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),2)
  
  
  lines_edges = cv2.addWeighted(graph_img, 0.8, line_image, 1, 0)
  
  cv2.imshow("denoised image",edges)
  
  
  if cv2.waitKey(0) &amp; 0xff == 27:
      cv2.destroyAllWindows()
  </code></pre>
  
  <p>This produces the output image below, which does not accurately recognize the graph line. How might I go about doing this? </p>
  
  <p>Note: For now, I am not concerned about the graph titles or any other text. </p>
  
  <p><a href="https://i.stack.imgur.com/n7WB3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n7WB3.jpg" alt="enter image description here"></a></p>
  
  <p>I would also like the code to work for other graph images aswell, such as:
  <a href="https://i.stack.imgur.com/yb3fw.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yb3fw.jpg" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/fPGaN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fPGaN.jpg" alt="enter image description here"></a>
  etc.</p>