22

I am trying to get a region of an image (ROI) using opencv python. The version of opencv used is 2.4.3. However when I try to call the API

cv2.SetImageROI

it returns this error

AttributeError: 'module' object has no attribute 'SetImageROI'

Also on checking the documentation it seems to suggest this api is a legacy python function. http://docs.opencv.org/2.4.3/search.html?q=setimageroi

I am not sure how to go about getting the ROI using this current version of opencv in python. Could some one please suggest how to go about this?

Thanks

coffeewin
  • 182
  • 3
  • 6
  • 26
Ajay Nair
  • 1,827
  • 3
  • 20
  • 33

3 Answers3

40

Okay, On further analysis realized that the cv2 since it has been supporting numpy array structure, there is no longer any need for a API, the entire image can be manipulated in the array itself. eg:

img = cv2.imread('image.png')
img = img[c1:c1+25,r1:r1+25]

Here c1 is the left side column pixel location, and r1 is the corresponding row location. And img now has the image specified within the pixels as the ROI.

EDIT: Very nicely explained here, How to copy a image region using opencv in python?

Community
  • 1
  • 1
Ajay Nair
  • 1,827
  • 3
  • 20
  • 33
  • Look also at the answer of @Abid Rahman K here : http://stackoverflow.com/questions/9084609/how-to-copy-a-image-region-using-opencv-in-python – Ann Orlova Mar 15 '13 at 06:49
  • That seems more comprehensive in explaination. Thanks – Ajay Nair Mar 15 '13 at 06:52
  • 1
    @AjayNair How would you crop if the region of interest is a rotated rectangle? – annena May 27 '15 at 01:32
  • Here is the documentation article where it's more clear than the answer for some reason http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_core/py_basic_ops/py_basic_ops.html#image-roi – gabbar0x Sep 05 '16 at 09:44
  • 2
    The ordering of the indices might be wrong here - I think the default Numpy index order is rows then columns – Andrew W Aug 19 '19 at 05:51
  • ATTENTION! Wrong order! See the example below ROI = image[y1:y2, x1:x2] - so, first rows range then columns range – Valentin H Feb 14 '21 at 22:06
22

Here's a visualization for selecting a ROI from an image

-------------------------------------------
|                                         | 
|    (x1, y1)      w                      |
|      ------------------------           |
|      |                      |           |
|      |                      |           | 
|      |         ROI          | h         |  
|      |                      |           |   
|      |                      |           |   
|      |                      |           |       
|      ------------------------           |   
|                           (x2, y2)      |    
|                                         |             
|                                         |             
|                                         |             
-------------------------------------------

Consider (0,0) as the top-left corner of the image with left-to-right as the x-direction and top-to-bottom as the y-direction. If we have (x1,y1) as the top-left and (x2,y2) as the bottom-right vertex of a ROI, we can use Numpy slicing to crop the image with:

ROI = image[y1:y2, x1:x2]

But normally we will not have the bottom-right vertex. In typical cases we will most likely have the ROI's bounding box (x,y,w,h) coordinates obtained from cv2.boundingRect() when iterating through contours

cnts = cv2.findContours(grayscale_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    ROI = image[y:y+h, x:x+w]

Since OpenCV v2.2, Numpy arrays are naively used to display images. This Numpy slicing method to extract the ROI may not work with older versions

nathancy
  • 42,661
  • 14
  • 115
  • 137
6

As mentioned in documentation, and regarding the error message you got, you rather need to import the appropriate module and then call SetImageROI() method:

import cv
cv.SetImageROI(imag, rect)