Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3592460
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2021-04-16 17:13:22

平滑线性空间滤波器

盒状态滤波

import numpy as np

from scipy import signal

from skimage import data, io

from matplotlib import pyplot as plt

# 定义二维灰度图像的空间滤波函数

def correl2d(img, window):

    s = signal.correlate2d(img, window, mode='same', boundary='fill')

    return s.astype(np.uint8)

img = data.grass()

#img = io.imread('tomato.jpg')

# 3*3 盒状滤波模板

window1 = np.ones((3, 3)) / (3 ** 2)

# 5*5 盒状滤波模板

window2 = np.ones((5, 5)) / (5 ** 2)

# 9*9 盒状滤波模板

window3 = np.ones((9, 9)) / (9 ** 2)

# 生成滤波结果

img1 = correl2d(img, window1)

img2 = correl2d(img, window2)

img3 = correl2d(img, window3)

plt.figure()

plt.imshow(img)

plt.figure()

plt.imshow(img1)

plt.figure()

plt.imshow(img2)

plt.figure()

plt.imshow(img3)

plt.show()

高斯平滑滤波

使用data自带图像

import numpy as np

from scipy import signal

from skimage import data, io

from matplotlib import pyplot as plt

import math

# 定义二维灰度图像的空间滤波函数

def correl2d(img, window):

    #mode='same' 表示输出尺寸等于输入尺寸

    #boundary=fill’ 表示滤波前,用常量值填充原始图像的边缘,默认常量值为0

    s = signal.correlate2d(img, window, mode='same', boundary='fill')

    return s.astype(np.uint8)

# 定义二维高斯函数

def gauss(i, j, sigma):

    return 1 / (2 * math.pi * sigma ** 2) * math.exp(-(i ** 2 + j ** 2) / (2 * sigma ** 2))

# 定义radius*radius的高斯平滑模板

def gauss_window(radius, sigma):

    window = np.zeros((radius * 2 + 1, radius * 2 + 1))

    for i in range(-radius, radius + 1):

        for j in range(-radius, radius + 1):

            window[i + radius][j + radius] = gauss(i, j, sigma)

    return window / np.sum(window)

img = data.grass()

# 3*3 高斯平滑滤波模板

window1 = gauss_window(3, 1.0)

# 5*5 高斯平滑滤波模板

window2 = gauss_window(5, 1.0)

# 9*9 高斯平滑滤波模板

window3 = gauss_window(9, 1.0)

# 生成滤波结果

img1 = correl2d(img, window1)

img2 = correl2d(img, window2)

img3 = correl2d(img, window3)

plt.figure()

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

plt.figure()

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

plt.figure()

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

plt.figure()

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

plt.show()

导入外部图像:

import cv2

import numpy as np

from scipy import ndimage

img1 = cv2.imread('tomato.jpg')

gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)  # 灰色

cv2.imwrite('gray.jpg', gray)

kernel_3x3 = np.array([

    [1, 2, 1],

    [2, 4, 2],

    [1, 2, 1]

])

kernel_5x5 = np.array([

    [1, 4, 7, 4, 1],

    [4, 16, 26, 16, 4],

    [7, 26, 41, 26, 7],

    [4, 16, 26, 16, 4],

    [1, 4, 7, 4, 1]

])

kernel_3x3 = kernel_3x3 / kernel_3x3.sum()  # 加权平均

kernel_5x5 = kernel_5x5 / kernel_5x5.sum()  # 加权平均

img = cv2.imread("tomato.jpg", 0)

k3 = ndimage.convolve(img, kernel_3x3)

k5 = ndimage.convolve(img, kernel_5x5)

cv2.imshow("3x3", k3)

cv2.imshow("5x5", k5)

cv2.imwrite('k3.jpg', k3)

cv2.imwrite('k5.jpg', k5)

cv2.waitKey()

阅读(1222) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~