opencv-master 4.5.1 - Python 示例代码运行测试 ( 一 ) | 分割 - 视频人像跟踪


文章目录

  • 环境搭建
  • opencv-master4.5.1 Python 边缘检测算子测试
  • 图像分割测试
  • 计算 视频帧的 PSNR 、SSIM
  • Background subtraction method(视频人像跟踪 - 背景提取)
  • 基于颜色分布的连续自适应均值漂移【跟踪】算法(camshift algorithm)
  • Lucas-Kanade Optical Flow calculation【光流计算跟踪】
  • This program demonstrates line finding with the Hough transform【使用霍夫变换进行寻线的过程】
  • Sample code that shows the functionality of copyMakeBorder【检测 图像边框】
  • 镜像 翻转
  • 图像变形 -- 旋转角度



环境搭建


系统环境 【 这里是用的 Linux 虚拟机 】

cat /etc/issue

Ubuntu 20.04.2 LTS \n \l

conda 新建独立环境如下:

conda create -n py369 python=3.6.9

conda activate py369

pip install opencv-python

pip install matplotlib 

pip install numpy

运行环境 pip list 如下:

pip list

Package         Version
--------------- -------------------
certifi         2020.12.5
cycler          0.10.0
kiwisolver      1.3.1
matplotlib      3.3.4
numpy           1.19.5
opencv-python   4.5.1.48
Pillow          8.2.0
pip             21.0.1
pyparsing       2.4.7
python-dateutil 2.8.1
setuptools      52.0.0.post20210125
six             1.15.0
wheel           0.36.2

opencv-master4.5.1源码下载地址

其它版本下载地址:https://github.com/opencv/opencv/releases

  • 本博文的测试是直接拉取 opencv-master4.5.1,然后在opencv/samples/python/tutorial_code/ImgTrans 目录下对相应 py 文件进行测试

opencv人像动漫化_opencv人像动漫化


opencv-master4.5.1 Python 边缘检测算子测试


测试所输入数据

  • opencv-master4.5.1/opencv/samples/data 目录下有测试可用数据

lena.jpg 原图:

cd pyOpenCVLearn/opencv/samples/python

cp opencv/samples/data/lena.jpg .

opencv人像动漫化_python_02

Sobel 算子

cd pyOpenCVLearn/opencv/samples/python/tutorial_code/ImgTrans/SobelDemo

python sobel_demo.py ../../../lena.jpg

运行效果如下:

opencv人像动漫化_python_03

LaPlace 算子

cd pyOpenCVLearn/opencv/samples/python/tutorial_code/ImgTrans/LaPlace

python laplace_demo.py ../../../lena.jpg

opencv人像动漫化_opencv_04

canny 算子 detector

cd pyOpenCVLearn/opencv/samples/python/tutorial_code/ImgTrans/canny_detector

python CannyDetector_Demo.py --input ../../../lena.jpg

运行效果如下:

opencv人像动漫化_opencv人像动漫化_05


图像分割测试


Code for Image Segmentation with Distance Transform and Watershed Algorithm

cd opencv/samples/python/tutorial_code/ImgTrans/distance_transformation

python imageSegmentation.py --input ../../../../data/cards.png

效果如下:

opencv人像动漫化_视频人像跟踪_06


计算 视频帧的 PSNR 、SSIM


这个代码感觉对于 超分重建 PSNR 计算还是有些许参考价值的;

cd opencv/samples/python/tutorial_code/videoio

python video-input-psnr-ssim.py -r ../../../data/Megamind.avi -t ../../../data/Megamind_bugy.avi

输出大致如下:

