🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 2. 图像显示与GUI OpenCV是计算机视觉库,有了图像处理,当然不可或缺的有图像显示和相应的图形界面接口了。 与GUI有关的函数,有创建窗口、改变窗口大小、创建拖动条、鼠标滚轮响应等等较为全面的函数体系,但是部分需要Qt的环境支持,此处不一一赘述,如有需要请观看手册。 ## 函数 ### 展示图像 ```C++ void cv::imshow ( const String & winname, InputArray mat ) /** @brief Displays an image in the specified window. 简介 在指定的窗口中显示图像。 The function imshow displays an image in the specified window. If the window was created with the cv::WINDOW_AUTOSIZE flag, the image is shown with its original size, however it is still limited by the screen resolution. Otherwise, the image is scaled to fit the window. The function may scale the image, depending on its depth: imshow函数在指定窗口中显示图像。如果窗口是使用cv::window_AUTOSIZE标志创建的,则图像将以其原始大小显示,但仍受屏幕分辨率的限制。否则,将缩放图像以适应窗口。该功能可根据图像的深度缩放图像: - If the image is 8-bit unsigned, it is displayed as is. - If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255\*256] is mapped to [0,255]. - If the image is 32-bit or 64-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255]. -如果图像是8位无符号的,它将按原样显示。 -如果图像是16位无符号或32位整数,则像素除以256。也就是说,值范围[0255\*256]映射到[0255]。 -如果图像是32位或64位浮点,则像素值乘以255。也就是说,值范围[0,1]映射到[0255]。 If window was created with OpenGL support, cv::imshow also support ogl::Buffer , ogl::Texture2D and cuda::GpuMat as input. If the window was not created before this function, it is assumed creating a window with cv::WINDOW_AUTOSIZE. If you need to show an image that is bigger than the screen resolution, you will need to call namedWindow("", WINDOW_NORMAL) before the imshow. 若窗口是使用OpenGL支持创建的,那个么cv::imshow也支持ogl::Buffer、ogl::Texture2D和cuda::GpuMat作为输入。 如果该窗口不是在此函数之前创建的,则假定使用cv::window\u AUTOSIZE创建窗口。 如果需要显示大于屏幕分辨率的图像,则需要在imshow之前调用namedWindow(“,WINDOW_NORMAL”)。 @note This function should be followed by cv::waitKey function which displays the image for specified milliseconds. Otherwise, it won't display the image. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed. (If you put it in a loop to read videos, it will display the video frame-by-frame) @注意:此函数后面应该是cv::waitKey函数,该函数显示指定毫秒的图像。否则,它将不会显示图像。例如,waitKey(0)将无限显示窗口,直到按任意键为止(适用于图像显示)。waitKey(25)将显示一帧25毫秒,之后显示屏将自动关闭(如果将其放入循环中以读取视频,它将逐帧显示视频) @note [__Windows Backend Only__] Pressing Ctrl+C will copy the image to the clipboard. [__Windows Backend Only__] Pressing Ctrl+S will show a dialog to save the image. @注 仅限Windows后端 按Ctrl+C将图像复制到剪贴板。 仅限Windows后端 按Ctrl+S将显示一个保存图像的对话框。 @param winname Name of the window. @param mat Image to be shown. */ ``` ### 键盘响应 ```C++ int cv::waitKey ( int delay = 0 ) /** @brief Waits for a pressed key. 简介 等待一个按下的键。 The function waitKey waits for a key event infinitely (when delay <= 0 or for delay milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is running on your computer at that time. It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed. 函数waitKey无限期地等待一个键事件(当延迟<=0时)或延迟毫秒(当它为正时)。由于操作系统在切换线程之间有一个最小的时间间隔,因此函数不会完全延迟ms,它将至少延迟ms,这取决于当时计算机上运行的其他内容。它返回按下的键的代码,如果在指定时间之前没有按下任何键,则返回-1。 @note This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing unless HighGUI is used within an environment that takes care of event processing. 此函数是HighGUI中唯一可以获取和处理事件的方法,因此需要定期调用此函数以进行正常的事件处理,除非在负责事件处理的环境中使用HighGUI。 The function only works if there is at least one HighGUI window created and the window is active. If there are several HighGUI windows, any of them can be active. 仅当至少创建了一个HighGUI窗口且该窗口处于活动状态时,该函数才起作用。如果有多个HighGUI窗口,则其中任何一个都可以处于活动状态。 @param delay Delay in milliseconds. 0 is the special value that means "forever". */ ```