Python计算机视觉入门:OpenCV基础应用
什么是计算机视觉
计算机视觉是人工智能的一个重要分支,旨在让计算机能够理解和解释视觉信息。Python凭借其丰富的库生态,成为了计算机视觉开发的首选语言。
OpenCV简介
OpenCV是一个开源的计算机视觉库,提供了丰富的图像处理和计算机视觉算法。它支持多种编程语言,包括Python、C++、Java等。
安装OpenCV
# 安装OpenCV
pip install opencv-python
pip install opencv-contrib-python # 包含额外功能
# 验证安装
import cv2
print(cv2.__version__)
图像读取与显示
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.jpg')
# 显示图像
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存图像
cv2.imwrite('output.jpg', img)
图像预处理
颜色空间转换
# RGB转灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# RGB转HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# RGB转LAB
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
图像滤波
# 高斯滤波
blurred = cv2.GaussianBlur(img, (15, 15), 0)
# 中值滤波
median = cv2.medianBlur(img, 5)
# 双边滤波
bilateral = cv2.bilateralFilter(img, 9, 75, 75)
边缘检测
# Canny边缘检测
edges = cv2.Canny(gray, 50, 150)
# Sobel边缘检测
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
sobel = np.sqrt(sobelx**2 + sobely**2)
特征检测与匹配
SIFT特征检测
# 创建SIFT检测器
sift = cv2.SIFT_create()
# 检测关键点和描述符
keypoints, descriptors = sift.detectAndCompute(gray, None)
# 绘制关键点
img_with_keypoints = cv2.drawKeypoints(img, keypoints, None)
特征匹配
# 使用FLANN匹配器
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
# 匹配特征
matches = flann.knnMatch(desc1, desc2, k=2)
# 应用Lowe's ratio test
good_matches = []
for match_pair in matches:
if len(match_pair) == 2:
m, n = match_pair
if m.distance < 0.7 * n.distance:
good_matches.append(m)
目标检测
Haar级联分类器
# 加载Haar级联分类器
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# 检测人脸
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# 绘制检测结果
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
深度学习模型集成
使用预训练模型
import cv2
import numpy as np
# 加载预训练的DNN模型
net = cv2.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'graph.pbtxt')
# 准备输入图像
blob = cv2.dnn.blobFromImage(img, 1.0, (300, 300), [104, 117, 123])
# 前向传播
net.setInput(blob)
detections = net.forward()
# 处理检测结果
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > 0.5:
x1 = int(detections[0, 0, i, 3] * img.shape[1])
y1 = int(detections[0, 0, i, 4] * img.shape[0])
x2 = int(detections[0, 0, i, 5] * img.shape[1])
y2 = int(detections[0, 0, i, 6] * img.shape[0])
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
实际应用案例
车牌识别系统
def detect_license_plate(img):
# 转换为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 高斯滤波
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# 边缘检测
edges = cv2.Canny(blurred, 50, 150)
# 查找轮廓
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 筛选车牌轮廓
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
aspect_ratio = w / h
# 车牌通常有特定的长宽比
if 2.5 < aspect_ratio < 5.0 and w > 100 and h > 30:
return (x, y, w, h)
return None
性能优化技巧
- 使用GPU加速(cv2.cuda模块)
- 图像预处理优化
- 多线程处理
- 内存管理优化
总结
OpenCV为Python计算机视觉开发提供了强大的工具集。从基础的图像处理到复杂的深度学习模型集成,OpenCV都能胜任。掌握OpenCV的基本用法,结合深度学习框架,可以构建强大的计算机视觉应用。
评论区