title: OpenCV - Appending contours inside ROI as numpy array in python
body: <p>In the picture below I have found and drawn the contour(green) of my object. I would like to get the contour coordinates as a numpy array, so I have used
   <code>array = np.vstack(contours).squeeze()</code>. This gives me the coordinates of the entire contour in an array. </p>
  
  <p><strong>My question: is it possible to append only the contour coordinates(two green lines) inside the ROI(red square) into an array</strong>? </p>
  
  <p>I know I can find contour inside a ROI separately, but I'm interested in doing it this way since the plan is to have several ROIs, and I dont want to find contours seperatly for each ROI. Any help is much appreciated.</p>
  
  <p><strong>Image with the contour</strong></p>
  
  <p><a href="https://i.stack.imgur.com/BOfhd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BOfhd.jpg" alt="Image with contour"></a></p>
  
  <p><strong>Original picture:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/My0if.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/My0if.jpg" alt="original picture"></a></p>
  
  <p>Here is the code if anyone is interested:</p>
  
  <pre><code>import numpy as np
  import cv2
  
  img = cv2.imread('image.jpg')
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  ret, thresh = cv2.threshold(gray, 127, 255, 0)
  inv = 255 - thresh
  
  im2, contours, hierarchy = cv2.findContours(inv, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  cv2.drawContours(img, contours, -1, (0,255,0), 2)
  array = np.vstack(contours).squeeze()
  
  x, y, w, h= 150, 50, 500 ,150
  roi = img[y:(y+h),x:(x+w)]
  cv2.rectangle(img,(x,y), (x+w, y+h), (0,0,255), 1)
  
  cv2.imshow('image', img)
  cv2.waitKey(0)
  </code></pre>