Computer image processing - Gaussian blur

1. Experiment introduction

1. Experimental content

In this experiment, we will learn about Gaussian Blur.

2. Experimental points

  • Gaussian blur image
  • Testing performance with a high-pass filter

3. Experimental environment

  • Python 3.6.6
  • numpy
  • matplotlib
  • cv2

2. Experimental steps

1 Import resources and display images

import numpy as np
import matplotlib.pyplot as plt
import cv2

%matplotlib inline

# 读入图像
image = cv2.imread('images/birds.jpg')

# 制作图像副本
image_copy = np.copy(image)

# 将颜色更改为RGB(从BGR)
image_copy = cv2.cvtColor(image_copy, cv2.COLOR_BGR2RGB)

plt.imshow(image_copy)
<matplotlib.image.AxesImage at 0x7f71199c44a8>

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-VlMmhZvp-1686574312174)(output_2_1.png)]

2 Gaussian blur image

# 转换为灰度用于过滤
gray = cv2.cvtColor(image_copy, cv2.COLOR_RGB2GRAY)

# 创建高斯模糊图像
gray_blur = cv2.GaussianBlur(gray, (9, 9), 0)

f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))

ax1.set_title('original gray')
ax1.imshow(gray, cmap='gray')

ax2.set_title('blurred image')
ax2.imshow(gray_blur, cmap='gray')
<matplotlib.image.AxesImage at 0x7f7119956748>

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-aqhgTNUm-1686574312175)(output_4_1.png)]

3 Testing performance with a high-pass filter

# 高通滤波器

# 3x3 Sobel滤波器用于边缘检测
sobel_x = np.array([[ -1, 0, 1], 
                   [ -2, 0, 2], 
                   [ -1, 0, 1]])


sobel_y = np.array([[ -1, -2, -1], 
                   [ 0, 0, 0], 
                   [ 1, 2, 1]])


# 使用filter2D过滤原始和模糊的灰度图像
filtered = cv2.filter2D(gray, -1, sobel_x)

filtered_blurred = cv2.filter2D(gray_blur, -1, sobel_y)
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))

ax1.set_title('original gray')
ax1.imshow(filtered, cmap='gray')

ax2.set_title('blurred image')
ax2.imshow(filtered_blurred, cmap='gray')
<matplotlib.image.AxesImage at 0x7f7119897ef0>

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-mXTUOOLF-1686574312176)(output_6_1.png)]

# 创建一个阈值,将所有过滤的像素设置为白色
# 在一定的阈值之上

retval, binary_image = cv2.threshold(filtered_blurred, 50, 255, cv2.THRESH_BINARY)

plt.imshow(binary_image, cmap='gray')

<matplotlib.image.AxesImage at 0x7f71197af470>

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-zRTkn8En-1686574312176)(output_7_1.png)]

Guess you like

Origin blog.csdn.net/chenyu128/article/details/131176754