cv2 imdecode method Implementation in Python

cv2 imdecode method Implementation in Python : With Steps

Opencv cv2 imdecode allows you to read any image from the memory and easily convert it into an image. Suppose you want to efficiently read an image from the internet, database, and file then you will use the cv2 imdecode method. In this entire tutorial, you will know how to decode an image using the imdecode() function in python.

Syntax for the cv2 imdecode() function

Before going to the demonstration part let’s first know the syntax of the cv2 imdecode() method.  Below is the syntax for it.

cv2.imdecode(buf,flags)

Here I have used two parameters one is the buffer image that is an image in a byte array and the other flags that allows you to specify how the image should be read. The default is cv2.IMREAD_COLOR. 

Steps to implement cv2.imread() function

In this entire section, you will know all the steps to implement cv2.imdecode() method. Make sure you should follow all the steps defined here for better understanding.

Step 1: Import all the necessary libraries

The first step is to import all the required libraries. In this entire tutorial I will use only three python packages one is NumPy, the other is OpenCV and the last is requests. So let’s import it.

import cv2
import numpy as np
import  requests

Step 2: Read the image

The second step is to read the image. In our example, I will read the image from the internet. To read it I will use the requests python package. I will just pass the URL of the sample image inside the requests.get() method. Execute the below lines of code.

url = "https://www.datasciencelearner.com/wp-content/uploads/2020/11/Final-dsl-logo_2.png"
response = requests.get(url)

Step 3: Convert the image to byte array

Before decoding the image you have to first convert it to a byte array. Numpy provides a function for this and it is numpy.asarray(). Just pass the response content to the np.asarray() to convert it. You have to also define the type of the array using the dtype=”uint8“. Add the below lines of code.

image = np.asarray(bytearray(response.content),dtype="uint8")

Step 4 : Apply yhe cv2.imdecode() method

After reading and converting the image to a byte array let’s apply this cv2.imdecode() function to the input image. Add the following lines of code.

image = cv2.imdecode(image, cv2.IMREAD_COLOR)
cv2.imshow("output.jpg", image)
cv2.waitKey(0)

You can see in the above code I am passing the input image and flag for decoding. After that, I am using the function imshow() to show the image to the screen with the help of cv2.waitKey(0).

You will get the output as below when you run the full code.

Full Code

import cv2
import numpy as np
import requests

url = "https://www.datasciencelearner.com/wp-content/uploads/2020/11/Final-dsl-logo_2.png"
response = requests.get(url)
# convert image to numpy array
image = np.asarray(bytearray(response.content), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
cv2.imshow("output.jpg", image)
cv2.waitKey(0)

Output

cv2 imdecode implementation output
cv2 imdecode implementation output

Conclusion

These are the easy steps for implementing imdecode() function in Python. Using this method you can also read images in an efficient way from the disk. I hope you have understood how to implement imdecode() method. If you have any queries then you can contact us for more help.

Source:

OpenCV Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner