BoW model for constructing global image descriptors
In this recipe, you will learn how to apply the Bag-of-Words (BoW) model for computing global image descriptors. This technique can be used for building a machine learning model to solve image classification problems.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV version 3.3 (or greater) Python API package.
How to do it...
You need to complete the following steps:
- Import the necessary modules:
import cv2 import numpy as np import matplotlib.pyplot as plt
- Load the two train images:
img0 = cv2.imread('../data/people.jpg', cv2.IMREAD_GRAYSCALE) img1 = cv2.imread('../data/face.jpeg', cv2.IMREAD_GRAYSCALE)
- Detect the keypoints and computer descriptors for each training image:
detector = cv2.ORB_create(500) _, fea0 = detector.detectAndCompute(img0, None) _, fea1 = detector.detectAndCompute(img1, None) descr_type = fea0.dtype
- Construct the BoW vocabulary:
bow_trainer = cv2.BOWKMeansTrainer(50) bow_trainer.add(np.float32...