OpenCV imread 函数 (转)

原文:https://docs.opencv.org/2.4.13.6/modules/highgui/doc/reading_and_writing_images_and_video.html#imread

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:
  • filename – Name of file to be loaded.
  • flags – Flags specifying the color type of a loaded image:
    • CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
    • CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
    • CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
    • >0 Return a 3-channel color image.

      Note

      In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.

    • =0 Return a grayscale image.
    • <0 Return the loaded image as is (with alpha channel).

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;
}

猜你喜欢

转载自blog.csdn.net/u011608180/article/details/85048155