Dimitar
Dimitar

Reputation: 1928

How do you crop an image from bounding box and create new image for each box

This is my image book image

I have used this question to create those boxes, but I am not good with python. How do I now create 3 images from the 3 boxes that i have created? I searched the internet and i can't find the right answer. Thanks!

Upvotes: 1

Views: 961

Answers (1)

shaik moeed
shaik moeed

Reputation: 5785

Replace the below code. I have followed the link you have provided in question.

var=1
for contour in contours:
    convex_contour = cv2.convexHull(contour)
    area = cv2.contourArea(convex_contour)
    if area > AREA_THRESHOLD:
        cv2.drawContours(img, [convex_contour], -1, (255,0,0), 3)

        # get rectangle bounding contour
        [x,y,w,h] = cv2.boundingRect(contour)
        crop_img = img[y:y+h, x:x+w]
        cv2.imwrite("crop"+str(var)+".png", crop_img)
        var+=1

This will save the cropped images in current program running location in .png format

Upvotes: 1

Related Questions