简体   繁体   English

如何在python中使用opencv复制图像区域?

[英]How to copy a image region using opencv in python?

I am trying to implement a license plate recognition software using the ideas from http://iamabhik.wordpress.com/category/opencv/ .我正在尝试使用来自http://iamabhik.wordpress.com/category/opencv/的想法实现车牌识别软件。

I implemented the plate location using opencv in python, using "import cv2".我在 python 中使用 opencv 实现了板位置,使用“import cv2”。 It works okay and now I need to copy the plate region to another image to do the segmentation of the characters and then the OCR part (maybe using a neural network).它工作正常,现在我需要将板块区域复制到另一个图像以进行字符的分割,然后是 OCR 部分(可能使用神经网络)。

I found the GetSubRect() function to copy or isolate part of the image but it does not appear to be available in python.我发现 GetSubRect() 函数可以复制或隔离图像的一部分,但它在 python 中似乎不可用。 Is there an alternative?有替代方案吗? The ROI functions do not seem to be implemented either. ROI 功能似乎也没有实现。

Is there an up-to-date documentation of the python interface to opencv?是否有关于 opencv 的 python 接口的最新文档?

I compiled opencv from svn repository (revision 7239) on a Debian wheezy/sid environment.我在 Debian wheezy/sid 环境中从 svn 存储库(修订版 7239)编译了 opencv。

Fell free to suggest alternative methods/ideas to solve this problem.随意提出解决这个问题的替代方法/想法。

Thanks in advance.提前致谢。

Both cv.GetSubRect and ROI functions are available in Python, but in old import cv mode or import cv2.cv . cv.GetSubRect 和 ROI 函数在 Python 中都可用,但在旧的import cv模式或import cv2.cv ie use cv2.cv.GetSubRect() or cv2.cv.SetImageROI if you are familier with them.即如果您熟悉它们,请使用cv2.cv.GetSubRect()cv2.cv.SetImageROI

On the other hand, it is simple to set ROI without these functions due to numpy integration in new cv2.另一方面,由于新 cv2 中的 numpy 集成,在没有这些函数的情况下设置 ROI 很简单。

If (x1,y1) and (x2,y2) are the two opposite vertices of plate you obtained, then simply use function:如果 (x1,y1) 和 (x2,y2) 是您获得的板的两个相对顶点,则只需使用函数:

roi = gray[y1:y2, x1:x2]

that is your image ROI.这就是您的图像投资回报率。

So choose whatever suit you.所以选择适合你的。

Here's a visualization for cropping a ROI from an image这是从图像中裁剪 ROI 的可视化

-------------------------------------------
|                                         | 
|    (x1, y1)                             |
|      ------------------------           |
|      |                      |           |
|      |                      |           | 
|      |         ROI          |           |  
|      |                      |           |   
|      |                      |           |   
|      |                      |           |       
|      ------------------------           |   
|                           (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.(0,0)视为图像的左上角,从左到右作为 x 方向,从上到下作为 y 方向。 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:如果我们将(x1,y1)作为 ROI 的左上角和(x2,y2)作为 ROI 的右下角顶点,我们可以使用 Numpy 切片来裁剪图像:

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

But normally we will not have the bottom-right vertex.但通常我们不会有右下角的顶点。 In typical cases, we will be iterating through contours where the rectangular ROI coordinates can be found with cv2.boundingRect() .在典型情况下,我们将遍历轮廓,其中可以使用cv2.boundingRect()找到矩形 ROI 坐标。 Additionally, if we wanted to save multiple ROIs, we could keep a counter此外,如果我们想保存多个 ROI,我们可以保留一个计数器

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

ROI_number = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    ROI = image[y:y+h, x:x+w]
    cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
    ROI_number += 1

Since OpenCV v2.2, Numpy arrays are naively used to display images.从 OpenCV v2.2 开始,Numpy 数组被天真地用于显示图像。 This Numpy slicing method to extract the ROI may not work with older versions这种用于提取 ROI 的 Numpy 切片方法可能不适用于旧版本

Example: If you have few points, and want to copy region contains its示例:如果您有几个点,并且想要复制包含其的区域

r = cv2.boundingRect(pts)
cv2.imwrite('roi.png', im[r[0]:r[0]+r[2], r[1]:r[1]+r[3]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM