17

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?

0

6 Answers 6

19

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)
Sign up to request clarification or add additional context in comments.

4 Comments

how did you use resize?
cv2.resize(image, (0, 0), fx=0.5, fy=0.5) will resize both axes by half while cv2.resize(image, (100, 50)) will resize to have width 100 and height 50 pixels.
How to add an image to the window?
How can we check whether we have Qt backend?
18

Simply write

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

and then manually resize it to your desired size

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

1 Comment

and in the next line: cv2.resizeWindow('image', 900, 900)
5

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

1 Comment

Doesn't work for me on Windows 11
1

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)

Comments

0

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

Comments

-3

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.

1 Comment

The option WINDOW_AUTOSIZE is as default and sets the size of the window to the actual fixed size of the image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.