• 图像处理初学者导引---OpenCV 方法演示项目


    OpenCV 方法演示项目

    项目地址:https://github.com/WangQvQ/opencv-tutorial

    请添加图片描述


    项目简介

    这个开源项目是一个用于演示 OpenCV 方法的工具,旨在帮助初学者快速理解和掌握 OpenCV 图像处理技术。通过这个项目,你可以轻松地对图像进行各种处理,从灰度化到边缘检测,以及更多其他方法。项目使用 Gradio 创建用户友好的界面,让用户能够轻松选择不同的图像处理方法和参数。


    为什么选择这个项目

    • 教育性:这个项目的主要目的是教育。它提供了对 OpenCV 方法的实际演示,以帮助初学者更好地理解和掌握这些技术。

    • 互动性:通过 Gradio 创建的用户界面,用户可以立即看到不同处理方法的效果,并可以自己调整参数,以更深入地理解每种方法的工作原理。

    • 适用广泛:这个项目可以帮助广大初学者,无论是学习计算机视觉、图像处理,还是对 OpenCV 有兴趣的人都会受益。


    特性

    • 提供了多种 OpenCV 图像处理方法的演示,包括灰度化、反转颜色、平移、直方图均衡化、腐蚀、膨胀、均值滤波、中值滤波、高斯滤波等。

    • 支持自定义卷积核,允许用户尝试不同的卷积核来处理图像。

    • 提供图像旋转、仿射变换和透射变换的演示,以及选择角度和参数的选项。

    • 使用 Gradio 创建用户友好的界面,让用户能够轻松选择不同的图像处理方法和参数。


    使用方法

    1. 获取项目:首先,你需要将这个项目克隆到你的本地计算机上。你可以使用以下命令来获取项目:

      git clone https://github.com/WangQvQ/opencv-tutorial.git
      
      • 1
    2. 安装依赖项:确保你已经安装了以下依赖项:

      • OpenCV
      • Gradio
      • NumPy

      如果你没有安装它们,你可以使用以下命令安装:

      pip install opencv-python-headless=4.7.0.72 gradio=3.1.5 numpy=1.22.4
      
      • 1
    3. 运行项目:使用以下命令来运行项目:

      python opencv_demo.py
      
      • 1

      运行后,你将看到一个网址,通常是 http://localhost:7860,你可以在浏览器中访问它。

    4. 使用界面:在浏览器中,你可以上传图像并选择不同的处理方法和参数,然后查看处理后的图像效果。


    示例代码

    请添加图片描述

    以下是部分方法的代码示例:

    # 灰度化处理函数
    def grayscale(input_image):
        gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
        return gray_image
    
    
    # 平移图像处理函数
    def translate_image(input_image, translation_x, translation_y):
        rows, cols, _ = input_image.shape
        translation_matrix = np.float32([[1, 0, translation_x], [0, 1, translation_y]])
        translated_image = cv2.warpAffine(input_image, translation_matrix, (cols, rows))
        return translated_image
    
    
    # Canny 边缘检测处理函数
    def edge_detection(input_image):
        edges = cv2.Canny(input_image, 100, 200)
        return edges
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    贡献

    如果你对项目有任何改进或建议,欢迎贡献代码或提出问题。我们欢迎开发者共同改进这个项目,以使其更加有用和友好。


    源代码

    如果你不想克隆项目,也可以直接运行我的源代码:

    import cv2  
    import gradio as gr  
    import numpy as np  
    
    
    # 原始图像处理函数
    def original_image(input_image):
        return input_image
    
    
    # 灰度化处理函数
    def grayscale(input_image):
        gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
        return gray_image
    
    
    # 平移图像处理函数
    def translate_image(input_image, translation_x, translation_y):
        rows, cols, _ = input_image.shape
        translation_matrix = np.float32([[1, 0, translation_x], [0, 1, translation_y]])
        translated_image = cv2.warpAffine(input_image, translation_matrix, (cols, rows))
        return translated_image
    
    
    # Canny 边缘检测处理函数
    def edge_detection(input_image):
        edges = cv2.Canny(input_image, 100, 200)
        return edges
    
    
    # Sobel 边缘检测处理函数
    def sobel_edge_detection(input_image):
        gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
        sobel_x = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=5)
        sobel_y = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=5)
        sobel_magnitude = cv2.magnitude(sobel_x, sobel_y)
        sobel_magnitude = np.uint8(255 * sobel_magnitude / np.max(sobel_magnitude))
        return sobel_magnitude
    
    
    # 反转颜色处理函数
    def invert_colors(input_image):
        inverted_image = cv2.bitwise_not(input_image)
        return inverted_image
    
    
    # 腐蚀处理函数
    def erosion(input_image, iterations):
        kernel = np.ones((5, 5), np.uint8)
        eroded_image = cv2.erode(input_image, kernel, iterations=iterations)
        return eroded_image
    
    
    # 膨胀处理函数
    def dilation(input_image, dilation_iterations):
        kernel = np.ones((5, 5), np.uint8)
        dilated_image = cv2.dilate(input_image, kernel, iterations=dilation_iterations)
        return dilated_image
    
    
    # 均值滤波处理函数
    def mean_blur(input_image):
        mean_blurred_image = cv2.blur(input_image, (5, 5))
        return mean_blurred_image
    
    
    # 中值滤波处理函数
    def median_blur(input_image):
        median_blurred_image = cv2.medianBlur(input_image, 5)
        return median_blurred_image
    
    
    # 高斯滤波处理函数
    def gaussian_blur(input_image):
        gaussian_blurred_image = cv2.GaussianBlur(input_image, (5, 5), 0)
        return gaussian_blurred_image
    
    
    # 双边滤波处理函数
    def bilateral_filter(input_image):
        bilateral_filtered_image = cv2.bilateralFilter(input_image, 9, 75, 75)
        return bilateral_filtered_image
    
    
    # 方块滤波处理函数
    def box_filter(input_image):
        box_filtered_image = cv2.boxFilter(input_image, -1, (5, 5))
        return box_filtered_image
    
    
    # 直方图均衡化处理函数
    def histogram_equalization(input_image):
        gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
        equalized_image = cv2.equalizeHist(gray_image)
        return cv2.cvtColor(equalized_image, cv2.COLOR_GRAY2BGR)
    
    
    # 仿射变换处理函数
    def affine_transform(input_image):
        # 创建仿射变换矩阵
        rows, cols, _ = input_image.shape
        matrix = cv2.getRotationMatrix2D((cols / 4, rows / 2), 70, 0.5)  # 90度旋转和1.5倍缩放
        result_image = cv2.warpAffine(input_image, matrix, (cols, rows))
        return result_image
    
    
    # 透射变换处理函数
    def perspective_transform(input_image):
        # 定义四个输入图像的角点坐标
        rows, cols, _ = input_image.shape
        # 修改pts1和pts2的值以减小透射变换的弯曲程度
        pts1 = np.float32([[0, 0], [cols, 0], [0, rows], [cols, rows]])
        pts2 = np.float32([[30, 30], [cols - 50, 50], [50, rows - 50], [cols - 50, rows - 50]])
        # 计算投射矩阵
        matrix = cv2.getPerspectiveTransform(pts1, pts2)
        # 进行投射变换
        result_image = cv2.warpPerspective(input_image, matrix, (cols, rows))
        return result_image
    
    
    # 自定义卷积核
    def custom_filter(input_image):
        kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
        return cv2.filter2D(input_image, -1, kernel)
    
    
    # 图像旋转处理函数
    def rotate_image(input_image, rotation_angle):
        rows, cols, _ = input_image.shape
        matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), rotation_angle, 1)
        result_image = cv2.warpAffine(input_image, matrix, (cols, rows))
        return result_image
    
    
    # 创建 Gradio 接口
    input_image = gr.inputs.Image()
    method = gr.inputs.Radio(
        choices=["原图", "灰度化", "反转颜色", "平移", "直方图均衡化", "腐蚀", "膨胀", "均值滤波", "中值滤波", "高斯滤波",
                 "双边滤波", "方块滤波", "仿射变换", "透射变换", "图像旋转", "Sobel边缘检测", "Canny边缘检测", "自定义卷积核"], default="原图")
    
    rotation_angle = gr.inputs.Slider(minimum=-180, maximum=180, default=45, label="图像旋转: 旋转角度")
    iterations = gr.inputs.Slider(minimum=0, maximum=10, step=1, default=1, label="腐蚀: 腐蚀参数")
    dilation_iterations = gr.inputs.Slider(minimum=0, maximum=10, step=1, default=1, label="膨胀: 膨胀参数")
    translation_x = gr.inputs.Slider(minimum=-200, maximum=200, default=200, label="平移: X轴平移")
    translation_y = gr.inputs.Slider(minimum=-200, maximum=200, default=200, label="平移: Y轴平移")
    
    output_image = gr.outputs.Image(type="pil")
    
    
    # 创建函数根据下拉菜单的选择来执行不同的方法
    def apply_opencv_methods(input_image, method, rotation_angle, iterations, dilation_iterations,
                             translation_x, translation_y):
        if method == "原图":
            return original_image(input_image)
        elif method == "图像旋转":
            return rotate_image(input_image, rotation_angle)
        elif method == "腐蚀":
            return erosion(input_image, iterations)
        elif method == "膨胀":
            return dilation(input_image, dilation_iterations)
        elif method == "Sobel边缘检测":
            return sobel_edge_detection(input_image)
        elif method == "平移":
            return translate_image(input_image, translation_x, translation_y)
        elif method == "自定义卷积核":
            return custom_filter(input_image)
        else:
            methods = {
                "灰度化": grayscale,
                "Canny边缘检测": edge_detection,
                "反转颜色": invert_colors,
                "均值滤波": mean_blur,
                "中值滤波": median_blur,
                "高斯滤波": gaussian_blur,
                "双边滤波": bilateral_filter,
                "方块滤波": box_filter,
                "仿射变换": affine_transform,
                "透射变换": perspective_transform,
                "直方图均衡化": histogram_equalization,
            }
            return methods[method](input_image)
    
    
    # 创建 Gradio 接口
    gr.Interface(
        fn=apply_opencv_methods,
        inputs=[input_image, method, rotation_angle, iterations, dilation_iterations, translation_x,
                translation_y],
        outputs=output_image,
        live=True,
        title="图像处理初学者导引",
        description="选择一张图像, 并选择对应方法"
    ).launch(share=False)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
  • 相关阅读:
    在职场上有多少人输在了不会用Python数据分析
    Flutter实现ControlExecutor进行多个异步任务执行时监听状态并可指定最后执行的异步并在指定的异步执行完毕后结束executor并回调。
    tcr历史比赛竞赛规则
    Servlet是什么?
    1533_AURIX_TriCore内核架构_指令集信息
    GoogLeNet网络
    基于元数据的无代码平台设计与开发概述
    “数字驱动 智领未来”—传化化学项目启动会
    Mysq查询性能调优
    09 【Attributes继承 provide与inject】
  • 原文地址:https://blog.csdn.net/weixin_43694096/article/details/133468177