日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2025/4/5 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。