Multiple crop sections with loop in opencv and mouse clicks

Question:

I have obtained region of interests through mouse clicks with OpenCV as

roi = [[(276, 756), (940, 828), 'text', 'name'],
       [(1572, 764), (2332, 824), 'text', 'cnic'],
       [(1996, 692), (2052, 752), 'box', 'corporate'],
       [(2300, 692), (2356, 756), 'box', 'individual']]

where each tuple is obtained from mouse clicks with OpenCV.

roi[0][0] , which is (276, 756), has (x, y) coordinates, so width for the first field named ‘name’ is 276:940, and height of first field named ‘name’ is 756:828.

How do I use a for loop to extract cropping of the image like img[756:828, 276:940] for all the ROIs?

I found that roi[0][0] is (276, 756) and roi[0][0][0] is 276, but don’t know how to implement one loop for all sections.
I will be sending this data to tesseract (OCR).

Asked By: Jawad Mansoor

||

Answers:

for pt1, pt2, _, _ in roi:
    cropped = img[pt1[1]:pt2[1], pt1[0]:pt2[0]]
Answered By: Burak
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.