CV2 in Python --- 1 part of 5

Getting Started with OpenCV in Python – Part 1

🧠 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
image = cv2.imread("sample.jpg")
cv2.imshow(window, image) Displays the image
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(0) Waits for a key press
cv2.waitKey(0)
cv2.destroyAllWindows() Closes OpenCV windows
cv2.destroyAllWindows()
cv2.imwrite(path, image) Saves the image
cv2.imwrite("output.jpg", image)
cv2.cvtColor(image, flag) Converts color spaces
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.resize(image, (w, h)) Resizes the image
resized = cv2.resize(image, (300, 200))
cv2.GaussianBlur(image, (k, k), 0) Applies blur
blurred = cv2.GaussianBlur(image, (7, 7), 0)

🧪 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!

⌨️ Made with Python & ❤️ by Champak Roy | Part 1 of 5

Post a Comment

0 Comments

Me