title: Is there a way to use cv2.approxPolyDP to approximate open curve?
body: <p>I would like to approximate smooth lines with chain of line segments.</p>
  
  <p>cv2.approxPolyDP in OpenCV 3.4 made a good result in the case of closed curve.</p>
  
  <p>Origin close curve:
  <a href="https://i.stack.imgur.com/hiVMO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hiVMO.png" alt="Origin close curve"></a>
  Approximated close curve:
  <a href="https://i.stack.imgur.com/8vQgh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8vQgh.png" alt="Approximated close curve"></a></p>
  
  <p>But in the case of open curve, cv2.approxPolyDP did not achieve the desired effect.</p>
  
  <p>Origin open curve:
  <a href="https://i.stack.imgur.com/1QYy5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1QYy5.png" alt="Origin open curve"></a>
  Approximated open curve:
  <a href="https://i.stack.imgur.com/8J39u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8J39u.png" alt="Approximated open curve"></a></p>
  
  <p>The result I want should be one chain of line segments but not a closed polygon, like this(this picture is created by Photoshop but not Python program):
  <a href="https://i.stack.imgur.com/dar9Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dar9Y.png" alt="enter image description here"></a></p>
  
  <p>Is there a way to use cv2.approxPolyDP to approximate open curve?</p>
  
  <p>My Python program is as follow:</p>
  
  <pre><code>import cv2
  
  img = cv2.imread('1.jpg')
  
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  
  cv2.imshow("gray", gray)
  cv2.waitKey(0)
  
  _, binary = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
  
  # cv2.imshow("binary", binary)
  # cv2.waitKey(0)
  
  _, contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  
  for contour in contours:
      epsilon = 0.009 * cv2.arcLength(contour, True)
      approx = cv2.approxPolyDP(contour, epsilon, closed=True)
      cv2.drawContours(img, [approx], -1, (0, 255, 255), 1)
  
  cv2.imshow("approx", img)
  cv2.waitKey(0)
  
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>The origin photos used in my program are as follow.</p>
  
  <p><a href="https://i.stack.imgur.com/bM2S6.jpg" rel="nofollow noreferrer">Close curve photo</a>
  <a href="https://i.stack.imgur.com/UXRuc.jpg" rel="nofollow noreferrer">Open curve photo</a></p>