High-level GUI
Overview
While OpenCV was designed for use in full-scale applications and can be used within functionally rich UI frameworks (such as Qt*, WinForms*, or Cocoa*) or without any UI at all, sometimes there it is required to try functionality quickly and visualize the results. More…
// typedefs typedef void (*cv::ButtonCallback)( int state, void *userdata ); typedef void (*cv::MouseCallback)( int event, int x, int y, int flags, void *userdata ); typedef void (*cv::OpenGlDrawCallback)(void *userdata); typedef void (*cv::TrackbarCallback)( int pos, void *userdata ); // enums enum cv::MouseEventFlags; enum cv::MouseEventTypes; enum cv::QtButtonTypes; enum cv::QtFontStyles; enum cv::QtFontWeights; enum cv::WindowFlags; enum cv::WindowPropertyFlags; // global functions int cv::createTrackbar( const String& trackbarname, const String& winname, int* value, int count, TrackbarCallback onChange = 0, void* userdata = 0 ); void cv::destroyAllWindows(); void cv::destroyWindow(const String& winname); int cv::getMouseWheelDelta(int flags); int cv::getTrackbarPos( const String& trackbarname, const String& winname ); double cv::getWindowProperty( const String& winname, int prop_id ); void cv::imshow( const String& winname, InputArray mat ); void cv::moveWindow( const String& winname, int x, int y ); void cv::namedWindow( const String& winname, int flags = WINDOW_AUTOSIZE ); void cv::resizeWindow( const String& winname, int width, int height ); Rect cv::selectROI( const String& windowName, InputArray img, bool showCrosshair = true, bool fromCenter = false ); Rect cv::selectROI( InputArray img, bool showCrosshair = true, bool fromCenter = false ); void cv::selectROIs( const String& windowName, InputArray img, std::vector<Rect>& boundingBoxes, bool showCrosshair = true, bool fromCenter = false ); void cv::setMouseCallback( const String& winname, MouseCallback onMouse, void* userdata = 0 ); void cv::setTrackbarMax( const String& trackbarname, const String& winname, int maxval ); void cv::setTrackbarMin( const String& trackbarname, const String& winname, int minval ); void cv::setTrackbarPos( const String& trackbarname, const String& winname, int pos ); void cv::setWindowProperty( const String& winname, int prop_id, double prop_value ); void cv::setWindowTitle( const String& winname, const String& title ); int cv::startWindowThread(); int cv::waitKey(int delay = 0); int cv::waitKeyEx(int delay = 0);
Detailed Documentation
While OpenCV was designed for use in full-scale applications and can be used within functionally rich UI frameworks (such as Qt*, WinForms*, or Cocoa*) or without any UI at all, sometimes there it is required to try functionality quickly and visualize the results. This is what the HighGUI module has been designed for.
It provides easy interface to:
- Create and manipulate windows that can display images and “remember” their content (no need to handle repaint events from OS).
- Add trackbars to the windows, handle simple mouse events as well as keyboard commands.
Typedefs
typedef void (*cv::ButtonCallback)( int state, void *userdata )
Callback function for a button created by cv::createButton.
Parameters:
state | current state of the button. It could be -1 for a push button, 0 or 1 for a check/radio box button. |
userdata | The optional parameter. |
typedef void (*cv::MouseCallback)( int event, int x, int y, int flags, void *userdata )
Callback function for mouse events. see cv::setMouseCallback.
Parameters:
event | one of the cv::MouseEventTypes constants. |
x | The x-coordinate of the mouse event. |
y | The y-coordinate of the mouse event. |
flags | one of the cv::MouseEventFlags constants. |
userdata | The optional parameter. |
typedef void (*cv::OpenGlDrawCallback)(void *userdata)
Callback function defined to be called every frame. See cv::setOpenGlDrawCallback.
Parameters:
userdata | The optional parameter. |
typedef void (*cv::TrackbarCallback)( int pos, void *userdata )
Callback function for Trackbar see cv::createTrackbar.
Parameters:
pos | current position of the specified trackbar. |
userdata | The optional parameter. |
Global Functions
int cv::createTrackbar( const String& trackbarname, const String& winname, int* value, int count, TrackbarCallback onChange = 0, void* userdata = 0 )
Creates a trackbar and attaches it to the specified window.
The function createTrackbar creates a trackbar (a slider or range control) with the specified name and range, assigns a variable value to be a position synchronized with the trackbar and specifies the callback function onChange to be called on the trackbar position change. The created trackbar is displayed in the specified window winname.
[Qt Backend Only] winname can be empty (or NULL) if the trackbar should be attached to the control panel.
Clicking the label of each trackbar enables editing the trackbar values manually.
Parameters:
trackbarname | Name of the created trackbar. |
winname | Name of the window that will be used as a parent of the created trackbar. |
value | Optional pointer to an integer variable whose value reflects the position of the slider. Upon creation, the slider position is defined by this variable. |
count | Maximal position of the slider. The minimal position is always 0. |
onChange | Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only value is updated. |
userdata | User data that is passed as is to the callback. It can be used to handle trackbar events without using global variables. |
void cv::destroyAllWindows()
Destroys all of the HighGUI windows.
The function destroyAllWindows destroys all of the opened HighGUI windows.
void cv::destroyWindow(const String& winname)
Destroys the specified window.
The function destroyWindow destroys the window with the given name.
Parameters:
winname | Name of the window to be destroyed. |
int cv::getMouseWheelDelta(int flags)
Gets the mouse-wheel motion delta, when handling mouse-wheel events cv::EVENT_MOUSEWHEEL and cv::EVENT_MOUSEHWHEEL.
For regular mice with a scroll-wheel, delta will be a multiple of 120. The value 120 corresponds to a one notch rotation of the wheel or the threshold for action to be taken and one such action should occur for each delta. Some high-precision mice with higher-resolution freely-rotating wheels may generate smaller values.
For cv::EVENT_MOUSEWHEEL positive and negative values mean forward and backward scrolling, respectively. For cv::EVENT_MOUSEHWHEEL, where available, positive and negative values mean right and left scrolling, respectively.
With the C API, the macro CV_GET_WHEEL_DELTA(flags) can be used alternatively.
Mouse-wheel events are currently supported only on Windows.
Parameters:
flags | The mouse callback flags parameter. |
int cv::getTrackbarPos( const String& trackbarname, const String& winname )
Returns the trackbar position.
The function returns the current position of the specified trackbar.
[Qt Backend Only] winname can be empty (or NULL) if the trackbar is attached to the control panel.
Parameters:
trackbarname | Name of the trackbar. |
winname | Name of the window that is the parent of the trackbar. |
double cv::getWindowProperty( const String& winname, int prop_id )
Provides parameters of a window.
The function getWindowProperty returns properties of a window.
Parameters:
winname | Name of the window. |
prop_id | Window property to retrieve. The following operation flags are available: (cv::WindowPropertyFlags) |
See also:
void cv::imshow( const String& winname, InputArray mat )
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:
- 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 floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].
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.
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)
[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.
Parameters:
winname | Name of the window. |
mat | Image to be shown. |
void cv::moveWindow( const String& winname, int x, int y )
Moves window to the specified position.
Parameters:
winname | Name of the window. |
x | The new x-coordinate of the window. |
y | The new y-coordinate of the window. |
void cv::namedWindow( const String& winname, int flags = WINDOW_AUTOSIZE )
Creates a window.
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.
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.
Qt backend supports additional flags:
- 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_FREERATIO or WINDOW_KEEPRATIO: WINDOW_FREERATIO adjusts the image with no respect to its ratio, whereas WINDOW_KEEPRATIO keeps the image ratio.
- 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. By default, flags == WINDOW_AUTOSIZE | WINDOW_KEEPRATIO | WINDOW_GUI_EXPANDED
Parameters:
winname | Name of the window in the window caption that may be used as a window identifier. |
flags | Flags of the window. The supported flags are: (cv::WindowFlags) |
void cv::resizeWindow( const String& winname, int width, int height )
Resizes window to the specified size.
- The specified window size is for the image area. Toolbars are not counted.
- Only windows created without cv::WINDOW_AUTOSIZE flag can be resized.
Parameters:
winname | Window name. |
width | The new window width. |
height | The new window height. |
Rect cv::selectROI( const String& windowName, InputArray img, bool showCrosshair = true, bool fromCenter = false )
Selects ROI on the given image. Function creates a window and allows user to select a ROI using mouse. Controls: use space
or enter
to finish selection, use key c
to cancel selection (function will return the zero cv::Rect).
The function sets it’s own mouse callback for specified window using cv::setMouseCallback (windowName, …). After finish of work an empty callback will be set for the used window.
Parameters:
windowName | name of the window where selection process will be shown. |
img | image to select a ROI. |
showCrosshair | if true crosshair of selection rectangle will be shown. |
fromCenter | if true center of selection will match initial mouse position. In opposite case a corner of selection rectangle will correspont to the initial mouse position. |
Returns:
selected ROI or empty rect if selection canceled.
Rect cv::selectROI( InputArray img, bool showCrosshair = true, bool fromCenter = false )
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
void cv::selectROIs( const String& windowName, InputArray img, std::vector<Rect>& boundingBoxes, bool showCrosshair = true, bool fromCenter = false )
Selects ROIs on the given image. Function creates a window and allows user to select a ROIs using mouse. Controls: use space
or enter
to finish current selection and start a new one, use esc
to terminate multiple ROI selection process.
The function sets it’s own mouse callback for specified window using cv::setMouseCallback (windowName, …). After finish of work an empty callback will be set for the used window.
Parameters:
windowName | name of the window where selection process will be shown. |
img | image to select a ROI. |
boundingBoxes | selected ROIs. |
showCrosshair | if true crosshair of selection rectangle will be shown. |
fromCenter | if true center of selection will match initial mouse position. In opposite case a corner of selection rectangle will correspont to the initial mouse position. |
void cv::setMouseCallback( const String& winname, MouseCallback onMouse, void* userdata = 0 )
Sets mouse handler for the specified window.
Parameters:
winname | Name of the window. |
onMouse | Mouse callback. See OpenCV samples, such as https://github.com/opencv/opencv/tree/master/samples/cpp/ffilldemo.cpp, on how to specify and use the callback. |
userdata | The optional parameter passed to the callback. |
void cv::setTrackbarMax( const String& trackbarname, const String& winname, int maxval )
Sets the trackbar maximum position.
The function sets the maximum position of the specified trackbar in the specified window.
[Qt Backend Only] winname can be empty (or NULL) if the trackbar is attached to the control panel.
Parameters:
trackbarname | Name of the trackbar. |
winname | Name of the window that is the parent of trackbar. |
maxval | New maximum position. |
void cv::setTrackbarMin( const String& trackbarname, const String& winname, int minval )
Sets the trackbar minimum position.
The function sets the minimum position of the specified trackbar in the specified window.
[Qt Backend Only] winname can be empty (or NULL) if the trackbar is attached to the control panel.
Parameters:
trackbarname | Name of the trackbar. |
winname | Name of the window that is the parent of trackbar. |
minval | New maximum position. |
void cv::setTrackbarPos( const String& trackbarname, const String& winname, int pos )
Sets the trackbar position.
The function sets the position of the specified trackbar in the specified window.
[Qt Backend Only] winname can be empty (or NULL) if the trackbar is attached to the control panel.
Parameters:
trackbarname | Name of the trackbar. |
winname | Name of the window that is the parent of trackbar. |
pos | New position. |
void cv::setWindowProperty( const String& winname, int prop_id, double prop_value )
Changes parameters of a window dynamically.
The function setWindowProperty enables changing properties of a window.
Parameters:
winname | Name of the window. |
prop_id | Window property to edit. The supported operation flags are: (cv::WindowPropertyFlags) |
prop_value | New value of the window property. The supported flags are: (cv::WindowFlags) |
void cv::setWindowTitle( const String& winname, const String& title )
Updates window title.
Parameters:
winname | Name of the window. |
title | New title. |
int cv::waitKey(int delay = 0)
Waits for a pressed key.
The function waitKey waits for a key event infinitely (when \(\texttt{delay}\leq 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.
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.
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.
Parameters:
delay | Delay in milliseconds. 0 is the special value that means “forever”. |
int cv::waitKeyEx(int delay = 0)
Similar to waitKey, but returns full key code.
Key code is implementation specific and depends on used backend: QT/GTK/Win32/etc