Creating GUIs in OpenCV

HighGUI

As mentioned in 'File Handling', OpenCV comes with a library called highgui that provides easy control of various aspects of image work, and this is especially true of its control over user interfaces. It is rather 'simple' - you create windows and add trackbars, and that's about it - but provides enough functionality to see and control what's going on within your program.

As the name 'highgui' suggests, the user interfaces are kept at a remarkably high level of abstraction and give virtually no control over the internal workings of the interface. By this I mean that windows are not treated as objects but are simply referred to by their name throughout an OpenCV program - you pass the window name to whichever method you're using. Time for an example, I think.

Windows

To create a window, you use cvNamedWindow(char[ ] name, 1). name can be whatever you want, and serves as both the string you refer to the window by as well as the caption that appears on screen. The 1 is simply a numeric flag that presumably gets some use lower down in OpenCV. Currently no other numbers are supported, but I'm guessing they might be once Intel adds more functionality.
To remove a window at any point, and especially to aid garbage removal at the end of a program, you can use cvDestroyWindow(char[ ] name) to remove a particular window or cvDestroyAllWindows() to get rid of all of them.

Moving and resizing of windows is also possible using cvResizeWindow(char[ ] name, int width, int height) and cvMoveWindow(char[ ] name, int x, int y), where width, height, x and y are in pixels.

Trackbars

When experimenting with some of the methods, such as those that perform actions on thresholded pictures, it can be useful to have a way of dynamically changing the numeric variables involved. Trackbars are useful in this context as they not only enable the user to adjust the variables but also to specify a callback method to run after each change. Once again, the window to add the trackbar to is specified by name.

cvCreateTrackbar(char[ ] label, char[ ] name, int& variable, int max, <method> callback) can only be called once a window has been created, as only then will you have a name to refer to the window by. label specifies the label given to the trackbar, which can be useful when you have more than one. name, as before, refers to the window name. variable has to be passed as the address of a previously-declared variable, which can be done by simply adding an ampersand ('&') before it. max specifies the maximum value that the trackbar can take, and is used to calculate the slider's position on the bar. Finally, method is simply the name of a method you wish to call whenever the trackbar's value changes - it can also be null.

Displaying Images

Of course, this is all useless unless you know how to display an image in the window. This is done with cvShowImage(char[ ] name, IplImage* image). As I've said previously on the site, all IplImages should be declared as pointers, as all of the methods in OpenCV use pointers rather than the objects themselves.

Refreshing

There is only one method in highgui that fetches and handles events, and that is cvWaitKey(int delay). delay specifies how long (in milliseconds) the method should wait for before continuing, and if set to zero waits indefinitely. As it is the only event-handling method in highgui, it needs to be called periodically for normal event processing, unless you use it within some environment that takes care of that for you.

As such, if you're using a system that is supposed to update an image and cvShowImage isn't making it happen, inserting a call to cvWaitKey(10) may help.