cv2imread
cv2.imread, often referred to as cv2.imread in Python, is a function from the OpenCV library used to load an image from disk into a NumPy array. It is part of the OpenCV-Python interface and serves as the primary way to bring image data into a program for processing.
The function reads the file and returns a multi-dimensional NumPy array representing the image data. If the
- filename: the path to the image file.
- flags: optional; determines how the image is read. Common values include:
- cv2.IMREAD_COLOR: loads a color image (default). Any existing alpha channel is discarded.
- cv2.IMREAD_GRAYSCALE: loads the image in grayscale as a single-channel array.
- cv2.IMREAD_UNCHANGED: loads the image as-is, preserving any alpha channel.
- cv2.IMREAD_ANYCOLOR and cv2.IMREAD_ANYDEPTH are also available for broader loading options in some contexts.
- A NumPy array with shape (height, width, channels) for color images (typically 3 channels in BGR
- None if the image cannot be read.
- Always check the result for None before proceeding.
- If color is required in RGB order for display or processing with other libraries, convert with
- Be mindful that OpenCV loads images in BGR by default; many image viewers assume RGB.
See also: cv2.cvtColor, cv2.IMREAD_GRAYSCALE, cv2.IMREAD_UNCHANGED, cv2.imdecode.