Bak
Bak

Reputation: 365

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?

Upvotes: 17

Views: 64547

Answers (6)

Ajani Timothy
Ajani Timothy

Reputation: 224

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

Upvotes: 0

user1245262
user1245262

Reputation: 7505

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)

Upvotes: 1

Shahrukh khan
Shahrukh khan

Reputation: 382

Simply write

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

and then manually resize it to your desired size

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

Upvotes: 18

Blutack
Blutack

Reputation: 353

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.

Upvotes: -2

rajkn90
rajkn90

Reputation: 189

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

Upvotes: 5

Alexey
Alexey

Reputation: 5978

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)

Upvotes: 19

Related Questions