如何从边界框裁剪图像并为每个框创建新图像

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

这是我的头像

我已经使用 this 问题来创建这些框,但我不擅长 python。我现在如何从我创建的 3 个框中创建 3 个图像?我搜索了互联网,但找不到正确的答案。谢谢!

替换下面的代码。我已按照您提供的问题 link 进行操作。

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

这将以.png格式

保存当前程序运行位置的裁剪图像