Example 1. Adrian Rosebrock

CHAPTER 4. Image Manipulation and Segmentation

Image Segmentation

import cv2
import numpy as np
image = cv2.imread('nh_label.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(gray, 50, 200)
cv2.imshow('1 Canny Edges', edged)
cv2.waitKey(0)
Example 3. OpenCV docs

CHAPTER 5. Object Detection and Recognition

Basics of Object Detection

Template Matching

import cv2
import numpy as np

colorImage = cv2.imread('nh_label.jpg')
colorIcon = cv2.imread('use_by_date.jpg')

grayImage = cv2.cvtColor(colorImage, cv2.COLOR_BGR2GRAY)
grayIcon = cv2.cvtColor(colorIcon, cv2.COLOR_BGR2GRAY)

w,h = grayIcon.shape[::-1]

result = cv2.matchTemplate(grayImage, grayIcon, cv2.TM_CCOEFF)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

# Create Bounding Box
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(grayImage, top_left, bottom_right, (0,0,255), 5)
cv2.imshow('Match Template', grayImage)
cv2.waitKey(0)

Feature Matching

Example 5. Adrian Rosebrock

Harris Corner Algorithm

Example 6. OpenCV docs
Example 7. Matplotlib docs

Scale-Invariant Feature Transform

Example 8. OpenCV docs