Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/.

I implemented the plate location using opencv in python, using "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).

I found the GetSubRect() function to copy or isolate part of the image but it does not appear to be available in python. Is there an alternative? The ROI functions do not seem to be implemented either.

Is there an up-to-date documentation of the python interface to opencv?

I compiled opencv from svn repository (revision 7239) on a Debian wheezy/sid environment.

Fell free to suggest alternative methods/ideas to solve this problem.

Thanks in advance.

like image 884
curumim Avatar asked Jan 31 '12 18:01

curumim


People also ask

How do I duplicate an image in OpenCV?

If you use cv2 , correct method is to use . copy() method in Numpy. It will create a copy of the array you need. Otherwise it will produce only a view of that object.

How do we pick up piece of an image or a region of interest in OpenCV using Python?

Python OpenCV – selectroi() Function With this method, we can select a range of interest in an image manually by selecting the area on the image. Parameter: window_name: name of the window where selection process will be shown. source image: image to select a ROI.

How do I select part of an image in Python?

Using PIL and im. crop(box) usually works, see pythonware.com/library/pil/handbook/introduction.htm can you post some more code that showcase what you are doing?


2 Answers

Both cv.GetSubRect and ROI functions are available in Python, but in old import cv mode or import cv2.cv. ie use cv2.cv.GetSubRect() or cv2.cv.SetImageROI if you are familier with them.

On the other hand, it is simple to set ROI without these functions due to numpy integration in new cv2.

If (x1,y1) and (x2,y2) are the two opposite vertices of plate you obtained, then simply use function:

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

that is your image ROI.

So choose whatever suit you.

like image 194
Abid Rahman K Avatar answered Oct 09 '22 21:10

Abid Rahman K


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

------------------------------------------- |                                         |  |    (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. 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 be iterating through contours where the rectangular ROI coordinates can be found with cv2.boundingRect(). Additionally, if we wanted to save multiple ROIs, we could keep a counter

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. This Numpy slicing method to extract the ROI may not work with older versions

like image 36
nathancy Avatar answered Oct 09 '22 22:10

nathancy