AI Engineer Preparation Guide - Part 2: Computer
Vision Basics
This is Part 2 of the AI Engineer Preparation Guide. Here we go deeper into Computer Vision
basics using OpenCV. You will learn essential image processing techniques such as filters, edge
detection, thresholding, and contour detection.
Step 1: Images as Arrays
1 An image is stored as a 2D or 3D NumPy array.
2 Grayscale image: height × width.
3 Color image (RGB/BGR): height × width × 3 channels.
import cv2
# Load image
img = cv2.imread("example.jpg")
print("Image shape:", img.shape) # (height, width, channels)
# Access pixel value
(b, g, r) = img[50, 50]
print("Pixel at (50, 50):", b, g, r)
Step 2: Image Transformations
1 Resize, crop, rotate images.
2 Flipping and shifting images is also possible with OpenCV.
# Resize
resized = cv2.resize(img, (200, 200))
# Crop (rows 50:200, cols 100:300)
cropped = img[50:200, 100:300]
# Rotate 90 degrees
rotated = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
# Flip horizontally
flipped = cv2.flip(img, 1)
Step 3: Filters and Blurring
1 Filters highlight or smoothen details in an image.
2 Blurring is used to remove noise before edge detection.
# Gaussian Blur
blurred = cv2.GaussianBlur(img, (5, 5), 0)
# Median Blur
median = cv2.medianBlur(img, 5)
# Bilateral Filter (edge-preserving)
bilateral = cv2.bilateralFilter(img, 9, 75, 75)
Step 4: Edge Detection
1 Edges are important features in computer vision.
2 The most popular algorithm is the Canny edge detector.
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply Canny edge detector
edges = cv2.Canny(gray, 100, 200)
cv2.imshow("Edges", edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
Step 5: Thresholding & Morphology
1 Thresholding converts an image into black and white.
2 Morphological operations like dilation and erosion help remove noise.
# Simple thresholding
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Adaptive thresholding
adaptive = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 11, 2)
# Dilation and Erosion
dilated = cv2.dilate(thresh, None, iterations=2)
eroded = cv2.erode(thresh, None, iterations=2)
Step 6: Contour Detection
1 Contours are the outlines of shapes in an image.
2 They are useful for detecting objects and measuring properties.
# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Draw contours
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
cv2.imshow("Contours", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
■ By completing Part 2, you now understand essential image processing techniques: image
transformations, filtering, edge detection, thresholding, and contours. These are the building blocks
for advanced computer vision tasks.