├── README.md └── opencv_demo.py /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # OpenCV 方法演示项目 4 | 5 |
6 | Snipaste_2023-10-03_16-43-46(1) 7 |
8 | 9 | ## 项目简介 10 | 11 | 这个开源项目是一个用于演示 OpenCV 方法的工具,旨在帮助初学者快速理解和掌握 OpenCV 图像处理技术。通过这个项目,你可以轻松地对图像进行各种处理,从灰度化到边缘检测,以及更多其他方法。项目使用 Gradio 创建用户友好的界面,让用户能够轻松选择不同的图像处理方法和参数。 12 | 13 | ## 为什么选择这个项目 14 | 15 | - **教育性**:这个项目的主要目的是教育。它提供了对 OpenCV 方法的实际演示,以帮助初学者更好地理解和掌握这些技术。 16 | 17 | - **互动性**:通过 Gradio 创建的用户界面,用户可以立即看到不同处理方法的效果,并可以自己调整参数,以更深入地理解每种方法的工作原理。 18 | 19 | - **适用广泛**:这个项目可以帮助广大初学者,无论是学习计算机视觉、图像处理,还是对 OpenCV 有兴趣的人都会受益。 20 | 21 | ## 特性 22 | 23 | - 提供了多种 OpenCV 图像处理方法的演示,包括灰度化、反转颜色、平移、直方图均衡化、腐蚀、膨胀、均值滤波、中值滤波、高斯滤波等。 24 | 25 | - 支持自定义卷积核,允许用户尝试不同的卷积核来处理图像。 26 | 27 | - 提供图像旋转、仿射变换和透射变换的演示,以及选择角度和参数的选项。 28 | 29 | - 使用 Gradio 创建用户友好的界面,让用户能够轻松选择不同的图像处理方法和参数。 30 | 31 | ## 使用方法 32 | 33 | 1. **获取项目**:首先,你需要将这个项目克隆到你的本地计算机上。你可以使用以下命令来获取项目: 34 | 35 | ```bash 36 | git clone https://github.com/WangQvQ/opencv-tutorial.git 37 | ``` 38 | 39 | 2. **安装依赖项**:确保你已经安装了以下依赖项: 40 | 41 | - OpenCV 42 | - Gradio 43 | - NumPy 44 | 45 | 如果你没有安装它们,你可以使用以下命令安装: 46 | 47 | ```bash 48 | pip install opencv-python-headless=4.7.0.72 gradio=3.1.5 numpy=1.22.4 49 | ``` 50 | 51 | 3. **运行项目**:使用以下命令来运行项目: 52 | 53 | ```bash 54 | python opencv_demo.py 55 | ``` 56 | 57 | 运行后,你将看到一个网址,通常是 `http://localhost:7860`,你可以在浏览器中访问它。 58 | 59 | 4. **使用界面**:在浏览器中,你可以上传图像并选择不同的处理方法和参数,然后查看处理后的图像效果。 60 | 61 | ## 示例代码 62 | 63 |
Snipaste_2023-10-03_16-43-35
64 | 65 | 66 | 67 | 以下是部分方法的代码示例: 68 | 69 | ```python 70 | # 灰度化处理函数 71 | def grayscale(input_image): 72 | gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) 73 | return gray_image 74 | 75 | 76 | # 平移图像处理函数 77 | def translate_image(input_image, translation_x, translation_y): 78 | rows, cols, _ = input_image.shape 79 | translation_matrix = np.float32([[1, 0, translation_x], [0, 1, translation_y]]) 80 | translated_image = cv2.warpAffine(input_image, translation_matrix, (cols, rows)) 81 | return translated_image 82 | 83 | 84 | # Canny 边缘检测处理函数 85 | def edge_detection(input_image): 86 | edges = cv2.Canny(input_image, 100, 200) 87 | return edges 88 | ``` 89 | 90 | ## 贡献 91 | 92 | 如果你对项目有任何改进或建议,欢迎贡献代码或提出问题。我们欢迎开发者共同改进这个项目,以使其更加有用和友好。如果你想贡献,请查看我们的[贡献指南](CONTRIBUTING.md)。 93 | 94 | -------------------------------------------------------------------------------- /opencv_demo.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import gradio as gr 3 | import numpy as np 4 | 5 | 6 | # 原始图像处理函数 7 | def original_image(input_image): 8 | return input_image 9 | 10 | 11 | # 灰度化处理函数 12 | def grayscale(input_image): 13 | gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) 14 | return gray_image 15 | 16 | 17 | # 平移图像处理函数 18 | def translate_image(input_image, translation_x, translation_y): 19 | rows, cols, _ = input_image.shape 20 | translation_matrix = np.float32([[1, 0, translation_x], [0, 1, translation_y]]) 21 | translated_image = cv2.warpAffine(input_image, translation_matrix, (cols, rows)) 22 | return translated_image 23 | 24 | 25 | # Canny 边缘检测处理函数 26 | def edge_detection(input_image): 27 | edges = cv2.Canny(input_image, 100, 200) 28 | return edges 29 | 30 | 31 | # Sobel 边缘检测处理函数 32 | def sobel_edge_detection(input_image): 33 | gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) 34 | sobel_x = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=5) 35 | sobel_y = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=5) 36 | sobel_magnitude = cv2.magnitude(sobel_x, sobel_y) 37 | sobel_magnitude = np.uint8(255 * sobel_magnitude / np.max(sobel_magnitude)) 38 | return sobel_magnitude 39 | 40 | 41 | # 反转颜色处理函数 42 | def invert_colors(input_image): 43 | inverted_image = cv2.bitwise_not(input_image) 44 | return inverted_image 45 | 46 | 47 | # 腐蚀处理函数 48 | def erosion(input_image, iterations): 49 | kernel = np.ones((5, 5), np.uint8) 50 | eroded_image = cv2.erode(input_image, kernel, iterations=iterations) 51 | return eroded_image 52 | 53 | 54 | # 膨胀处理函数 55 | def dilation(input_image, dilation_iterations): 56 | kernel = np.ones((5, 5), np.uint8) 57 | dilated_image = cv2.dilate(input_image, kernel, iterations=dilation_iterations) 58 | return dilated_image 59 | 60 | 61 | # 均值滤波处理函数 62 | def mean_blur(input_image): 63 | mean_blurred_image = cv2.blur(input_image, (5, 5)) 64 | return mean_blurred_image 65 | 66 | 67 | # 中值滤波处理函数 68 | def median_blur(input_image): 69 | median_blurred_image = cv2.medianBlur(input_image, 5) 70 | return median_blurred_image 71 | 72 | 73 | # 高斯滤波处理函数 74 | def gaussian_blur(input_image): 75 | gaussian_blurred_image = cv2.GaussianBlur(input_image, (5, 5), 0) 76 | return gaussian_blurred_image 77 | 78 | 79 | # 双边滤波处理函数 80 | def bilateral_filter(input_image): 81 | bilateral_filtered_image = cv2.bilateralFilter(input_image, 9, 75, 75) 82 | return bilateral_filtered_image 83 | 84 | 85 | # 方块滤波处理函数 86 | def box_filter(input_image): 87 | box_filtered_image = cv2.boxFilter(input_image, -1, (5, 5)) 88 | return box_filtered_image 89 | 90 | 91 | # 直方图均衡化处理函数 92 | def histogram_equalization(input_image): 93 | gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) 94 | equalized_image = cv2.equalizeHist(gray_image) 95 | return cv2.cvtColor(equalized_image, cv2.COLOR_GRAY2BGR) 96 | 97 | 98 | # 仿射变换处理函数 99 | def affine_transform(input_image): 100 | # 创建仿射变换矩阵 101 | rows, cols, _ = input_image.shape 102 | matrix = cv2.getRotationMatrix2D((cols / 4, rows / 2), 70, 0.5) # 90度旋转和1.5倍缩放 103 | result_image = cv2.warpAffine(input_image, matrix, (cols, rows)) 104 | return result_image 105 | 106 | 107 | # 透射变换处理函数 108 | def perspective_transform(input_image): 109 | # 定义四个输入图像的角点坐标 110 | rows, cols, _ = input_image.shape 111 | # 修改pts1和pts2的值以减小透射变换的弯曲程度 112 | pts1 = np.float32([[0, 0], [cols, 0], [0, rows], [cols, rows]]) 113 | pts2 = np.float32([[30, 30], [cols - 50, 50], [50, rows - 50], [cols - 50, rows - 50]]) 114 | # 计算投射矩阵 115 | matrix = cv2.getPerspectiveTransform(pts1, pts2) 116 | # 进行投射变换 117 | result_image = cv2.warpPerspective(input_image, matrix, (cols, rows)) 118 | return result_image 119 | 120 | 121 | # 自定义卷积核 122 | def custom_filter(input_image): 123 | kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]]) 124 | return cv2.filter2D(input_image, -1, kernel) 125 | 126 | 127 | # 图像旋转处理函数 128 | def rotate_image(input_image, rotation_angle): 129 | rows, cols, _ = input_image.shape 130 | matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), rotation_angle, 1) 131 | result_image = cv2.warpAffine(input_image, matrix, (cols, rows)) 132 | return result_image 133 | 134 | 135 | # 创建 Gradio 接口 136 | input_image = gr.inputs.Image() 137 | method = gr.inputs.Radio( 138 | choices=["原图", "灰度化", "反转颜色", "平移", "直方图均衡化", "腐蚀", "膨胀", "均值滤波", "中值滤波", "高斯滤波", 139 | "双边滤波", "方块滤波", "仿射变换", "透射变换", "图像旋转", "Sobel边缘检测", "Canny边缘检测", "自定义卷积核"], default="原图") 140 | 141 | rotation_angle = gr.inputs.Slider(minimum=-180, maximum=180, default=45, label="图像旋转: 旋转角度") 142 | iterations = gr.inputs.Slider(minimum=0, maximum=10, step=1, default=1, label="腐蚀: 腐蚀参数") 143 | dilation_iterations = gr.inputs.Slider(minimum=0, maximum=10, step=1, default=1, label="膨胀: 膨胀参数") 144 | translation_x = gr.inputs.Slider(minimum=-200, maximum=200, default=200, label="平移: X轴平移") 145 | translation_y = gr.inputs.Slider(minimum=-200, maximum=200, default=200, label="平移: Y轴平移") 146 | 147 | output_image = gr.outputs.Image(type="pil") 148 | 149 | 150 | # 创建函数根据下拉菜单的选择来执行不同的方法 151 | def apply_opencv_methods(input_image, method, rotation_angle, iterations, dilation_iterations, 152 | translation_x, translation_y): 153 | if method == "原图": 154 | return original_image(input_image) 155 | elif method == "图像旋转": 156 | return rotate_image(input_image, rotation_angle) 157 | elif method == "腐蚀": 158 | return erosion(input_image, iterations) 159 | elif method == "膨胀": 160 | return dilation(input_image, dilation_iterations) 161 | elif method == "Sobel边缘检测": 162 | return sobel_edge_detection(input_image) 163 | elif method == "平移": 164 | return translate_image(input_image, translation_x, translation_y) 165 | elif method == "自定义卷积核": 166 | return custom_filter(input_image) 167 | else: 168 | methods = { 169 | "灰度化": grayscale, 170 | "Canny边缘检测": edge_detection, 171 | "反转颜色": invert_colors, 172 | "均值滤波": mean_blur, 173 | "中值滤波": median_blur, 174 | "高斯滤波": gaussian_blur, 175 | "双边滤波": bilateral_filter, 176 | "方块滤波": box_filter, 177 | "仿射变换": affine_transform, 178 | "透射变换": perspective_transform, 179 | "直方图均衡化": histogram_equalization, 180 | } 181 | return methods[method](input_image) 182 | 183 | 184 | # 创建 Gradio 接口 185 | gr.Interface( 186 | fn=apply_opencv_methods, 187 | inputs=[input_image, method, rotation_angle, iterations, dilation_iterations, translation_x, 188 | translation_y], 189 | outputs=output_image, 190 | live=True, 191 | title="图像处理初学者导引", 192 | description="选择一张图像, 并选择对应方法" 193 | ).launch(share=False) 194 | --------------------------------------------------------------------------------