引言
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]
以上就是python中opencv绑定库的使用方法详解的详细内容,更多关于python opencv绑定库使用的资料请关注代码网其它相关文章!
发表评论