Page 33 -
P. 33
Chapter 1 ■ Practical Aspects of a Vision System 7
returns a 1 channel (1 byte per pixel) grey-level image if f=0, and returns the
actual image type that is found in the file if f<0.
Writing an image to a file can be simple or complex, depending on what the
user wants to accomplish. Writing grey-level or RGB color images is simple,
using the code:
k = cvSaveImage( filename, img );
The filename is, as usual, a string indicating the name of the file to be saved,
and the img variable is the image to be written to that file. The file type will
correspond to the suffix on the file, so if the filename is file.jpg, then the file
format will be JPEG. If the file cannot be written, then the function returns 0.
1.2.3 Image Display
If the basic C/C++ compiler is used alone, then displaying an image is quite
involved. One of the big advantages in using OpenCV is that it provides easy
ways to call functions that open a window and display images within it. This
does not require the use of other systems, such as Tcl/Tk or Java, and asks
the programmer to have only a basic knowledge of the underlying system for
managing windows on their computer.
The user interface functions of OpenCV are collected into a library named
highgui, and are documented on the Internet and in books. The basics are as
follows: a window is created using the cvNamedWindowfunction, which specifies
a name for the window. All windows are referred to by their name and not
through pointers. When created, the window can be given the autosize
property or not. Following this, the function cvShowImage can be used to
display an image (as specified by an IplImage pointer) in an existing window.
For windows with the autosize property, the window will change size to fit
the image; otherwise, the image will be scaled to fit the window.
Whenever cvShowimage is called, the image passed as a parameter is dis-
played in the given window. In this way, consecutive parts of the processing
of an image can be displayed, and simple animations can be created and
displayed. After a window has been created, it can be moved to any position
on the screen using cvMoveWindow (name, x, y). It can also be moved using
the mouse, just like any other window.
1.2.4 An Example
It is now possible to write a simple OpenCV program that will read, process,
and display an image. The input image will be that of Figure 1.1, and the goal
will be to threshold it.