欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python彩色图像如何进行高斯滤波ValueError: correlate2d inputs must both be 2-D arrays解决方法

发布时间:2025/4/5 python 55 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python彩色图像如何进行高斯滤波ValueError: correlate2d inputs must both be 2-D arrays解决方法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

      • 遇到的问题
      • 解决方法
      • 参考

遇到的问题

在执行高斯滤波的代码时,遇到下列问题

ValueError: correlate2d inputs must both be 2-D arrays

进行试验的代码

import numpy as np from scipy import signal from skimage import data from matplotlib import pyplot as plt from skimage.io import imread import mathdef correl2d(img, window):"""二维灰度图像的空间滤波函数"""# 使用滤波器实现图像的空间相关 # mode = 'same'表示输出尺寸等于输入尺寸 # boundary = 'fill' 表示滤波前,用常量值填充原始图像的边缘,默认常量值为0s = 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))def gauss_window(radius, sigma):"""定义radius * radius 的高斯平滑模板"""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为原始图像 img = imread("4.1.04.tiff") # 3*3 高斯平滑滤波模板 window_1 = gauss_window(3, 1.0)# 生成滤波结果 new_img_1 = correl2d(img, window_1)# 显示图像 plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文 fig, axs = plt.subplots(1, 2) axs[0].imshow(img, cmap='gray') axs[0].set_title("摄影师原图") axs[1].imshow(new_img_1, cmap='gray') axs[1].set_title("3*3 高斯平滑滤波模板")plt.tight_layout() plt.show()

解决方法

多个通道分别进行滤波

window = gauss_window(3, 1.0) for i in range(3):new_img[:,:,i]= correl2d(img[:, :, i],window)

代码测试成功

完整测试代码

import numpy as np from scipy import signal from skimage import data from matplotlib import pyplot as plt from skimage.io import imread import mathdef correl2d(img, window):"""二维灰度图像的空间滤波函数"""# 使用滤波器实现图像的空间相关 # mode = 'same'表示输出尺寸等于输入尺寸 # boundary = 'fill' 表示滤波前,用常量值填充原始图像的边缘,默认常量值为0s = 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))def gauss_window(radius, sigma):"""定义radius * radius 的高斯平滑模板"""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为原始图像 img = imread("4.1.04.tiff") new_img =np.zeros(img.shape) # 3*3 高斯平滑滤波模板window = gauss_window(3, 1.0) for i in range(3):new_img[:,:,i]= correl2d(img[:, :, i],window) # 显示图像 plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文 fig, axs = plt.subplots(1, 2) axs[0].imshow(img, cmap='gray') axs[0].set_title("摄影师原图") axs[1].imshow(new_img.astype('uint8')) axs[1].set_title("3*3 高斯平滑滤波模板")plt.tight_layout() plt.show()

参考

[1]https://stackoverflow.com/questions/51562341/valueerror-convolve2d-inputs-must-be-both-2d-arrays

总结

以上是生活随笔为你收集整理的python彩色图像如何进行高斯滤波ValueError: correlate2d inputs must both be 2-D arrays解决方法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。