OpenCV部分

OpenCV安装

执行以下命令安装opencv-python库(核心库)和opencv-contrib-python库(贡献库)。注意:命令拷贝后要合成一行执行,中间不要换行。

# 安装opencv核心库
pip3 install  --user opencv-python==3.4.2.16 --index-url https://pypi.tuna.tsinghua.edu.cn/simple/  --trusted-host https://pypi.tuna.tsinghua.edu.cn

# 安装opencv贡献库
pip3 install  --user opencv-contrib-python==3.4.2.16 --index-url https://pypi.tuna.tsinghua.edu.cn/simple/  --trusted-host https://pypi.tuna.tsinghua.edu.cn

视频基本处理

1)读取摄像头

import numpy as np
import cv2

cap = cv2.VideoCapture(0)  # 实例化VideoCapture对象, 0表示第一个摄像头
while cap.isOpened():
    ret, frame = cap.read()  # 捕获帧
    cv2.imshow("frame", frame)
    c = cv2.waitKey(1)  # 等待1毫秒,等待用户输入
    if c == 27:  # ESC键
        break

cap.release()  # 释放摄像头
cv2.destroyAllWindows()

2)播放视频文件

import numpy as np
import cv2

cap = cv2.VideoCapture("D:\\tmp\\min_nong.mp4")  # 打开视频文件
while cap.isOpened():
    ret, frame = cap.read()  # 读取帧
    cv2.imshow("frame", frame)  # 显示
    c = cv2.waitKey(25)
    if c == 27:  # ESC键
        break

cap.release()  # 释放视频设备
cv2.destroyAllWindows()

3)捕获并保存视频

import numpy as np
import cv2

""" 编解码4字标记值说明
cv2.VideoWriter_fourcc('I','4','2','0')表示未压缩的YUV颜色编码格式,色度子采样为4:2:0。
    该编码格式具有较好的兼容性,但产生的文件较大,文件扩展名为.avi。
cv2.VideoWriter_fourcc('P','I','M','I')表示 MPEG-1编码类型,生成的文件的扩展名为.avi。
cv2.VideoWriter_fourcc('X','V','I','D')表示MPEG-4编码类型。如果希望得到的视频大小为平均值,可以选用这个参数组合。
    该组合生成的文件的扩展名为.avi。
cv2.VideoWriter_fourcc('T','H','E','O')表示Ogg Vorbis编码类型,文件的扩展名为.ogv。
cv2.VideoWriter_fourcc('F','L','V','I')表示Flash视频,生成的文件的扩展名为.flv。
"""
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc("I", "4", "2", "0")  # 编解码4字标记值
out = cv2.VideoWriter("output.avi",  # 文件名
                      fourcc,  # 编解码类型
                      20,  # fps(帧速度)
                      (640, 480))  # 视频分辨率

while cap.isOpened():
    ret, frame = cap.read()  # 读取帧
    if ret == True:
        out.write(frame)  # 写入帧
        cv2.imshow("frame", frame)
        if cv2.waitKey(1) == 27:  # ESC键
            break
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

综合案例

1)利用OpenCV实现图像校正

【任务描述】

我们对图像中的目标进行分析和检测时,目标往往具有一定的倾斜角度,自然条件下拍摄的图像,完全平正是很少的。因此,需要将倾斜的目标“扶正”的过程就就叫做图像矫正。该案例中使用的原始图像如下:
在这里插入图片描述

# 2021-01-19 星期二 16:10
# 11_img_rectify.py
# 图像形状矫正
import cv2
import math
import numpy as np

# 读取数据
im = cv2.imread('../img_data/paper.jpg')
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.imshow('im_gray', im_gray)
cv2.imwrite('im_gray.jpg',im_gray)#保存图片
# 二值化
# t,im_bin=cv2.threshold(im_gray,
#                        210,255,
#                        cv2.THRESH_BINARY)
# cv2.imshow('im_bin',im_bin)
# 模糊、膨胀合并过细的细节
im_blur = cv2.GaussianBlur(im_gray, (5, 5), 0)
im_dilate = cv2.dilate(im_blur, (3, 3))
cv2.imshow('im_dilate', im_dilate)
cv2.imwrite('im_dilate.jpg',im_dilate)#保存图片
# canny边沿提取
im_canny = cv2.Canny(im_dilate,  # 图像
                     30,  # 滞后阈值
                     120)  # 模糊度
cv2.imshow('im_canny', im_canny)
cv2.imwrite('im_canny.jpg',im_canny)#保存图片
# 在canny边沿提取结果之上查找外部轮廓
img, cnts, hie = cv2.findContours(
    im_canny,  # canny边沿提取结果
    cv2.RETR_EXTERNAL,  # 只提取外部轮廓
    cv2.CHAIN_APPROX_SIMPLE)  # 简化格式保存轮廓
# 绘制轮廓
im_cnt = cv2.drawContours(im,  # 原图上绘制
                          cnts,  # 轮廓数据
                          -1,  # 绘制所有轮廓
                          (0, 0, 255), 2)  # 颜色、粗细

cv2.imshow('in_cnt', im_cnt)
cv2.imwrite('in_cnt.jpg',im_cnt)#保存图片
# 计算轮廓面积,由大到小排序
if len(cnts) > 0:  # 轮廓数量大于0、
    # 排序
    cnts = sorted(cnts,  # 待排序容器
                  key=cv2.contourArea,  # 排序依据:面积
                  reverse=True)  # 倒序排列
    # 对轮廓进行多边形拟合
    for cnt in cnts:
        peri = cv2.arcLength(cnt, True)  # 计算轮廓周长
        approx = cv2.approxPolyDP(cnt,  # 轮廓
                                  0.02 * peri,  # 精度
                                  True)  # 产生封闭多边形
        if len(approx) == 4:  # 四边形
            docCnt = approx
            break

print(docCnt)

# 用圆圈标记角点
points = []
for peak in docCnt:
    peak = peak[0]
    cv2.circle(im,  # 在原图上绘制
               tuple(peak),  # 将角点转换为元祖
               10,  # 圆圈半径大小
               (0, 0, 255), 2)  # 颜色、粗细
    points.append(peak)
cv2.imshow('im_circle', im)
cv2.imwrite('im_circle.jpg',im)#保存图片
# 校正
src = np.float32([points[0], points[1], points[2], points[3]])  # 原来逆时针方向四个点

"""
dst=np.float32([[0,0],[0,488],[337,488],[337,0]])#对应变换后逆时针方向四个点
m=cv2.getPerspectiveTransform(src,dst)#生成透视变换矩阵
result=cv2.warpPerspective(im_gray.copy(),
                           m,
                           (337,448))#透视变换
cv2.imshow('result',result)
"""
h = int(math.sqrt((points[1][0] - points[0][0]) ** 2 + (points[1][1]
                                                        - points[0][1]) ** 2))  # 宽度
w = int(math.sqrt((points[2][0] - points[1][0]) ** 2 + (points[2][1]
                                                        - points[1][1]) ** 2))  # 宽度
print('w:', w, 'h:', h)
dst = np.float32([[0, 0], [0, h], [w, h], [w, 0]])
m = cv2.getPerspectiveTransform(src, dst)  # 生成透视变换矩阵
result = cv2.warpPerspective(im_gray.copy(), m, (w, h))  # 透视变换
cv2.imshow('result', result)
cv2.imwrite('result.jpg',result)#保存图片
cv2.waitKey()
cv2.destroyAllWindows()

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_49304690/article/details/113032055