OpenCV Python Tutorial: Complete Guide for Beginners
OpenCV (Open Source Computer Vision Library) is one of the most powerful and widely used libraries for image processing and computer vision tasks. With Python support, it becomes extremely easy to build real-world applications like face detection, object tracking, image filtering, and video analysis.
In this tutorial, you will learn OpenCV in Python from the ground up with practical examples.
1. What is OpenCV?
OpenCV is an open-source library designed for:
- Image processing
- Video analysis
- Object detection
- Face recognition
- Machine learning-based vision tasks
It is widely used in:
- AI applications
- Robotics
- Surveillance systems
- Self-driving cars
- Medical imaging
2. Install OpenCV in Python
Before starting, install OpenCV using pip:
pip install opencv-python
For extra features (optional):
pip install opencv-contrib-python
Check installation:
import cv2
print(cv2.__version__)
3. Reading and Displaying Images
Load an image
import cv2
img = cv2.imread("image.jpg")
cv2.imshow("Image Window", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation:
-
imread()→ reads image -
imshow()→ displays image -
waitKey(0)→ waits for key press -
destroyAllWindows()→ closes window
4. Writing/Saving Images
import cv2
img = cv2.imread("image.jpg")
cv2.imwrite("output.jpg", img)
5. Image Properties
print(img.shape) # height, width, channels
print(img.size) # total pixels
print(img.dtype) # image data type
6. Converting Color Spaces
Convert BGR to Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray Image", gray)
Convert BGR to RGB
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
7. Image Resizing
resized = cv2.resize(img, (300, 300))
cv2.imshow("Resized Image", resized)
8. Image Cropping
cropped = img[100:400, 200:500]
cv2.imshow("Cropped Image", cropped)
9. Drawing Shapes on Images
Draw a line
cv2.line(img, (0,0), (200,200), (255,0,0), 3)
Draw rectangle
cv2.rectangle(img, (50,50), (200,200), (0,255,0), 2)
Draw circle
cv2.circle(img, (150,150), 50, (0,0,255), -1)
10. Text on Image
cv2.putText(img, "Hello OpenCV", (50,50),
cv2.FONT_HERSHEY_SIMPLEX, 1,
(255,255,255), 2)
11. Image Blurring
Gaussian Blur
blur = cv2.GaussianBlur(img, (15,15), 0)
cv2.imshow("Blurred Image", blur)
12. Edge Detection (Canny)
edges = cv2.Canny(img, 100, 200)
cv2.imshow("Edges", edges)
13. Image Thresholding
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
cv2.imshow("Threshold", thresh)
14. Working with Video (Webcam)
Capture video from webcam
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow("Webcam", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
15. Saving Video from Webcam
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while True:
ret, frame = cap.read()
out.write(frame)
cv2.imshow("Video", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
16. Face Detection (Basic Example)
OpenCV includes pre-trained classifiers.
import cv2
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, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
cv2.imshow("Faces", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
17. Real-World OpenCV Projects You Can Build
Here are some ideas:
- Face detection system
- Motion detection camera
- Object tracking system
- Barcode scanner
- AI-based attendance system
- License plate recognition system
18. Conclusion
OpenCV with Python is a powerful combination for building real-world computer vision applications. Once you master the basics like image processing, filtering, and video handling, you can move into advanced AI-based vision projects.
Start experimenting with images and videos today to strengthen your computer vision skills!


0 Comments