OpenCV 物体识别技术概述
OpenCV(Open Source Computer Vision Library)是一个开源的计算机视觉库,广泛应用于物体识别、图像处理和机器学习任务。物体识别是计算机视觉的核心任务之一,涉及目标检测、特征提取和分类等技术。
Haar 特征分类器用于人脸识别
Haar 特征分类器是一种基于机器学习的物体检测方法,常用于人脸识别。OpenCV 提供了预训练的 Haar 级联分类器模型,可直接用于检测。
import cv2
# 加载 Haar 级联分类器
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# 读取图像并转换为灰度图
img = cv2.imread('face.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 检测人脸
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
# 绘制检测框
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('Face Detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
使用预训练的深度学习模型进行物体检测
OpenCV 支持加载预训练的深度学习模型(如 YOLO、SSD、MobileNet)进行高精度物体检测。
import cv2
import numpy as np
# 加载预训练模型和类别标签
net = cv2.dnn.readNetFromDarknet('yolov3.cfg', 'yolov3.weights')
classes = []
with open('coco.names', 'r') as f:
classes = [line.strip() for line in f.readlines()]
# 读取图像
img = cv2.imread('objects.jpg')
height, width, _ = img.shape
# 构建输入 blob
blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
output_layers = net.getUnconnectedOutLayersNames()
outputs = net.forward(output_layers)
# 解析检测结果
boxes = []
confidences = []
class_ids = []
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
# 非极大值抑制
indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# 绘制检测框和标签
for i in indices:
box = boxes[i]
x, y, w, h = box
label = f"{classes[class_ids[i]]}: {confidences[i]:.2f}"
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(img, label, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.imshow('Object Detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
基于 SIFT/SURF 的特征匹配
SIFT(Scale-Invariant Feature Transform)和 SURF(Speeded-Up Robust Features)是经典的局部特征提取算法,可用于物体识别和匹配。
import cv2
import numpy as np
# 读取参考图像和目标图像
img_ref = cv2.imread('reference.jpg', cv2.IMREAD_GRAYSCALE)
img_target = cv2.imread('target.jpg', cv2.IMREAD_GRAYSCALE)
# 初始化 SIFT 检测器
sift = cv2.SIFT_create()
# 检测关键点和计算描述符
kp_ref, desc_ref = sift.detectAndCompute(img_ref, None)
kp_target, desc_target = sift.detectAndCompute(img_target, None)
# 使用暴力匹配器进行特征匹配
bf = cv2.BFMatcher()
matches = bf.knnMatch(desc_ref, desc_target, k=2)
# 应用比率测试筛选匹配点
good_matches = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
good_matches.append([m])
# 绘制匹配结果
img_matches = cv2.drawMatchesKnn(img_ref, kp_ref, img_target, kp_target, good_matches, None, flags=2)
cv2.imshow('Feature Matching', img_matches)
cv2.waitKey(0)
cv2.destroyAllWindows()
自定义物体识别训练
OpenCV 支持训练自定义物体分类器,例如使用 SVM(支持向量机)和 HOG(方向梯度直方图)特征。
import cv2
import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from skimage.feature import hog
# 加载正负样本数据
def load_samples(positive_dir, negative_dir):
features = []
labels = []
# 加载正样本(物体图像)
for img_path in positive_dir:
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
hog_feature = hog(img, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2))
features.append(hog_feature)
labels.append(1)
# 加载负样本(背景图像)
for img_path in negative_dir:
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
hog_feature = hog(img, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2))
features.append(hog_feature)
labels.append(0)
return np.array(features), np.array(labels)
# 训练 SVM 分类器
features, labels = load_samples(['object1.jpg', 'object2.jpg'], ['background1.jpg', 'background2.jpg'])
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)
svm = SVC(kernel='linear')
svm.fit(X_train, y_train)
# 测试分类器
accuracy = svm.score(X_test, y_test)
print(f"Accuracy: {accuracy:.2f}")
总结
OpenCV 提供了多种物体识别方法,包括传统机器学习(Haar、SIFT/SURF)和深度学习(YOLO、SSD)。开发者可以根据需求选择合适的算法,并结合自定义训练提升识别精度。