Python之OpenCV绑定库详细使用示例
OpenCV是一个开源的计算机视觉库,广泛应用于图像处理和计算机视觉领域。Python通过cv2
模块提供了对OpenCV的绑定,使得开发者可以方便地使用Python进行图像处理和计算机视觉任务。本文将详细介绍Python中OpenCV绑定库的使用方法,并提供丰富的示例代码。
一、安装OpenCV
首先需要安装OpenCV库:
pip install opencv-python
如果需要额外的功能(如SIFT、SURF等专利算法),可以安装:
pip install opencv-contrib-python
二、基本图像操作
1. 读取和显示图像
import cv2
# 读取图像
img = cv2.imread('image.jpg') # 默认BGR格式
# 显示图像
cv2.imshow('Image', img)
# 等待按键并关闭窗口
cv2.waitKey(0)
cv2.destroyAllWindows()
2. 保存图像
cv2.imwrite('output.jpg', img) # 保存为JPEG格式
3. 获取图像信息
print(f"图像形状: {img.shape}") # (高度, 宽度, 通道数)
print(f"图像大小: {img.size} 字节")
print(f"图像数据类型: {img.dtype}") # 通常是uint8
三、图像基本处理
1. 颜色空间转换
# BGR转灰度
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# BGR转RGB
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 显示结果
cv2.imshow('Gray', gray)
cv2.imshow('RGB', rgb)
cv2.waitKey(0)
2. 图像缩放
# 缩放到指定尺寸
resized = cv2.resize(img, (300, 200)) # (宽度, 高度)
# 按比例缩放
scale_percent = 50 # 缩放到50%
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
resized = cv2.resize(img, (width, height))
cv2.imshow('Resized', resized)
cv2.waitKey(0)
3. 图像裁剪
# 裁剪图像 (y1:y2, x1:x2)
cropped = img[100:400, 200:500]
cv2.imshow('Cropped', cropped)
cv2.waitKey(0)
4. 图像旋转
# 获取图像中心
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
# 旋转矩阵
M = cv2.getRotationMatrix2D(center, 45, 1.0) # 旋转45度,缩放1.0
# 应用旋转
rotated = cv2.warpAffine(img, M, (w, h))
cv2.imshow('Rotated', rotated)
cv2.waitKey(0)
四、图像滤波
1. 均值模糊
blurred = cv2.blur(img, (5, 5)) # 5x5核大小
cv2.imshow('Blurred', blurred)
cv2.waitKey(0)
2. 高斯模糊
gaussian = cv2.GaussianBlur(img, (5, 5), 0) # 核大小5x5,标准差0
cv2.imshow('Gaussian', gaussian)
cv2.waitKey(0)
3. 中值模糊
median = cv2.medianBlur(img, 5) # 核大小5
cv2.imshow('Median', median)
cv2.waitKey(0)
4. 双边滤波
bilateral = cv2.bilateralFilter(img, 9, 75, 75) # 核大小9,颜色和空间sigma
cv2.imshow('Bilateral', bilateral)
cv2.waitKey(0)
五、边缘检测
1. Canny边缘检测
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200) # 阈值100和200
cv2.imshow('Edges', edges)
cv2.waitKey(0)
2. Sobel算子
grad_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3) # x方向
grad_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3) # y方向
# 合并梯度
abs_grad_x = cv2.convertScaleAbs(grad_x)
abs_grad_y = cv2.convertScaleAbs(grad_y)
grad = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
cv2.imshow('Sobel', grad)
cv2.waitKey(0)
六、形态学操作
1. 膨胀和腐蚀
# 二值化图像
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 定义核
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
# 膨胀
dilated = cv2.dilate(binary, kernel, iterations=1)
# 腐蚀
eroded = cv2.erode(binary, kernel, iterations=1)
cv2.imshow('Dilated', dilated)
cv2.imshow('Eroded', eroded)
cv2.waitKey(0)
2. 开运算和闭运算
# 开运算(先腐蚀后膨胀)
opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
# 闭运算(先膨胀后腐蚀)
closing = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
cv2.imshow('Opening', opening)
cv2.imshow('Closing', closing)
cv2.waitKey(0)
七、特征检测与匹配
1. Harris角点检测
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Harris角点检测
corners = cv2.cornerHarris(gray, 2, 3, 0.04)
# 结果可视化
img_corners = img.copy()
img_corners[corners > 0.01 * corners.max()] = [0, 0, 255]
cv2.imshow('Harris Corners', img_corners)
cv2.waitKey(0)
2. SIFT特征检测
# 确保安装了opencv-contrib-python
sift = cv2.SIFT_create()
# 检测关键点和描述符
keypoints, descriptors = sift.detectAndCompute(gray, None)
# 绘制关键点
img_sift = cv2.drawKeypoints(img, keypoints, None, color=(0, 255, 0))
cv2.imshow('SIFT Keypoints', img_sift)
cv2.waitKey(0)
3. 特征匹配
# 读取第二张图像
img2 = cv2.imread('image2.jpg')
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# 检测关键点和描述符
keypoints2, descriptors2 = sift.detectAndCompute(gray2, 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(descriptors, descriptors2, k=2)
# 应用比率测试
good = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good.append(m)
# 绘制匹配结果
img_matches = cv2.drawMatches(img, keypoints, img2, keypoints2, good, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
cv2.imshow('Feature Matches', img_matches)
cv2.waitKey(0)
八、视频处理
1. 读取和显示视频
cap = cv2.VideoCapture('video.mp4') # 或使用0读取摄像头
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
cv2.imshow('Video', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
2. 视频写入
cap = cv2.VideoCapture(0) # 读取摄像头
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 处理帧(例如转换为灰度)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)) # 需要转换回BGR
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
九、图像分割
1. 阈值分割
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 固定阈值
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 自适应阈值
thresh_adapt = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2)
cv2.imshow('Threshold', thresh)
cv2.imshow('Adaptive Threshold', thresh_adapt)
cv2.waitKey(0)
2. 轮廓检测
# 二值化图像
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, _ = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
img_contours = img.copy()
cv2.drawContours(img_contours, contours, -1, (0, 255, 0), 2)
cv2.imshow('Contours', img_contours)
cv2.waitKey(0)
十、高级示例:人脸检测
# 加载预训练的人脸检测模型
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, minSize=(30, 30))
# 绘制矩形框
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)
十一、性能优化技巧
-
使用NumPy操作替代循环:
# 不推荐
for i in range(rows):
for j in range(cols):
img[i,j] = [255, 255, 255] if some_condition else [0, 0, 0]
# 推荐
condition = some_condition_array
img = np.where(condition[..., None], [255, 255, 255], [0, 0, 0])
使用inRange进行颜色分割:
# 创建掩膜
lower = np.array([0, 100, 100])
upper = np.array([10, 255, 255])
mask = cv2.inRange(hsv_img, lower, upper)
使用积分图像加速计算:
# 计算积分图像
integral = cv2.integral(gray)
# 快速计算矩形区域和
sum_rect = integral[x2,y2] - integral[x1-1,y2] - integral[x2,y1-1] + integral[x1-1,y1-1]