OpenCV Examples
OpenCV How To Examples in Python
OpenCV is an open-source computer vision library that provides various functions and tools for image and video processing. It is widely used in the field of computer vision and machine learning. In this tutorial, we will cover 10 examples of how to use OpenCV in Python.
Example 1: Reading and Displaying an Image
import cv2
# Read the image
image = cv2.imread('image.jpg')
# Display the image
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Expected Output: The image will be displayed in a new window.
Example 2: Converting an Image to Grayscale
import cv2
# Read the image
image = cv2.imread('image.jpg')
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Display the grayscale image
cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Expected Output: The grayscale version of the image will be displayed in a new window.
Example 3: Resizing an Image
import cv2
# Read the image
image = cv2.imread('image.jpg')
# Resize the image
resized_image = cv2.resize(image, (500, 300))
# Display the resized image
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Expected Output: The resized image will be displayed in a new window.
Example 4: Drawing Shapes on an Image
import cv2
# Create a blank image
image = np.zeros((500, 500, 3), dtype=np.uint8)
# Draw a rectangle
cv2.rectangle(image, (100, 100), (400, 400), (0, 255, 0), 2)
# Draw a circle
cv2.circle(image, (250, 250), 100, (0, 0, 255), -1)
# Display the image with shapes
cv2.imshow('Image with Shapes', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Expected Output: An image with a rectangle and a filled circle will be displayed in a new window.
Example 5: Applying Image Filters
import cv2
# Read the image
image = cv2.imread('image.jpg')
# Apply a blur filter
blurred_image = cv2.blur(image, (5, 5))
# Apply an edge detection filter
edges = cv2.Canny(image, 100, 200)
# Display the filtered images
cv2.imshow('Blurred Image', blurred_image)
cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
Expected Output: The blurred image and the edges detected from the original image will be displayed in separate windows.
Example 6: Detecting and Drawing Contours
import cv2
# Read the image
image = cv2.imread('image.jpg')
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply a threshold
ret, threshold = cv2.threshold(gray_image, 127, 255, 0)
# Find contours
contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Draw contours
cv2.drawContours(image, contours, -1, (0, 255, 0), 3)
# Display the image with contours
cv2.imshow('Image with Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Expected Output: The image with detected contours will be displayed in a new window.
Example 7: Face Detection
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the image
image = cv2.imread('image.jpg')
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray_image, 1.1, 4)
# Draw rectangles around the faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display the image with face detection
cv2.imshow('Image with Face Detection', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Expected Output: The image with rectangles around the detected faces will be displayed in a new window.
Example 8: Image Thresholding
import cv2
# Read the image
image = cv2.imread('image.jpg')
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply a threshold
ret, threshold = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)
# Display the thresholded image
cv2.imshow('Thresholded Image', threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()
Expected Output: The thresholded image will be displayed in a new window.
Example 9: Image Blending
import cv2
# Read the images
image1 = cv2.imread('image1.jpg')
image2 = cv2.imread('image2.jpg')
# Resize the images
image1 = cv2.resize(image1, (500, 500))
image2 = cv2.resize(image2, (500, 500))
# Blend the images
blended_image = cv2.addWeighted(image1, 0.5, image2, 0.5, 0)
# Display the blended image
cv2.imshow('Blended Image', blended_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Expected Output: The blended image of two input images will be displayed in a new window.
Example 10: Video Capture and Display
import cv2
# Open the video capture
cap = cv2.VideoCapture(0)
while True:
# Read a frame from the video
ret, frame = cap.read()
# Display the frame
cv2.imshow('Video', frame)
# Exit the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture
cap.release()
cv2.destroyAllWindows()
Expected Output: The video captured from the webcam will be displayed in a new window. Press 'q' to exit the program.
These examples cover some of the basic functionalities of OpenCV in Python. OpenCV provides a wide range of functions and tools for various image and video processing tasks, and these examples can serve as a starting point for further exploration and experimentation.