In Python, we perform various Computer Vision tasks using the OpenCV library. This library has implemented various techniques and can process images very efficiently.
One of the basic tasks in processing images is displaying them. This tutorial will discuss showing an image using the OpenCV library in Python.
imshow() Function to Show an Image Using the OpenCV Library in Python
imshow() function from the OpenCV library shows images. This function does not return anything directly but creates a new window that displays the image.
Images are read as
numpy arrays in Python. We can pass this object to the
imshow() function, which will display it.
We can also add a title to the window name in this function using the
window_name parameter. See the code below.
import cv2 img = cv2.imread('obj.png') cv2.imshow("Show image", img) cv2.waitKey(0) cv2.destroyAllWindows()
In the above code, we read an image using the
imread() function to create an object that stores this image.
This object is passed to the
imshow() function, and the image is displayed in a new window. We also added a title to this window.
We used two functions,
destroyAllWindows(), after the
imshow() method, which prevented the newly created window from closing automatically. It waits for the user to press some key before closing it.