No history yet

Introduction to OpenCV

Giving Your Code Eyes

Computers are great at crunching numbers, but they're naturally blind. They don't see pixels as images; they see them as a massive grid of numerical values. OpenCV (Open Source Computer Vision Library) is a powerful tool that helps your code make sense of this visual data. It's like a pair of glasses that allows a program to see, interpret, and manipulate images and videos.

Originally developed by Intel, OpenCV is a free library used everywhere, from interactive art installations to the software that helps self-driving cars navigate. It works seamlessly with Python, making it one of the most popular choices for computer vision tasks.

Lesson image

Getting Started

Before we can start processing images, we need to install the library. If you have Python and its package manager, pip, set up, installation is just a single command in your terminal.

pip install opencv-python

This command downloads and installs the main OpenCV package. It also automatically installs NumPy, another library that OpenCV depends on heavily. NumPy is fantastic for handling the large arrays of numbers that make up images.

Handling Your First Image

The most fundamental tasks in computer vision are reading an image from a file, displaying it, and saving it back to your disk. Let's walk through how to do this with OpenCV.

First, you'll need an image file (like a JPG or PNG) in the same directory as your Python script. Let's assume you have one named photo.jpg.

To get started in your Python script, you first need to import the library. Even though we installed opencv-python, it is imported into Python under the name cv2.

import cv2

Now, we can read the image from the file using the imread() function. This function loads the image from the specified path and returns it as a NumPy array.

# Load an image from the file 'photo.jpg'
image = cv2.imread('photo.jpg')

With the image loaded into the image variable, the next step is to display it. Simply printing the variable will just show you a massive array of numbers, which isn't very helpful. Instead, OpenCV provides a simple way to show the image in a new window.

# Display the image in a window named 'My Image'
cv2.imshow('My Image', image)

# Wait indefinitely until a key is pressed
cv2.waitKey(0)

# Close all the windows that OpenCV opened
cv2.destroyAllWindows()

Let's break that down. cv2.imshow() opens a window. The first argument is the window's title, and the second is the image variable to display. The cv2.waitKey(0) function is crucial. It tells the program to pause and wait for a key press. Without it, the script would run, flash the window on screen for a millisecond, and close it before you could even see it.

Finally, it's good practice to call cv2.destroyAllWindows() to clean up and close any open windows when you're done.

After loading and viewing an image, you might want to save it. Perhaps you performed some modifications (which we'll cover later) or just want to save it in a different format. The imwrite() function handles this.

# Save the image to a new file named 'new_photo.png'
cv2.imwrite('new_photo.png', image)

This function takes the desired filename as the first argument and the image data as the second. OpenCV is smart enough to figure out the file format (PNG, JPG, etc.) from the file extension you provide.

Quiz Questions 1/5

What does OpenCV stand for?

Quiz Questions 2/5

Which command is used to install the OpenCV library for Python using pip?

That's the basic workflow. You now have the fundamental tools to load, view, and save any image using Python and OpenCV.