Frame: 0# 0dB 
Frame: 1# 41.279dB 
Frame: 2# 41.354dB 
Frame: 3# 41.201dB 
Frame: 4# 41.043dB 
Frame: 5# 19.214dB MSSISM: R 94.69% G 93.39% B 90.06% 
Frame: 6# 40.818dB 
Frame: 7# 41.046dB 
Frame: 8# 40.919dB 
Frame: 9# 41.166dB 
Frame: 10# 15.832dB MSSISM: R 93.17% G 91.99% B 88.64% 
Frame: 11# 40.767dB 
Frame: 12# 41.016dB 
Frame: 13# 40.746dB 
Frame: 14# 41.186dB 
Frame: 15# 40.297dB 
Frame: 16# 40.44dB 
Frame: 17# 40.97dB 
Frame: 18# 40.846dB 
Frame: 19# 40.727dB 
Frame: 20# 22.09dB MSSISM: R 94.64% G 93.88% B 90.64% 
Frame: 21# 40.953dB 
Frame: 22# 40.845dB 
Frame: 23# 40.894dB 
Frame: 24# 40.776dB 
Frame: 25# 22.021dB MSSISM: R 93.1% G 91.86% B 89.21% 
Frame: 26# 40.067dB 
Frame: 27# 39.661dB 
Frame: 28# 39.176dB 
Frame: 29# 39.289dB 
Frame: 30# 21.877dB MSSISM: R 90.4% G 88.64% B 86.24%

Background subtraction method(视频人像跟踪 - 背景提取)


cd opencv/samples/python/tutorial_code/video/background_subtraction

python bg_sub.py --input ../../../../data/vtest.avi

效果如下:

opencv人像动漫化_视频人像跟踪_07

# Background subtraction method (KNN, MOG2)
# 算法设置为 KNN 测试如下: 

python bg_sub.py --input ../../../../data/vtest.avi --algo KNN

opencv人像动漫化_opencv人像动漫化_08


基于颜色分布的连续自适应均值漂移【跟踪】算法(camshift algorithm)


有兴趣了解这个算法可以自行检索该算法相关文献:

例如:基于Camshift算法的小球跟踪

脚本运行输入

输入是一个 mp4 需要自行点击下载

运行代码如下:

# 下载的 mp4  文件 copy 到 opencv/samples/data/ 目录下

cp slow_traffic_small.mp4  opencv/samples/data/


cd opencv/samples/python/tutorial_code/video/meanshift

# 运行输入如下:

python camshift.py ../../../../data/slow_traffic_small.mp4

# 另外一个 meanshift.py 脚本也可以运行测试:

python meanshift.py ../../../../data/slow_traffic_small.mp4

运行效果如下:

opencv人像动漫化_opencv人像动漫化_09


Lucas-Kanade Optical Flow calculation【光流计算跟踪】


运行代码如下:

cd opencv/samples/python/tutorial_code/video/optical_flow

python optical_flow.py ../../../../data/slow_traffic_small.mp4

运行效果如下:

opencv人像动漫化_视频人像跟踪_10

运行代码如下:

cd opencv/samples/python/tutorial_code/video/optical_flow

# 需要 把 vtest.avi copy 到当前目录作为默认输入
cp ../../../../data/vtest.avi .

python optical_flow_dense.py

效果如下:

opencv人像动漫化_opencv_11


This program demonstrates line finding with the Hough transform【使用霍夫变换进行寻线的过程】


运行代码如下:

cd  /opencv/samples/python/tutorial_code/ImgTrans/HoughLine

# 把 opencv-master4.5.1\opencv\samples\data 目录下 代码使用的 数据 copy 到 当前目录
cp ../../../../data/sudoku.png .

python hough_lines.py

# 或者另一个 方法

cd ../HoughCircle/

cp ../../../../data/smarties.png .

运行效果如下:

opencv人像动漫化_python_12

opencv人像动漫化_视频人像跟踪_13


Sample code that shows the functionality of copyMakeBorder【检测 图像边框】


运行代码如下:

cd opencv/samples/python/tutorial_code/ImgTrans/MakeBorder

cp ../../../../data/lena.jpg .

python copy_make_border.py

效果如下:

opencv人像动漫化_opencv人像动漫化_14


镜像 翻转


运行代码如下:

cd opencv/samples/python/tutorial_code/ImgTrans/remap

python Remap_Demo.py --input ../../../../data/chicky_512.png

效果如下:

opencv人像动漫化_python_15


图像变形 – 旋转角度


运行代码如下:

cd opencv/samples/python/tutorial_code/ImgTrans/warp_affine

python Geometric_Transforms_Demo.py --input ../../../../data/lena.jpg

效果如下:

opencv人像动漫化_opencv人像动漫化_16