Search code examples
pythonnumpyopencvcropbounding-box

How to make crops from list of lists


I have a list of n lists like this

pixels = [[400, 220, 515, 293], [433, 373, 482, 452], [401, 370, 477, 475]]

Each sublist correspond to the Top Left and Bottom Right Points for each crop

I made this code to extract a single crop from the whole list

  x1 = min(map(lambda x: x[0], pixels))
  y1 = min(map(lambda x: x[1], pixels))
  x2 = max(map(lambda x: x[2], pixels))
  y2 = max(map(lambda x: x[3], pixels))

  crop = img[y1:y2, x1:x2]
 cv2_imshow(crop)

How can I make n crops from n sublists?


Solution

  • Hope It will work

    import cv2
    pixels = [[400, 220, 515, 293], [433, 373, 482, 452], [401, 370, 477, 475]]
    for i in pixels:
        img = cv2.imread(r"img _path")
        x1,y1,x2,y2 = i
        crop = img[y1:y2, x1:x2]