简体   繁体   中英

How to resize window in opencv2 python

I am using opencv 2 with a webcam. I can get the video stream and process it, but I can't seem to figure out a way to resize the display window. I have some video images stacked horizontally, but the image dimension is very small that it's difficult to see things.

My code is pretty simple, and along the lines of this:

cv2.namedWindow("main")

....

result = np.hstack((res2, foreground))
result = np.hstack((ff, result))

cv2.imshow("main", result)
cv2.waitKey(20)

The opencv documentation states:

namedWindow
flags – Flags of the window. Currently the only supported flag is CV_WINDOW_AUTOSIZE . If this is set, the window size is automatically adjusted to fit the displayed image (see imshow() ), and you cannot change the window size manually.

But qt backends apparently have extra flags. I don't have a qt backend. Is there a way for me to increase the size of the images so that I can see them?

Yes, unfortunately you can't manually resize a nameWindow window without Qt backend. Your options:

  • use cv2.resize function to resize the image to desired size prior to displaying the image
  • install OpenCV with Qt backend support and use cv2.namedWindow("main", CV_WINDOW_NORMAL)

Simply write

cv2.namedWindow("main", cv2.WINDOW_NORMAL)

and then manually resize it to your desired size

cv2.resizeWindow('image', 900, 900) 

You can use the WINDOW_NORMAL flag when calling the namedWindow function as shown below. This will allow you to resize your window.

namedWindow("Image", WINDOW_NORMAL);

Check the namedWindow function documented here

What worked for me was to resize the image instead of the window (I never did manage to get window resizing to work):

import cv2

img = cv2.imread('your_image.jpg')
res = cv2.resize(img, dsize=(500,500), interpolation=cv2.INTER_CUBIC)
cv2.namedWindow("Resized", cv2.WINDOW_NORMAL)
cv2.imshow("Resized", res)

It has to with the resolution of pc camera run this code to confirm

cap = cv2.VideoCapture(0)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(width, height)

most likely its 640 x 480 px. You might have to reduce the size of the image to fit the screen

As per the docs, you need to create your window with autosize true.

cv2.namedWindow("main", cv2.WINDOW_AUTOSIZE)

Your call to imshow will then automatically resize the window to fit your image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM