Computer vision powers everything from facial recognition to autonomous vehicles — and OpenCV (Open Source Computer Vision Library) is the most widely used open-source tool for this field. With Python, OpenCV becomes incredibly accessible and flexible.
This tutorial will take you from installing OpenCV to performing advanced tasks like image processing, face detection, and real-time video analysis — all with Python.
What is OpenCV?
OpenCV (Open Source Computer Vision Library) is an open-source toolkit for real-time computer vision, developed initially by Intel. It supports:
- Image and video processing
- Object and face detection
- Machine learning models for visual data
- Real-time video analysis
It’s fast, written in C/C++, but has bindings for Python, Java, and other languages.
Installing OpenCV in Python
You can install OpenCV using pip
:
pip install opencv-python
For advanced features like video codecs or GUI, install the full version:
pip install opencv-contrib-python
Then import it in your Python script:
import cv2
Loading and Displaying Images
import cv2
# Load an image
img = cv2.imread('cat.jpg')
# Show the image
cv2.imshow('My Image', img)
# Wait for key press and close window
cv2.waitKey(0)
cv2.destroyAllWindows()
Convert to Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Image', gray)
Saving Images
cv2.imwrite('gray_cat.jpg', gray)
Reading and Displaying Video
cap = cv2.VideoCapture(0) # 0 for webcam
while True:
ret, frame = cap.read()
cv2.imshow('Live Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Image Resizing, Cropping, and Drawing
Resize Image
resized = cv2.resize(img, (300, 300))
Crop Image
cropped = img[50:200, 100:300] # y1:y2, x1:x2
Draw Shapes and Text
cv2.rectangle(img, (50, 50), (200, 200), (0, 255, 0), 2)
cv2.circle(img, (150, 150), 40, (255, 0, 0), 3)
cv2.putText(img, 'Hello OpenCV', (50, 300), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
Image Processing Techniques
1. Blurring
blurred = cv2.GaussianBlur(img, (7, 7), 0)
2. Edge Detection (Canny)
edges = cv2.Canny(img, 100, 200)
3. Thresholding
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
4. Erosion and Dilation
kernel = np.ones((5, 5), np.uint8)
dilated = cv2.dilate(thresh, kernel, iterations=1)
eroded = cv2.erode(thresh, kernel, iterations=1)
Face Detection with Haar Cascades
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
img = cv2.imread('face.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('Face Detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Contours and Shape Detection
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
Color Detection in Images
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Define blue color range
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)
result = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow('Detected Blue', result)
Useful OpenCV Functions Summary
Function | Description |
---|---|
cv2.imread() | Load image |
cv2.imshow() | Display image |
cv2.resize() | Resize image |
cv2.cvtColor() | Convert color space |
cv2.VideoCapture() | Capture video stream |
cv2.Canny() | Detect edges |
cv2.threshold() | Apply binary threshold |
cv2.findContours() | Detect shapes |
cv2.putText() | Add text to image |
OpenCV + AI: Go Further
Once you’ve mastered the basics, OpenCV pairs beautifully with:
- TensorFlow / PyTorch for deep learning
- MediaPipe for hand and face tracking
- YOLO / OpenVINO for real-time object detection
Final Thoughts
OpenCV + Python unlocks a powerful combination for anyone interested in:
- Image editing
- Real-time computer vision apps
- Surveillance, robotics, or AR
Whether you’re a beginner or building your first face detection system, this tutorial gives you a solid foundation.
What to Build Next?
- Face mask detector
- Motion detection camera
- Real-time barcode/QR scanner
- Image filters (like Instagram!)
- Handwriting recognition app
Pingback: How to Work with Camera Feed in Python (Real-Time Video Capture Tutorial) - painlessprogramming.com