OpenCV

pip install opencv-python

Image

Basic

import cv2

image = cv2.imread('image.jpg')

cv2.imshow('Original Image', image)
cv2.imwrite('image.jpg', image)

cv2.waitKey(0)
cv2.destroyAllWindows()

Processing

import cv2

# Read the image
image = cv2.imread('image.jpg')

# Convert the image to grayscale
grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Crop the image
y, h, w, x = 300, 100, 300, 100
cropped_image = image[y:y+h, w:w+x]

# Resize the image by 50%
resized_image = cv2.resize(image, (0, 0), fx=0.5, fy=0.5)

# Rotate the image by 45 degrees
rotated_image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)

# Apply a Canny edge detection filter to the image
edges_image = cv2.Canny(image, 50, 150)

# Wait for a key press to quit
cv2.waitKey(0)
cv2.destroyAllWindows()

Video

Capturing Video from a Camera

Reading Video from a File

Write Video

Last updated