Loading and Displaying Images

Step by step tutorial for loading and displaying an image with OpenCV

Updated March 19, 2023


Hey! If you love Computer Vision and AI, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

As a beginner in the field of computer vision, it is important to understand the basics of loading and displaying images using OpenCV. This article will guide you through the process of loading and displaying images, explaining the theory behind the process and providing multiple code examples to help you understand the concepts covered.

What is OpenCV?

OpenCV is an open source computer vision library that contains various algorithms used for image processing, detection, and analysis. It is used in a wide variety of applications such as robotics, augmented reality, self-driving cars, medical imaging, and more. OpenCV supports various programming languages such as C++, Python, and Java.

Loading an Image

Before we can display an image, we first need to load it into memory. OpenCV provides a simple function called “imread” for loading images. This function takes the path of the image as the input and returns a NumPy array representing the image.

Here is an example of loading an image using imread:

import cv2

# Load the image
img = cv2.imread("example_image.jpg")

In this example, we are loading an image with the filename “example_image.jpg”. The imread function returns a NumPy array containing the pixel values of the image.

Displaying an Image

Once we have loaded the image, we can display it using the “imshow” function provided by OpenCV. This function takes two arguments: the window name and the image array.

import cv2

# Load the image
img = cv2.imread("example_image.jpg")

# Display the image
cv2.imshow("Example Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we are displaying the loaded image using “imshow”. The first argument is the window name we want to give to the image window. The second argument is the image array we loaded in the previous step. Finally, we call the “waitKey” function to wait for a key event, and the “destroyAllWindows” function to close the image window when a key is pressed.

Converting to Grayscale

Many image processing algorithms work on grayscale images, so it is often necessary to convert an image to grayscale before applying any further processing. OpenCV provides a simple function called “cvtColor” for converting an image to grayscale.

import cv2

# Load the image
img = cv2.imread("example_image.jpg")

# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Display the grayscale image
cv2.imshow("Grayscale Image", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we are converting the loaded image to grayscale using “cvtColor”. The first argument is the image array we want to convert, and the second argument is the conversion code. In this case, we are using “COLOR_BGR2GRAY” which is the code for converting a BGR color image to grayscale.

Resizing an Image

Sometimes we need to resize an image to a specific size for further processing or display. OpenCV provides a simple function called “resize” for resizing an image.

import cv2

# Load the image
img = cv2.imread("example_image.jpg")

# Resize the image to a new width and height
new_width = 500
new_height = 500
resized = cv2.resize(img, (new_width, new_height))

# Display the resized image
cv2.imshow("Resized Image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we are resizing the loaded image to a new width and height using “resize”. The first argument is the image array we want to resize, and the second argument is a tuple containing the new width and height of the image.

Conclusion

In this article, we have covered the basics of loading and displaying images using OpenCV. We have looked at how to load images, display images, convert images to grayscale, and resize images. By mastering these concepts, you will be well on your way to understanding the basics of computer vision and image processing using OpenCV. Keep practicing and experimenting, and you will be amazed at what you can achieve with OpenCV!