How to Learn OpenCV Image Processing in Python

5.5M views
•
November 3, 2020
by
freeCodeCamp.org
YouTube video player
How to Learn OpenCV Image Processing in Python

TL;DR

Install Python 3.7 or later, then add opencv-contrib-python to begin processing images and videos with OpenCV. The course progresses from reading, displaying, resizing, drawing, and transforming media to contours, color spaces, masking, histograms, thresholding, edge detection, face detection, face recognition, and a deep computer vision classifier for Simpsons characters.

Transcript

Everyone and welcome to this Python and open CV course. In this course, we'll be talking about everything you need to know. To get started with open CV in Python, we're going to start off with the very basics that is reading images and video, manipulating those media files with image transformations, and how to draw shapes and put text on those fil... Read More

Key Insights

  • OpenCV is a computer vision library available in Python, C++, and Java, and the course uses its Python interface to process and manipulate images and videos. The material starts with foundational media operations before progressing to image analysis, faces, and deep computer vision.
  • Python 3.7 or later is the recommended foundation for following the course because the demonstrated work may not function correctly with older releases, especially Python 2. The installation check uses the terminal command that reports the currently installed Python version.
  • The opencv-contrib-python package includes the main OpenCV module plus contribution modules provided by the community. The instructor recommends it instead of installing only opencv-python because it provides the OpenCV functionality needed across the beginner, advanced, and face-focused sections.
  • Images are read with cv.imread by supplying either a relative or absolute file path, and the result is a matrix of pixels. The matrix can then be displayed in a separate window with cv.imshow, while cv.waitKey keeps that window open until keyboard input arrives.
  • Large images can extend beyond the monitor because their dimensions may exceed the available screen space. The demonstrated solution path is to resize or rescale frames and images, a topic covered immediately after the lesson on reading image and video files.
  • Videos are processed frame by frame through an instance of cv.VideoCapture. Its read method returns both the current frame and a Boolean indicating whether that frame was successfully read, allowing a loop to display the sequence as a video.
  • A negative 215 assertion failure commonly appears in the demonstrated workflow when OpenCV cannot read the requested media. The course produces it with an incorrect image path and again after a video runs out of frames, showing that the requested image or frame was unavailable.
  • The advanced curriculum connects several image-processing techniques, including color-space conversion, channel handling, blurring, bitwise operations, masking, histograms, thresholding, and edge detection. These topics lead into Haar cascade face detection and recognition with OpenCV's built-in recognizer.

Install to Summarize YouTube Videos and Get Transcripts

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: How do you install OpenCV for this Python course?

Use Python 3.7 or later and install the opencv-contrib-python package with pip. The instructor recommends this package because it contains the main OpenCV module together with contribution modules supplied by the community. NumPy is also installed because OpenCV uses it for matrix and array work. Caer is recommended for utility functions used later in the deep computer vision project.

Q: How do you read and display an image with OpenCV?

Import cv2 using the alias cv, then pass the image's relative or absolute path to cv.imread. The method returns the image as a matrix of pixels, which can be stored in a variable. Pass a window name and that matrix to cv.imshow. Calling cv.waitKey with zero keeps the displayed window open indefinitely until a keyboard key is pressed.

Q: How do you read a video file with OpenCV?

Create a capture object by passing the video's file path to cv.VideoCapture. Inside a loop, call the capture object's read method to obtain a Boolean success value and the next frame. Display each frame with cv.imshow and use cv.waitKey to check for a chosen exit key. After the loop, release the capture object and destroy the OpenCV windows.

Q: How do you access a webcam with OpenCV?

Pass an integer to cv.VideoCapture instead of a video file path. The integer zero generally references the webcam, while subsequent integers can reference additional connected cameras. Once the capture object is created, read frames inside a loop and display them individually, following the same frame-by-frame approach used when playing a stored video file.

Q: Why does OpenCV show a negative 215 assertion failed error?

In the course examples, a negative 215 assertion failure means OpenCV could not read the requested media at the specified location. It occurs when an incorrect path is supplied for an image, and it also occurs after the demonstrated video reaches its final frame because no additional frame can be read. Checking the path and read result identifies these cases.

Q: Why does a large OpenCV image extend off the screen?

OpenCV displays the image using its original dimensions, so an image larger than the monitor can extend beyond the visible screen. The course demonstrates this with a 2400 by 1600 image after first showing a 640 by 427 version. It addresses the problem in the following lesson by teaching how to resize and rescale images and video frames.

Q: What image-processing techniques does the OpenCV course cover?

The course begins with reading images and videos, resizing and rescaling frames, drawing shapes, adding text, essential OpenCV functions, image transformations, and contour detection. It then covers color spaces, color channels, blurring, bitwise operations, masking, histogram computation, thresholding or binarizing images, and edge detection before moving into face detection and face recognition.

Q: How does the course approach face detection and recognition?

The face section separates finding faces from identifying them. Face detection is taught with Haar cascades, while face recognition uses OpenCV's built-in recognizer. The broader course introduction describes these tasks as detecting and finding faces in an image, then recognizing them with built-in methods. These lessons follow the foundational and advanced image-processing sections.

Summary & Key Takeaways

  • The course begins with environment setup and basic media handling. It recommends Python 3.7 or later and installing opencv-contrib-python, which includes the main OpenCV module and community contribution modules. NumPy supports matrix and array operations, while the optional Caer utility package is reserved mainly for the final deep computer vision project.

  • Images are loaded as pixel matrices with cv.imread and displayed in a window with cv.imshow. Videos and camera feeds use cv.VideoCapture, with file paths selecting stored videos and integer arguments selecting connected cameras. A loop reads and displays each frame until playback ends or a designated key stops execution.

  • The curriculum advances from resizing, drawing, essential functions, transformations, and contour detection to color spaces, channels, blurring, bitwise operations, masking, histograms, thresholding, and edge detection. Its final sections cover Haar cascade face detection, OpenCV's built-in face recognizer, and a deep computer vision model that classifies Simpsons characters from images.


Read in Other Languages (beta)

Share This Summary 📚

Explore More Summaries from freeCodeCamp.org 📚