guided_filter 引导滤波在图像处理和高光谱图像处理中的应用 代码

本文仅限于直接调用openCV的函数引导滤波函数,而不是自己编写。
本文不讲解引导滤波原理,默认我们都已经知道了大概。

opencv中引导滤波函数的调用:

import cv2
dst = cv2.ximgproc.guidedFilter(guide=guided_image, src=img_one, radius=7, eps=1000, dDepth=-1)

# 其中guided_image 为引导图
# img_one 为高光谱单通道图像,试过使用多通道(100个)的高光谱图像, 没能输出正常的结果。
# radius 引导滤波的窗口
# eps 会显著影响模糊范围
# dDepth一般为-1,表示输出与输入src的通道数一致。

opencv中引导滤波函数处理彩色jpg图像(亲测可用,忘记从哪里找到的代码,向原创作表示感激感谢感恩,也为无法知会原创而致以诚挚歉意):

import argparse  # 导包
import cv2
import matplotlib.pyplot as plt
import skimage
import numpy as np

img=cv2.imread('C:/Users/nudtl/Desktop/fly.jpg')  # cv2.imread()接口读图像,读进来直接是BGR 格式数据格式在 0~255,通道格式为(W,H,C)
print(img.dtype)

# img=img[:,:,::-1]  
# img[:,:,::-1]的作用就是实现RGB到BGR通道的转换 (若图片一开始就是BGR的,就是实现从BGR到RGB的转换)
 # 列表img进行img[:,:,::-1]的作用是列表数组左右翻转

guide=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) #用灰度图作为引导
print(guide.dtype)

dst1=cv2.ximgproc.guidedFilter(guide=guide,src=img,radius=7,eps=10,dDepth=-1)
print(dst1.shape)

if __name__=='__main__':
    cv2.imshow("Img",img)  #在窗口显示图像
    cv2.imshow("dst1",dst1)
    cv2.waitKey(0)  #显示图像必备

高光谱图像HSI 引导滤波的应用:

import numpy as np
import scipy.io as scio
import matplotlib.pyplot as plt
import cv2
from sklearn.decomposition import PCA

# PCA降维使用第一个主成分作为引导图像
def applyPCA(X, numcomponents=20):
    newx = np.reshape(X,(-1, X.shape[2]))
    pca = PCA(n_components=numcomponents, whiten=True)
    newx = pca.fit_transform(newx)
    newx = np.reshape(newx, (X.shape[0], X.shape[1], numcomponents))
    ratios = pca.explained_variance_ratio_
    scale = np.sum(ratios)
    return newx, scale

data = scio.loadmat('C:/Users/nudtl/Desktop/random_label_ijrs/houston/houston.mat')['houston']

# 数据归一化
data = data.astype(float)
data -= np.min(data)
data /= np.max(data)

data = 255 * data
data = data.astype('uint8')

X, scale = applyPCA(data, numcomponents=1)

guided_image = X[:,:, 0]
guided_image -= np.min(guided_image)
guided_image /= np.max(guided_image)
guided_image = guided_image*255
guided_image = guided_image.astype('uint8')
guided_image = np.array(guided_image)

print(guided_image.shape)
print(np.max(guided_image))

H = data.shape[0]
W = data.shape[1]
B = data.shape[2]
print(H,W,B)

print(data.dtype, guided_image.dtype)

datacubes = np.zeros_like(data)

# 对于高光谱图像的多个通道,每次只处理一个通道的图片,通过循环完成所有通道图像的滤波。这一点与openCV的bilateral_filter 类似
for b in range(B):
    img_one = data[:,:, b]
    dst = cv2.ximgproc.guidedFilter(guide=guided_image, src=img_one, radius=7, eps=1000, dDepth=-1)
    datacubes[:,:,b] = dst

plt.subplot(1,2,1)
plt.imshow(data[:,:,10])
plt.subplot(1,2,2)
plt.imshow(datacubes[:,:,10])
plt.show()
发布了34 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_39393430/article/details/103672958
今日推荐