感兴趣的区域 opencv python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15424852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:05:33  来源:igfitidea点击:

Region of Interest opencv python

pythonopencvimage-processing

提问by Ajay Nair

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

我正在尝试使用 opencv python 获取图像区域(ROI)。使用的opencv版本是2.4.3。但是,当我尝试调用 API 时

cv2.SetImageROI

it returns me the 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

同样在检查文档时,它似乎表明这个 api 是一个遗留的 python 函数。 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?

我不知道如何在 python 中使用当前版本的 opencv 来获得 ROI。有人可以建议如何解决这个问题吗?

Thanks

谢谢

采纳答案by Ajay Nair

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:

好的,进一步分析发现cv2既然已经支持numpy数组结构了,就不再需要任何API了,整个图像都可以在数组本身中进行操作。例如:

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.

这里c1是左侧列像素位置,r1是对应的行位置。现在 img 将像素内指定的图像作为 ROI。

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

编辑:这里很好地解释了如何在 python 中使用 opencv 复制图像区域?

回答by Ajay Nair

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

文档中所述,以及关于您收到的错误消息,您宁愿需要导入适当的模块,然后调用SetImageROI()方法:

import cv
cv.SetImageROI(imag, rect)

回答by nathancy

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

这是从图像中选择 ROI 的可视化

-------------------------------------------
|                                         | 
|    (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:

考虑(0,0)为图像的左上角,从左到右为 x 方向,从上到下为 y 方向。如果我们有一个 ROI(x1,y1)的左上角和(x2,y2)右下角的顶点,我们可以使用 Numpy 切片来裁剪图像:

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

但通常我们不会有右下角的顶点。在典型情况下,我们很可能会在迭代轮廓时(x,y,w,h)获得ROI 的边界框坐标cv2.boundingRect()

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

从 OpenCV v2.2 开始,Numpy 数组被天真地用于显示图像。这种用于提取 ROI 的 Numpy 切片方法可能不适用于旧版本