title: Get a contour (x,y) coordinates of a shape in an image with python
body: <p>I need to get a matrix with the coordinates (x,y) of the contour of the following image with python.</p>
  
  <p><a href="https://i.stack.imgur.com/cxWTt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cxWTt.png" alt="enter image description here"></a></p>
  
  <p>I try it with opencv canny detector and find contours but I get a lot of contours and I don't know how to get the one I want.</p>
  
  <pre><code>import numpy as np
  from matplotlib import pyplot as plt
  import cv2
  #from skimage import measure, feature, io
  #from skimage import img_as_ubyte
  
  x1 = 330
  xf = 690
  y1 = 0
  yf = 400
  
  img = cv2.imread('test.tif')
  img = img[y1:yf, x1:xf]
  edge = cv2.Canny(img, 100, 200)
  
  image, contours, hierarchy = cv2.findContours(edge, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/iUVEt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iUVEt.png" alt="enter image description here"></a></p>
  
  <p>I just need an array with the (x,y) coordinates of the contour. I think it is in the contours output of <code>cv2.findContours()</code> but I don't find the contour that I want…</p>
  
  <p>I also tried with the <code>matplotlib.pyplot.contour</code> function :</p>
  
  <pre><code>import cv2
  import numpy as np
  from matplotlib import pyplot as plt
  
  img = cv2.imread('test.tif', 0) # read image
  img = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)[1] # threshold image
  img = cv2.medianBlur(img, 15)  # remove noise
  
  # skeletonize 
  size = np.size(img)  # get number of pixels
  skel = np.zeros(img.shape, np.uint8) # create an array of zeros with the same shape as the image and 256 gray levels
  
  element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) # create a structurant element (cross)
  done = False
  
  while(not done):
      eroded = cv2.erode(img, element)
      temp = cv2.dilate(eroded, element)
      temp = cv2.subtract(img, temp)
      skel = cv2.bitwise_or(skel, temp)
      img = eroded.copy()
      zeros = size - cv2.countNonZero(img)
      if zeros == size:
          done = True
  
  cs = plt.contour(skel, 1)
  p = cs.collections[0].get_paths()[0]
  v = p.vertices
  x = v[:, 0]
  y = v[:, 1]
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/p9rbG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p9rbG.png" alt="enter image description here"></a></p>
  
  <p>But I just have closed contours and not the open contour which is going from the left to the right of the image.</p>
  
  <p>Thanks a lot for your answers.</p>