how to add border around an image in opencv python

后端 未结 4 2102
清歌不尽
清歌不尽 2020-12-09 15:06

If I have an image like below, how can I add border all around the image such that the overall height and width of the final image increases but the height and width of the

4条回答
  •  攒了一身酷
    2020-12-09 15:31

    Try This:

    import cv2
    import numpy as np     
    
    img=cv2.imread("img_src.jpg")
    h,w=img.shape[0:2]
    
    base_size=h+20,w+20,3
    # make a 3 channel image for base which is slightly larger than target img
    base=np.zeros(base_size,dtype=np.uint8)
    cv2.rectangle(base,(0,0),(w+20,h+20),(255,255,255),30) # really thick white rectangle
    base[10:h+10,10:w+10]=img # this works
    

提交回复
热议问题