OpenCV & Python - Image too big to display

后端 未结 7 2048
猫巷女王i
猫巷女王i 2020-12-04 23:33

I have an image that is 6400 × 3200, while my screen is 1280 x 800. Therefore, the image needs to be resized for display only. I am using Python and OpenCV 2.4.9. According

7条回答
  •  时光说笑
    2020-12-04 23:45

    The other answers perform a fixed (width, height) resize. If you wanted to resize to a specific size while maintaining aspect ratio, use this

    def ResizeWithAspectRatio(image, width=None, height=None, inter=cv2.INTER_AREA):
        dim = None
        (h, w) = image.shape[:2]
    
        if width is None and height is None:
            return image
        if width is None:
            r = height / float(h)
            dim = (int(w * r), height)
        else:
            r = width / float(w)
            dim = (width, int(h * r))
    
        return cv2.resize(image, dim, interpolation=inter)
    

    Example

    image = cv2.imread('img.png')
    resize = ResizeWithAspectRatio(image, width=1280) # Resize by width OR
    # resize = ResizeWithAspectRatio(image, height=1280) # Resize by height 
    
    cv2.imshow('resize', resize)
    cv2.waitKey()
    

提交回复
热议问题