代码如下:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main()
{
// 读入一张图片(poyanghu缩小图)
Mat img = imread("D:\\faceDetection.png",0);
// 创建一个名为 "图片"窗口
namedWindow("图片");
// 在窗口中显示图片
imshow("图片", img);
// 等待6000 ms后窗口自动关闭
waitKey(6000);
return 0;
}
函数如下:
@param winname Name of the window in the window caption that may be used as a window identifier.
@param winname 窗口标题中的窗口名称,可用作窗口标识符。
@param flags Flags of the window. The supported flags are: (cv::WindowFlags)
@param flags 窗口的标志。 支持的标志是:(cv::WindowFlags)
CV_EXPORTS_W void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);
@brief Creates a window.
@brief 创建一个窗口。
The function namedWindow creates a window that can be used as a placeholder for images and trackbars. Created windows are referred to by their names.
namedWindow 函数创建一个窗口,可以用作图像和轨迹栏的占位符。 创建的窗口由它们的名称引用。
If a window with the same name already exists, the function does nothing.
如果已存在同名窗口,则该函数不执行任何操作。
You can call cv::destroyWindow or cv::destroyAllWindows to close the window and de-allocate any associated memory usage. For a simple program, you do not really have to call these functions because all the resources and windows of the application are closed automatically by the operating system upon exit.
您可以调用 cv::destroyWindow 或 cv::destroyAllWindows 来关闭窗口并取消分配任何相关的内存使用。 对于一个简单的程序,您实际上不必调用这些函数,因为应用程序的所有资源和窗口在退出时都会由操作系统自动关闭。
@note
Qt backend supports additional flags:
Qt 后端支持额外的标志:
- WINDOW_NORMAL or WINDOW_AUTOSIZE: WINDOW_NORMAL enables you to resize the window, whereas WINDOW_AUTOSIZE adjusts automatically the window size to fit the displayed image (see imshow ), and you cannot change the window size manually.
- WINDOW_NORMAL 或 WINDOW_AUTOSIZE: WINDOW_NORMAL 允许您调整窗口大小,而 WINDOW_AUTOSIZE 自动调整窗口大小以适应显示的图像(请参阅 imshow ),您不能手动更改窗口大小。
- WINDOW_FREERATIO or WINDOW_KEEPRATIO: WINDOW_FREERATIO adjusts the image with no respect to its ratio, whereas WINDOW_KEEPRATIO keeps the image ratio.
- WINDOW_FREERATIO 或 WINDOW_KEEPRATIO: WINDOW_FREERATIO 调整图像而不考虑其比例,而 WINDOW_KEEPRATIO 保持图像比例。
- WINDOW_GUI_NORMAL or WINDOW_GUI_EXPANDED: WINDOW_GUI_NORMAL is the old way to draw the window without statusbar and toolbar, whereas WINDOW_GUI_EXPANDED is a new enhanced GUI.
- WINDOW_GUI_NORMAL 或 WINDOW_GUI_EXPANDED: WINDOW_GUI_NORMAL 是绘制没有状态栏和工具栏的窗口的旧方法,而 WINDOW_GUI_EXPANDED 是一种新的增强型 GUI。
By default, flags == WINDOW_AUTOSIZE | WINDOW_KEEPRATIO | WINDOW_GUI_EXPANDED
默认情况下,标志 == WINDOW_AUTOSIZE | WINDOW_KEEPRATIO | WINDOW_GUI_EXPANDED
enum WindowFlags {
WINDOW_NORMAL = 0x00000000, //!< 用户可以调整窗口大小(无约束)/也用于将全屏窗口切换到正常大小。
WINDOW_AUTOSIZE = 0x00000001, //!< 用户无法调整窗口大小,大小受显示图像的限制。
WINDOW_OPENGL = 0x00001000, //!< 支持 opengl 的窗口。
WINDOW_FULLSCREEN = 1, //!< 将窗口更改为全屏。
WINDOW_FREERATIO = 0x00000100, //!< 图像尽可能多地消耗(无比例限制)。
WINDOW_KEEPRATIO = 0x00000000, //!< 尊重图像的比例。
WINDOW_GUI_EXPANDED=0x00000000, //!< 状态栏和工具栏
WINDOW_GUI_NORMAL = 0x00000010, //!< 老方式
};