🧠Getting Started with OpenCV in Python
Part 1: Introduction & Essential Functions
📌 Series Overview
- Part 1: Introduction & Essential Functions ✅
- Part 2: Reading, Displaying, and Writing Images
- Part 3: Image Processing Basics – Resizing, Cropping, and Color Spaces
- Part 4: Drawing Shapes and Putting Text on Images
- Part 5: Real-time Video Capture and Simple Filters with Webcam
🔑 Essential OpenCV Functions (with Examples)
Function | Description | Example |
---|---|---|
cv2.imread(path) |
Reads an image from a file |
|
cv2.imshow(window, image) |
Displays the image |
|
cv2.waitKey(0) |
Waits for a key press |
|
cv2.destroyAllWindows() |
Closes OpenCV windows |
|
cv2.imwrite(path, image) |
Saves the image |
|
cv2.cvtColor(image, flag) |
Converts color spaces |
|
cv2.resize(image, (w, h)) |
Resizes the image |
|
cv2.GaussianBlur(image, (k, k), 0) |
Applies blur |
|
🧪 Full Example: Combined Use
import cv2
# Load image
image = cv2.imread("sample.jpg")
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Resize
resized = cv2.resize(gray, (300, 200))
# Blur
blurred = cv2.GaussianBlur(resized, (7, 7), 0)
# Show and save
cv2.imshow("Processed", blurred)
cv2.imwrite("processed_output.jpg", blurred)
cv2.waitKey(0)
cv2.destroyAllWindows()
🧠This example combines all the essential OpenCV functions step-by-step. Try it with your own image named sample.jpg
!
0 Comments