python 矩形补正方形

boxes结构:x1 y1 x2 y2

f t_h>t_w:
                width = t_h
                x_min=max(0,(boxes[0]+boxes[2])*0.5-width*0.5)
                x_max=min(frame.shape[1],(boxes[0]+boxes[2])*0.5+width*0.5)
                boxes[0],boxes[2]=x_min,x_max

            else:
                width = t_w
                y_min = max(0, (boxes[1] + boxes[3]) * 0.5 - width * 0.5)
                y_max = min(frame.shape[0], (boxes[1] + boxes[3]) * 0.5 + width * 0.5)
                boxes[1], boxes[3] = y_min, y_max

图片补:

 if width>height:
            #top, bottom, left, right
            img = cv2.copyMakeBorder(img, (width-height)//2, (width-height)//2, 0, 0, cv2.BORDER_CONSTANT, value=[255, 255, 255])
            # img = cv2.copyMakeBorder(img, (width-height)//2, (width-height)//2, 0, 0, cv2.BORDER_REPLICATE)
        else:
            img = cv2.copyMakeBorder(img, 0, 0, -(width-height)//2, -(width-height)//2, cv2.BORDER_CONSTANT, value=[255, 255, 255])

 

你可能感兴趣的:(python)