Loading an Image
Before we can display an image in our window, we'll need to know how to load an image
from disk. The function for this is cvLoadImage():
IplImage* cvLoadImage(
const char* filename,
int iscolor = CV_LOAD_IMAGE_COLOR
);When opening an image, cvLoadImage() does not look at
the file extension. Instead, cvLoadImage() analyzes the
first few bytes of the file (aka its signature or
"magic sequence") and determines the appropriate codec using that. The second argument
iscolor can be set to one of several values. By
default, images are loaded as three-channel images with 8 bits per channel; the optional
flag CV_LOAD_IMAGE_ANYDEPTH can be added to allow loading
of non-8-bit images. By default, the number of channels will be three because the iscolor flag has the default value of CV_LOAD_IMAGE_COLOR. This means that, regardless of the number of channels in
the image file, the image will be converted to three channels if needed. The alternatives to
CV_LOAD_IMAGE_COLOR are CV_LOAD_IMAGE_GRAYSCALE and CV_LOAD_IMAGE_ANYCOLOR. Just as CV_LOAD_IMAGE_COLOR forces any image into a three-channel image, CV_LOAD_IMAGE_GRAYSCALE automatically converts any image into a
single-channel image. CV_LOAD_IMAGE_ANYCOLOR will simply
load the image as it is stored in the file. Thus, to load a 16-bit color image you would use
CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH. If you
want both the color and depth to be loaded exactly "as is", you could instead use the
all-purpose flag CV_LOAD_IMAGE_UNCHANGED ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access