简体   繁体   中英

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

This is my 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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