验证性实验 - 高斯模糊

该实验介绍了如何使用Python3和OpenCV库进行图像处理,包括读取图像、高斯模糊、颜色空间转换、Sobel滤波器进行边缘检测以及二值化处理。实验中,原始图像经过高斯模糊后,再应用Sobel滤波器以检测边缘,最后生成二值图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、实验介绍

1. 实验内容

本实验将学习高斯模糊。

2. 实验要点

  • 高斯模糊图像
  • 使用高通滤波器测试性能

3. 实验环境

  • Python 3.6.6
  • numpy
  • matplotlib
  • cv2

二、实验步骤

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

# 读入图像
image = cv2.imread('brain.png')

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

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

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

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

# 高通滤波器

# 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)

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

p1 = plt.figure()

ax0 = p1.add_subplot(2, 3, 1)
plt.imshow(gray, cmap='gray')

ax1 = p1.add_subplot(2, 3, 2)
plt.imshow(gray_blur, cmap='gray')

ax2 = p1.add_subplot(2, 3, 3)
plt.imshow(filtered, cmap='gray')

ax3 = p1.add_subplot(2, 3, 4)
plt.imshow(filtered_blurred, cmap='gray')

ax4 = p1.add_subplot(2, 3, 5)
plt.imshow(binary_image, cmap='gray')

plt.show()

三、实验现象

在这里插入图片描述

从左往右,再从上往下依次是:原始灰度图像,高斯模糊图像,原始sobel边缘检测图像,高斯模糊sobel边缘检测图像,二值图像

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值