Working with images
Let's get started with the basics of OpenCV's Python API. All the scripts we will write and run will use the OpenCV library, which must be imported with the import cv2
line. We will import few more libraries as required, and in the next sections and chapters, cv2.imread()
will be used to import an image. It takes two arguments. The first argument is the image filename. The image should be in the same directory where the Python script is the absolute path that should be provided to cv2.imread()
. It reads images and saves them as NumPy arrays.
The second argument is a flag that specifies that the mode image should be read. The flag can have the following values:
cv2.IMREAD_COLOR
: This loads a color image; it is the default flagcv2.IMREAD_GRAYSCALE
: This loads an image in the grayscale modecv2.IMREAD_UNCHANGED
: This loads an image as it includes an alpha channel
The numeric values of the preceding flags are 1, 0, and -1, respectively.
Take a look at the following code:
import...