如何在opencv2 python中调整窗口大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16815194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:44:11  来源:igfitidea点击:

How to resize window in opencv2 python

pythonopencv

提问by Bak

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.

我正在使用带有网络摄像头的 opencv 2。我可以获取视频流并对其进行处理,但我似乎无法找到一种调整显示窗口大小的方法。我有一些水平堆叠的视频图像,但图像尺寸很小,很难看清东西。

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 documentationstates:

OpenCV的文件中指出:

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?

但是 qt 后端显然有额外的标志。我没有qt后端。有没有办法让我增加图像的大小以便我可以看到它们?

采纳答案by Alexey

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

是的,不幸的是,如果nameWindow没有 Qt 后端,您无法手动调整窗口大小。您的选择:

  • use cv2.resizefunction 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)
  • cv2.resize在显示图像之前使用函数将图像调整为所需的大小
  • 安装带有 Qt 后端支持的 OpenCV 并使用 cv2.namedWindow("main", CV_WINDOW_NORMAL)

回答by Blutack

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

根据文档,您需要使用 autosize true 创建窗口。

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

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

您对 imshow 的调用将自动调整窗口大小以适合您的图像。

回答by rajkn90

You can use the WINDOW_NORMALflag when calling the namedWindowfunction as shown below. This will allow you to resize your window.

您可以WINDOW_NORMAL在调用namedWindow函数时使用该标志,如下所示。这将允许您调整窗口大小。

namedWindow("Image", WINDOW_NORMAL);

Check the namedWindowfunction documented here

检查此处namedWindow记录的功能

回答by Shahrukh khan

Simply write

简单地写

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

and then manually resize it to your desired size

然后手动将其调整为您想要的大小