Working With Files

HighGUI

OpenCV comes with its own library (highgui) that enables easy control of several aspects of image work within a program. Its GUI methods are covered here, but before displaying an image you might like to know how to load some you 'prepared earlier' and save those that you alter.

As the name 'highgui' suggests, the methods are kept at a remarkably high level of abstraction and give virtually no control over the internal workings of the interface. For example, you don't specify the file type - this is assumed to be represented by the file's extension.

Reading Files

Loading images couldn't be simpler. Be advised that the method here returns a pointer to an IplImage, so you'll need to define one first. Assuming you haven't, this would work:
IplImage *img = cvLoadImage(char[ ] filename, int type);
The filename can also include a path if the image is not in the same folder. type refers to whether or not the image is colour, and can be entered as an integer or a constant, depending on personal preference:
CV_LOAD_IMAGE_COLOR = 1 forces the resultant IplImage to be colour.
CV_LOAD_IMAGE_GRAYSCALE = 0 forces a greyscale IplImage.
CV_LOAD_IMAGE_UNCHANGED = -1 generates a file-dependent represenation. If the file is colour, you get a 3-channel IplImage. If not, you get grayscale.

Writing Files

This is just as simple as before, but without the complication of a type.
cvSaveImage(char[ ] filename, IplImage* img) saves img as filename, again using the extension specified to determine the filetype.