Reading images using OpenCV
Now that we learned all about the Mat
class in OpenCV, we can move on to learn to read images and fill a Mat
class with an image to further process it. As you have seen briefly in the previous chapters, the imread
function can be used to read images from the disk. Here's an example:
Mat image = imread("c:/dev/test.jpg", IMREAD_GRAYSCALE | IMREAD_IGNORE_ORIENTATION);
imread
simply takes a C++ std::string
class as the first parameter and an ImreadModes
flag as the second parameter. If for any reason the image cannot be read, then it returns an empty Mat
class (data == NULL
), otherwise, it returns a Mat
class filled with the image pixels with the type and color specified in the second parameter. Depending on the availability of some image types in the platform, imread
can read the following image types:
- Windows bitmaps:
\*.bmp
,\*.dib
- JPEG files:
\*.jpeg
,\*.jpg
,\*.jpe
- JPEG 2000 files:
\*.jp2
- Portable Network Graphics:
\*.png
- WebP:
\*.webp
- Portable image format...