1 | initial version |
Python
The OpenCV-Python interface uses NumPy arrays for image objects. So in Python you can do it as follows.
First use cv2.boundingRect
to get the bounding rectangle for a set of points (i.e. contours):
x, y, width, height = cv2.boundingRect(contours[i])
You can then use NumPy indexing to get your ROI from the image:
roi = img[y:y+height, x:x+width]
And save the ROI to a new file:
cv2.imwrite("roi.png", roi)