Home > Software design >  Resize imshow output
Resize imshow output

Time:07-23

I have the following code below pasted below. When I run the cv2.imshow('img', image) located at the bottom of the code, the size of the output exceeds my screen. How do I resize the output to an e.g 900 size screen? I have looked at many forums but they don't seem to fix this. Please could someone help

# Import required modules
import cv2
import numpy as np
import os
import glob
    
    
# Define the dimensions of checkerboard
CHECKERBOARD = (9, 6)
    
# stop the iteration when specified
# accuracy, epsilon, is reached or
# specified number of iterations are completed.
criteria = (cv2.TERM_CRITERIA_EPS  
            cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# Vector for 3D points
threedpoints = []

# Vector for 2D points
twodpoints = []

# 3D points real world coordinates
objectp3d = np.zeros((1, CHECKERBOARD[0]
                      * CHECKERBOARD[1],
                      3), np.float32)
objectp3d[0, :, :2] = np.mgrid[0:CHECKERBOARD[0],
                               0:CHECKERBOARD[1]].T.reshape(-1, 2)
prev_img_shape = None

# Extracting path of individual image stored
# in a given directory. Since no path is
# specified, it will take current directory
# jpg files alone
images = glob.glob('*.jpeg')

for filename in images:
    print(filename)
    image = cv2.imread(filename)
    grayColor = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Find the chess board corners
    # If desired number of corners are
    # found in the image then ret = true
    ret, corners = cv2.findChessboardCorners(
                    grayColor, CHECKERBOARD,
                    cv2.CALIB_CB_ADAPTIVE_THRESH
                      cv2.CALIB_CB_FAST_CHECK  
                    cv2.CALIB_CB_NORMALIZE_IMAGE)

    # If desired number of corners can be detected then,
    # refine the pixel coordinates and display
    # them on the images of checker board
    if ret == True:
        threedpoints.append(objectp3d)

        # Refining pixel coordinates
        # for given 2d points.
        corners2 = cv2.cornerSubPix(
            grayColor, corners, (11, 11), (-1, -1), criteria)

        twodpoints.append(corners2)

        # Draw and display the corners
        image = cv2.drawChessboardCorners(image,
                                          CHECKERBOARD,
                                          corners2, ret)

    cv2.imshow('img', image)
    cv2.waitKey(0)

cv2.destroyAllWindows()

CodePudding user response:

This must be correct way to resize output window:

show_width,show_height = 900,900    
cv2.imshow('img', cv2.resize(image,(show_width,show_height)))
cv2.waitKey(0)

CodePudding user response:

You need to call cv.namedWindow() with appropriate flags. Try cv.WINDOW_NORMAL. That's a good default.

Then the window is resizable. Use cv.resizeWindow() to set an exact size.

This does not require resizing the image itself. The GUI handles that.

  • Related