Loads an image from a file.
C++:
Mat imread
(const string& filename, int flags=1 )
Python:
cv2.
imread
(filename[, flags]) → retval
C:
IplImage* cvLoadImage
(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR )
C:
CvMat* cvLoadImageM
(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR )
Python:
cv.
LoadImage
(filename, iscolor=CV_LOAD_IMAGE_COLOR) → None
Python:
cv.
LoadImageM
(filename, iscolor=CV_LOAD_IMAGE_COLOR) → None
Parameters: |
|
---|
The function imread
loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL
). Currently, the following file formats are supported:
- Windows bitmaps -
*.bmp, *.dib
(always supported)- JPEG files -
*.jpeg, *.jpg, *.jpe
(see the Notes section)- JPEG 2000 files -
*.jp2
(see the Notes section)- Portable Network Graphics -
*.png
(see the Notes section)- Portable image format -
*.pbm, *.pgm, *.ppm
(always supported)- Sun rasters -
*.sr, *.ras
(always supported)- TIFF files -
*.tiff, *.tif
(see the Notes section)
Note
- The function determines the type of an image by the content, not by the file extension.
- On Microsoft Windows* OS and MacOSX*, the codecs shipped with an OpenCV image (libjpeg, libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs, and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware that currently these native image loaders give images with different pixel values because of the color management embedded into MacOSX.
- On Linux*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for codecs supplied with an OS image. Install the relevant packages (do not forget the development files, for example, “libjpeg-dev”, in Debian* and Ubuntu*) to get the codec support or turn on the
OPENCV_BUILD_3RDPARTY_LIBS
flag in CMake.
Note
In the case of color images, the decoded images will have the channels stored in B G R
order.
Code example (C++):
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, char **argv) {
Mat src = imread("/home/abb/Downloads/red_left_00045.jpg",IMREAD_COLOR);
if(src.empty()){
cout << "Image is empty"<<endl;
return -1;
}
cout << "image width * hight = " << src.cols << " * " << src.rows << endl;
return 0;
}