OpenCV for Image Processing

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. It provides a common infrastructure for computer vision applications and accelerates the usage of machine perception in commercial products.

Why Use OpenCV?

10 Examples

import cv2

# Example 1: Read and display image
img = cv2.imread('image.jpg')
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Example 2: Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Example 3: Resize image
resized = cv2.resize(img, (100, 100))

# Example 4: Draw rectangle
cv2.rectangle(img, (50, 50), (150, 150), (0, 255, 0), 2)

# Example 5: Save image
cv2.imwrite('output.jpg', img)

# Example 6: Capture from webcam
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imshow('Webcam', frame)
cap.release()
cv2.destroyAllWindows()

# Example 7: Gaussian blur
blurred = cv2.GaussianBlur(img, (5, 5), 0)

# Example 8: Edge detection
edges = cv2.Canny(img, 100, 200)

# Example 9: Image thresholding
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# Example 10: Contour detection
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)