Contents Show
You can read image into a numpy array using opencv library. The array contains pixel level data. And as per the requirement, you may modify the data of the image at a pixel level by updating the array values. To read an image in Python using OpenCV, use cv2.imread() function. imread() returns a 2D or 3D matrix based on the number of color channels present in the image. For a binary or grey scale image, 2D array is sufficient. But for a colored image, you need 3D array.
In this tutorial, we shall learn in detail how to read an image using OpenCV, by considering some of the regular scenarios. We will also learn the order in which imread() function decodes the color channels from an image and how imread() treats different image extensions. Syntax of cv2.imread()The syntax of cv2.imread() function is given below.
where /path/to/image has to be the complete absolute path to the image. The flag is optional and one of the following possible values can be passed for the flag.
Example 1: OpenCV cv2 Read Color ImageIn this example, we will read a color image. As the default value of the Python Program
Output Run the above python program, and you shall get the following output.
Example 2: OpenCV cv2 – Read Image as Grey ScaleIn this example, we will read image as a grey scale image. Input can be color image or grey scale image. But, if flag argument is
Python Program
Output
Height of the image is 400 pixels, width is 640. Each element in the array represents grey scale value at the respective pixel. Example 3: OpenCV cv2 – Read Image with Transparency ChannelIn this example, we will read an image with transparency channel. If there is a transparency channel in the image, then we can pass cv2.IMREAD_UNCHANGED to read the transparency channel along with the color channels. Python Program
Output
We have read all the four channels of the image. Namely Red, Green, Blue and Transparency. imread() and Color Channelsimread() decodes the image into a matrix with the color channels stored in the order of Blue, Green, Red and A (Transparency) respectively. If (400, 640, 4) is the shape of the image, then
imread() and File ExtensionsThere are many extensions used for images based on the operating system, compression technique, etc. When imread() method reads image, it does not consider the extension of the image file name to determine the format of the image. But decides the extension based on the format present in the file data. imread() supports JPEGs, PNGs, and TIFFs across all platforms. But for the combination of other formats and Operating Systems, imread() may consider the operating system level codecs. You may refer the official documentation of imread() for these special scenarios. SummaryConcluding this tutorial of Python Examples, we learned to use cv2 imread() method to read an image into a Python Array. Related Tutorials
How do I create an array image in OpenCV?python opencv create new image. # use numpy to create an array of color.. image_height = 300.. image_width = 300.. number_of_color_channels = 3.. color = (255,0,255). pixel_array = numpy. full((image_height, image_width, number_of_color_channels), color, dtype=numpy. uint8). cv2. imshow('image',pixel_array). cv2. waitKey(0). How do you convert a matrix to an image in Python?Since images are just an array of pixels carrying various color codes. NumPy can be used to convert an array into image.. Create a numpy array.. Reshape the above array to suitable dimensions.. Create an image object from the above array using PIL library.. Save the image object in a suitable file format.. Is OpenCV image NumPy array?OpenCV is the most popular computer vision library and has a wide range of features. It doesn't have its own internal storage format for images, instead, it uses NumPy arrays.
Can cv2 read NumPy array?The cv2 package provides an imread() function to load the image. It also reads a PIL image in the NumPy array format. The only thing we need to convert is the image color from BGR to RGB. imwrite() saves the image in the file.
|