top of page

Computer Vision Tutorials in Python


Computer Vision Tutorials in Python


Prerequisites:

1. Basic knowledge of Python programming.

2. Familiarity with image concepts (pixels, color channels, etc.).


Installation:

Ensure you have OpenCV installed. You can install it using pip:


pip install opencv-python


1. Loading and Displaying Images:

Load and display an image using OpenCV and matplotlib.


import cv2
import matplotlib.pyplot as plt

image_path = 'path/to/your/image.jpg'
image = cv2.imread(image_path)

plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.title('Original Image')
plt.show()

2. Image Manipulation:

Perform common image manipulations using OpenCV.


gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
new_size = (300, 300)
resized_image = cv2.resize(image, new_size)
rotation_angle = 45
rows, cols = image.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((cols/2, rows/2), rotation_angle, 1)
rotated_image = cv2.warpAffine(image, rotation_matrix, (cols, rows))

3. Image Filtering:

Apply image filters for smoothing and edge detection.


blur_image = cv2.GaussianBlur(image, (5, 5), 0)
edges = cv2.Canny(gray_image, threshold1=30, threshold2=70)

4. Color Spaces:

Convert images between different color spaces.


hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)

5. Histogram Equalization:

Enhance image contrast using histogram equalization.


equalized_image = cv2.equalizeHist(gray_image)

6. Object Detection with Haar Cascades:

Use pre-trained Haar Cascade classifiers for object detection.



face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

7. Contours and Shape Detection:

Identify and work with image contours.


contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)

8. Image Thresholding:

Apply thresholding techniques for image segmentation.



_, thresholded_image = cv2.threshold(gray_image, 128, 255, cv2.THRESH_BINARY)
adaptive_threshold=cv2.adaptiveThreshold(gray_image,255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)

9. Image Morphology:

Perform basic morphological operations like erosion and dilation.


kernel = np.ones((5, 5), np.uint8)
eroded_image = cv2.erode(thresholded_image, kernel, iterations=1)
dilated_image = cv2.dilate(thresholded_image, kernel, iterations=1)

10. Image Warping and Perspective Transformation:

Warp images using perspective transformations.


pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
matrix = cv2.getPerspectiveTransform(pts1, pts2)
warped_image = cv2.warpPerspective(image, matrix, (300, 300))

11. Image Template Matching:

Detect instances of a template within an image.


template = cv2.imread('template.jpg', cv2.IMREAD_GRAYSCALE)
result = cv2.matchTemplate(gray_image, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
top_left = max_loc

12. Corner Detection:

Find and mark corners in images.


corners = cv2.goodFeaturesToTrack(gray_image, maxCorners=100, qualityLevel=0.01, minDistance=10)
corners = np.int0(corners)
for corner in corners:
x, y = corner.ravel()
cv2.circle(image, (x, y), 3, 255, -1)

13. Optical Flow:

Calculate and visualize optical flow using Lucas-Kanade method.



prev_frame = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
next_frame = cv2.cvtColor(next_frame, cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(prev_frame, next_frame, None, 0.5, 3, 15, 3, 5, 1.2, 0)

14. Image Segmentation with Watershed Algorithm:

Segment images using the watershed algorithm.


gradient = cv2.morphologyEx(edges, cv2.MORPH_GRADIENT, kernel)
_, markers = cv2.connectedComponents(gradient)
markers = markers + 1
markers[unknown == 255] = 0
markers = cv2.watershed(image, markers)
image[markers == -1] = [0, 0, 255]

15. Deep Learning-based Image Classification:

Use a pre-trained deep learning model for image classification.


import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions

model = ResNet50(weights='imagenet')
input_image = cv2.resize(image, (224, 224))
input_image = preprocess_input(np.expand_dims(input_image, axis=0))
predictions = model.predict(input_image)
decoded_predictions = decode_predictions(predictions, top=3)[0]




Related Posts

How to Install and Run Ollama on macOS

Ollama is a powerful tool that allows you to run large language models locally on your Mac. This guide will walk you through the steps to...

bottom of page