Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we will explore how to read an image in Python using the OpenCV library. Does anyone know what OpenCV is used for?
Is it for computer vision tasks?
Exactly! OpenCV stands for Open Source Computer Vision Library. It allows us to capture and manipulate images. Now, let me show you how to read an image using cv2.imread(). You provide the filename, and it imports the image into your program. Can anyone recall the method we use to display the image after reading it?
I think we use cv2.imshow()?
Correct! Using cv2.imshow(), we can display the image in a new window. Remember, if you're using Jupyter notebooks, it's advised to use Matplotlib instead. This way, you can avoid issues with displaying windows. Let's practice! Who wants to read and display an image?
I can try it with an example!
Great! Just remember, you’ll use cv2.imread('image.jpg') and then cv2.imshow('Window Title', img).
In summary, we learned about reading an image with cv2.imread() and displaying it with cv2.imshow(). If you're in a Jupyter notebook, stick to Matplotlib.
Now, let’s talk about what to do if you're using Jupyter notebooks. What can you recall about using Matplotlib for image display?
We can use plt.imshow() instead?
Exactly! By using plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)), we can change the color format from BGR to RGB, which Matplotlib requires for correct color representation. This is vital because OpenCV reads images in BGR format by default.
Do we have to turn off the axis when we show the image with Matplotlib?
Good question! Yes, we often use plt.axis('off') to hide the axes for a cleaner look. Shall we go ahead and display an image using Matplotlib together?
Yes, let's do it!
To recap, when using Matplotlib, we change the color format and can hide the axis for better presentation. This helps improve visual clarity in our outputs.
Reading images brings us delight, OpenCV shows them in plain sight.
Jessie was coding in Python and wanted to view an image of her puppy. She used OpenCV to read it and realized that RGB colors were all wrong, so she learned to convert it properly with Matplotlib. Now her puppy was finally displayed in the right colors for everyone to enjoy!
Remember OpenCV for Objective views (images).
BGR - Blue, Green, Red. The order OpenCV uses for colors.
Term
OpenCV
Click to flip
Term
cv2.imread()
Click to flip
Term
cv2.imshow()
Click to flip
Term
Matplotlib
Click to flip
Term
BGR Format
Click to flip
An open-source computer vision library designed to streamline image processing tasks.
A function used to read an image from a specified file.
A function that displays the image in a new window.
A plotting library in Python that can also display images.
The default color format read by OpenCV, where Blue, Green, and Red values are used.
A color format used by Matplotlib, where Red, Green, and Blue values are used.
This section teaches how to read and display an image in Python using the OpenCV library.
Students will learn to utilize OpenCV for image processing in Python, starting with reading an image file and displaying it. They will also discover alternative methods to display images using Matplotlib, ensuring versatility in handling image data.
In this section, the focus is on the practical application of image processing in Python using the OpenCV library. Students are introduced to reading an image file with cv2.imread() and displaying it using cv2.imshow(). This is critical for any data analysis involving visual data interpretation. The section also notes the caution necessary when using cv2.imshow() within Jupyter notebooks, providing an alternative with Matplotlib for those working in such environments. The ability to manipulate and visualize images is essential for various applications including computer vision, machine learning, and artificial intelligence.