• ubuntu22.04@laptop OpenCV Get Started: 008_image_filtering_using_convolution


    1. 源由

    卷积过滤也是OpenCV图像最基本的操作之一。

    可能有人会问,这么复杂,什么是卷积?还矩阵,线性代数?

    千万不要这么想,记住一点,计算机编程不是学数学,学物理,学化学,学辩证哲学这种逻辑复杂的学科。凡事我们应该尽量简化,不用想得这么复杂。因为,这些复杂的专业知识,深层次领域知识是OpenCV模块、架构师需要搞明白的。作为入门学习的计算机编程人员来说,只要知道概念,学会API使用即可。

    那么,为了更好的了解概念,这里做个补充一些类似的知识点或者说日常生活常识:

    • 一个未知数的方程 ==》 对应一个变量:x ==》一维空间
    • 二个未知数的方程 ==》 对应二个变量:x,y ==》 二维空间
    • 三个未知数的方程 ==》 对应三个变量:x,y,z ==》三维空间
    • 四个未知数的方程 ==》 对应四个变量:x,y,z,t ==》四维空间

    通常来说,人能够比较直观感受、认识,以及理解的大体上是这四类空间。

    x,y,z,t可以看做四个未知数(变量),当只有一个变量变化的时候,从空间维度的角度看就是一维空间。

    好了,到这里我们实际上就从多因素分析问题直接跳跃到单一因素分析问题了。

    例如:传感器采样,数据采样进行算术平均

    1. 在某 t 1 t_1 t1时刻,采集ADC数据值348;
    2. 在某 t 2 = t 1 + Δ t t_2=t_1+\Delta t t2=t1+Δt时刻,采集ADC数据值344;
    3. 在某 t 3 = t 2 + Δ t t_3=t_2+\Delta t t3=t2+Δt时刻,采集ADC数据值346;
    4. 在某 t 4 = t 3 + Δ t t_4=t_3+\Delta t t4=t3+Δt时刻,采集ADC数据值350;
    5. 在某 t 5 = t 4 + Δ t t_5=t_4+\Delta t t5=t4+Δt时刻,采集ADC数据值352;
    6. 假设 Δ t \Delta t Δt非常短,因此我们认为上述4个ADC采样数据是同一时刻 t 3 t_3 t3的采样值,由于干扰,误差等导致数据存在偏差;
    7. 假设算术平均能够对上述ADC数据进行去噪处理(或者说噪声符合算术平均规律); //请注意,这里是业务领域,不同的业务模型会不一样,可能是算术平均,可能是加权,可能是一阶滤波,也可能是二阶滤波等等
    8. 因此,使用算术平均值模型,进行如下计算 2 n + 1 2n+1 2n+1个采样值分析,得出平均值 A D C t 3 ‾ = 348 \overline{ADC_{t_{3}}}=348 ADCt3=348

    A D C t n + 1 ‾ = ∑ 1 ≤ i ≤ 2 n + 1 A D C t i n + 1 = 348 + 344 + 346 + 350 + 352 5 = 348 \overline{ADC_{t_{n+1}}}=\dfrac{\sum_{\mathclap{1\le i\le 2n+1}} ADC_{t_i}}{n+1}=\dfrac{348+344+346+350+352}{5}=348 ADCtn+1=n+11i2n+1ADCti=5348+344+346+350+352=348

    上面这个例子是一维空间的算术平滑过滤,因为传感器的x,y,z三维空间的位置并未发生任何位移,仅仅时间上略有变化。

    当我们假设这个采样时间间隔不是采样值变化的主要因素时,就需要解决时间维度变量下,噪声该如何去除,建立过滤模型。

    理解了上面这个概念,那么就可以理解照片我们是在某个时刻t拍摄(采样)的数据,但是由于像素曝光空间上可能存在衍射,电子噪声干扰等问题,我们假设的模型是周边像素采样值可能影响到采样数据,因此就会产生去噪的模型。

    一维空间通常是方程式,二维空间我们用行列式(矩阵),这样说,相信就可以理解了。

    注:当遇到多维度空间问题时,复杂程度就远超常人能够理解的维度,此时,业务建模,数据分析就凸显重要性。当然现在也有AI算法(深度学习,神经网络)。这些底层的算术逻辑可能需要更高层面的深入分析和理解,以及公理去证明。也超出现在个人的理解能力,不做讨论。对这些感兴趣的朋友不妨了解下《一种部件生命期监测方法》

    2. convolution应用Demo

    008_image_filtering_using_convolution是OpenCV图像过滤的示例程序。

    2.1 C++应用Demo

    C++应用Demo工程结构:

    008_image_filtering_using_convolution/CPP$ tree .
    .
    ├── CMakeLists.txt
    ├── image_convolution.cpp
    └── test.jpg
    
    0 directories, 3 files
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    确认OpenCV安装路径:

    $ find /home/daniel/ -name "OpenCVConfig.cmake"
    /home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
    /home/daniel/OpenCV/opencv/build/OpenCVConfig.cmake
    /home/daniel/OpenCV/opencv/build/unix-install/OpenCVConfig.cmake
    
    
    $ export OpenCV_DIR=/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    C++应用Demo工程编译执行:

    $ mkdir build
    $ cd build
    $ cmake ..
    $ cmake --build . --config Release
    $ cd ..
    $ ./build/convolution
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.2 Python应用Demo

    Python应用Demo工程结构:

    008_image_filtering_using_convolution/Python$ tree .
    .
    ├── image_convolution.py
    ├── requirements.txt
    └── test.jpg
    
    0 directories, 3 files
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Python应用Demo工程执行:

    $ workoncv-4.9.0
    $ python image_convolution.py
    
    • 1
    • 2

    3. 重点分析

    在这里插入图片描述

    3.1 identity矩阵

    根据线性代数,行列式计算方法,很容易看出该3x3矩阵就是将周边像素影响降为权值为0。

    在这里插入图片描述

    C++:

    // Apply identity filter using kernel
    Mat kernel1 = (Mat_(3,3) << 0, 0, 0, 0, 1, 0, 0, 0, 0);
    Mat identity; 
    filter2D(image, identity, -1 , kernel1, Point(-1, -1), 0, 4);
    
    • 1
    • 2
    • 3
    • 4

    Python:

    # Apply identity kernel
    kernel1 = np.array([[0, 0, 0],
                       [0, 1, 0],
                       [0, 0, 0]])
    
    identity = cv2.filter2D(src=image, ddepth=-1, kernel=kernel1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.2 all ones 5x5矩阵

    5行x5列,全1矩阵,并做一个归一化。

    根据线性代数,行列式计算方法,很容易看出该3x3矩阵就是将周边像素影响权值为1,做算术平均。

    C++:

    // Blurred using kernel
    // Initialize matrix with all ones
    Mat kernel2 = Mat::ones(5,5, CV_64F);
    // Normalize the elements
    kernel2 = kernel2 / 25;
    Mat img;
    filter2D(image, img, -1 , kernel2, Point(-1, -1), 0, 4);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Python:

    # Apply blurring kernel
    kernel2 = np.ones((5, 5), np.float32) / 25
    img = cv2.filter2D(src=image, ddepth=-1, kernel=kernel2)
    
    • 1
    • 2
    • 3

    3.3 blur 5x5矩阵

    5x5的OpenCV内置blur矩阵模糊化过滤。

    C++:

    // Blurred using OpenCV C++ blur() function
    Mat img_blur;
    blur(image, img_blur, Size(5,5));
    
    • 1
    • 2
    • 3

    Python:

    # Apply blur using `blur()` function
    img_blur = cv2.blur(src=image, ksize=(5,5)) 
    
    • 1
    • 2

    3.4 GaussianBlur 5x5矩阵

    5x5的OpenCV内置Gaussian Blur矩阵模糊化过滤。

    C++:

    // Performing Gaussian Blur
    Mat gaussian_blur;
    GaussianBlur(image, gaussian_blur, Size(5,5), 0);
    
    • 1
    • 2
    • 3

    Python:

    # Apply Gaussian blur
    gaussian_blur = cv2.GaussianBlur(src=image, ksize=(5,5), sigmaX=0)
    
    • 1
    • 2

    3.5 medianBlur 5x5矩阵

    5x5的OpenCV内置median Blur矩阵模糊化过滤。

    C++:

    // Apply Median Blur
    Mat median_blurred;
    medianBlur(image, median_blurred, (5,5));
    
    • 1
    • 2
    • 3

    Python:

    # Apply Median blur
    median_blurred = cv2.medianBlur(src=image, ksize=5)
    
    • 1
    • 2

    3.6 Sharpening 3x3矩阵

    5x5的定制了一个Kernel3矩阵进行凸显(锐化)中间像素。

    C++:

    // Apply Sharpening using kernel
    Mat sharp_img;
    Mat kernel3 = (Mat_(3,3) << 0, -1, 0, -1, 5, -1, 0, -1 ,0);
    filter2D(image, sharp_img, -1 , kernel3, Point(-1, -1), 0, BORDER_DEFAULT);
    
    • 1
    • 2
    • 3
    • 4

    Python:

    # Apply sharpening using kernel
    kernel3 = np.array([[0, -1, 0],
                       [-1, 5, -1],
                       [0, -1, 0]])
    sharp_img = cv2.filter2D(src=image, ddepth=-1, kernel=kernel3)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.7 bilateralFilter矩阵

    对区域内的像素进行数据过滤。

    C++:

    // Apply bilateral filtering
    Mat bilateral_filter;
    bilateralFilter(image, bilateral_filter, 9, 75, 75);
    
    • 1
    • 2
    • 3

    Python:

    # Apply Bilateral Filtering
    bilateral_filter = cv2.bilateralFilter(src=image, d=9, sigmaColor=75, sigmaSpace=75)
    
    • 1
    • 2

    4. 总结

    通过以下矩阵,对图像进行卷积的计算机操作,从而对数据进行有效性过滤。

    其实计算机编程就是这么简单,当然要深入理解为什么要过滤,怎么有效过滤,这个就是深入的课程,需要了解API参数的含义,以及各参数背后业务的具体意义。

    5. 参考资料

    【1】ubuntu22.04@laptop OpenCV Get Started
    【2】ubuntu22.04@laptop OpenCV安装
    【3】ubuntu22.04@laptop OpenCV定制化安装

    6. 补充

    学习是一种过程,对于前面章节学习讨论过的,就不在文中重复了。

    有兴趣了解更多的朋友,请从《ubuntu22.04@laptop OpenCV Get Started》开始,一个章节一个章节的了解,循序渐进。

  • 相关阅读:
    【ARC机制和Java中C#中垃圾回收机制的区别 构造方法里面必须用setter方法赋值 Objective-C语言】
    flink1.17安装
    Centos7 离线安装 KVM,并安装 Csr1000v
    【翻译】Neural 3D Mesh Renderer
    prototype 原型对象
    linux sed 命令汇总
    基于Python + Flask+ Mysq实现简易留言板
    重磅推荐,Github上一批优秀的「低代码」项目 ,点赞收藏按需取用
    托管机构管理系统
    手工测试如何转向自动化测试?字节5年自动化经验浅谈一下...
  • 原文地址:https://blog.csdn.net/lida2003/article/details/136114954