Robustly crop rotated bounding box on photos

后端 未结 1 1313
后悔当初
后悔当初 2021-01-03 04:30

I\'m trying to extract the rotated bounding box of contours robustly. I would like to take an image, find the largest contour, get its rotated bounding box, rotate the image

相关标签:
1条回答
  • 2021-01-03 04:47

    After some research, this is what I get:

    This is how I get it:

    • pad the original image on each side (500 pixels in my case)
    • find the four corner points of the shoe (the four points should form a polygon enclosing the shoe, but do not need to be exact rectangle)
    • employing the code here to crop the shoe:
    
    img = cv2.imread("padded_shoe.jpg")
    # four corner points for padded shoe
    cnt = np.array([
        [[313, 794]],
        [[727, 384]],
        [[1604, 1022]],
        [[1304, 1444]]
    ])
    print("shape of cnt: {}".format(cnt.shape))
    rect = cv2.minAreaRect(cnt)
    print("rect: {}".format(rect))
    
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    width = int(rect[1][0])
    height = int(rect[1][1])
    
    src_pts = box.astype("float32")
    dst_pts = np.array([[0, height-1],
                        [0, 0],
                        [width-1, 0],
                        [width-1, height-1]], dtype="float32")
    M = cv2.getPerspectiveTransform(src_pts, dst_pts)
    warped = cv2.warpPerspective(img, M, (width, height))
    
    

    Cheers, hope it helps.

    0 讨论(0)
提交回复
热议问题