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.

edit retag close merge delete

Sort by » oldest newest most voted

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)

more

And for realtime processing of images, such as in robotics and surveillance, you can pass the ROI image directly on to the next stage, such as cascade classifier, etc.

( 2014-03-04 04:31:21 -0600 )edit

TypeError: slice indices must be integers or None or have an __index__ method

( 2018-08-29 09:02:31 -0600 )edit

You could use boundingRect() to get bounding box for a contour. See tutorial here

Using the above bounding Rect you can save the image like

   Rect R=boundingRect(contour[i]); // Get bounding box for contour i
Mat ROI=src(R); //Set ROI on source image
imwrite("cropped.jpg",ROI); //save ROI image

more

1

In Java, the call is to;

public Mat(Mat m, Rect roi)

( 2014-02-28 15:25:15 -0600 )edit

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

( 2014-03-04 03:42:21 -0600 )edit

you can easily crop the image in python by using

roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]


In order to get the two points you can call cv2.setMouseCallback("image", mouse_crop). The function is something like this

def mouse_crop(event, x, y, flags, param):
# grab references to the global variables
global x_start, y_start, x_end, y_end, cropping

# if the left mouse button was DOWN, start RECORDING
# (x, y) coordinates and indicate that cropping is being
if event == cv2.EVENT_LBUTTONDOWN:
x_start, y_start, x_end, y_end = x, y, x, y
cropping = True

# Mouse is Moving
elif event == cv2.EVENT_MOUSEMOVE:
if cropping == True:
x_end, y_end = x, y

# if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates
x_end, y_end = x, y
cropping = False # cropping is finished

refPoint = [(x_start, y_start), (x_end, y_end)]

if len(refPoint) == 2: #when two points were found
roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]
cv2.imshow("Cropped", roi)
cv2.imwrite("crop.jpg",roi)


You can get details from here : Mouse Click and Cropping using Python

more

Why in a name would you revivie a dead topic of 2014?

( 2018-01-15 04:21:55 -0600 )edit

C++ version：

function—Mat roi(image,Rect(20,20,60,60)); that means selecting roi （20,20,60,60） a rectangle you know

more

York, what would it be for a Rect that already exists in a variable - pls update to show how a variable is passed into this, and you'll start receiving uparrow credits (I'll be the first!)

( 2014-03-01 08:04:30 -0600 )edit

aoh~get it…just passing by ，sorry for my ignorance ps：credits ！who care — —

( 2014-03-02 00:46:15 -0600 )edit