Ask Your Question

Serrano P.'s profile - activity

2016-04-02 22:31:50 -0500 received badge  Famous Question (source)
2015-12-02 11:30:30 -0500 received badge  Student (source)
2015-09-17 07:49:31 -0500 received badge  Notable Question (source)
2015-06-04 09:17:21 -0500 received badge  Popular Question (source)
2014-03-04 04:28:57 -0500 received badge  Nice Answer (source)
2014-03-04 04:23:37 -0500 received badge  Teacher (source)
2014-03-04 04:07:38 -0500 received badge  Self-Learner (source)
2014-03-04 03:42:21 -0500 commented answer How to save a rectangular ROI?

Thanks for providing the C++ solution! I posted the Python solution myself.

2014-03-04 03:40:42 -0500 answered a question How to save a rectangular ROI?

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)
2014-02-28 08:15:00 -0500 received badge  Supporter (source)
2014-02-28 08:12:54 -0500 received badge  Editor (source)
2014-02-28 06:39:42 -0500 asked a question How to save a rectangular ROI?

I have extracted contours with cv2.findContours from a binary mask which I created using a segmentation algorithm. Each contour corresponds to an object in the image that I want to a save to a separate image file.

How can I get a rectangular region of interest (ROI) from a set of points (e.g. contour) and save that ROI to a separate image?

I use Python, but if you can post the answer for C++, that's also appreciated.