OpenCV & Python - Image too big to display

后端 未结 7 1938
猫巷女王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()
    
    0 讨论(0)
  • 2020-12-04 23:47

    In opencv, cv.namedWindow() just creates a window object as you determine, but not resizing the original image. You can use cv2.resize(img, resolution) to solve the problem.

    Here's what it displays, a 740 * 411 resolution image.

    image = cv2.imread("740*411.jpg")
    cv2.imshow("image", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    Here, it displays a 100 * 200 resolution image after resizing. Remember the resolution parameter use column first then is row.

    image = cv2.imread("740*411.jpg")
    image = cv2.resize(image, (200, 100))
    cv2.imshow("image", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    0 讨论(0)
  • 2020-12-04 23:56

    Try with this code:

    from PIL import Image
    
    Image.fromarray(image).show()
    
    0 讨论(0)
  • 2020-12-04 23:59

    Try this:

    image = cv2.imread("img/Demo.jpg")
    image = cv2.resize(image,(240,240))
    

    The image is now resized. Displaying it will render in 240x240.

    0 讨论(0)
  • 2020-12-05 00:08

    Use this for example:

    cv2.namedWindow('finalImg', cv2.WINDOW_NORMAL)
    cv2.imshow("finalImg",finalImg)
    
    0 讨论(0)
  • 2020-12-05 00:08

    Looks like opencv lib is pretty sensitive to parameters passed to the methods. The following code worked for me using opencv 4.3.0:

    win_name = "visualization"  #  1. use var to specify window name everywhere
    cv2.namedWindow(win_name, cv2.WINDOW_NORMAL)  #  2. use 'normal' flag
    img = cv2.imread(filename)
    h,w = img.shape[:2]  #  suits for image containing any amount of channels
    h = int(h / resize_factor)  #  one must compute beforehand
    w = int(w / resize_factor)  #  and convert to INT
    cv2.resizeWindow(win_name, w, h)  #  use variables defined/computed BEFOREHAND
    cv2.imshow(win_name, img)
    
    0 讨论(0)
提交回复
热议问题