scipy中常用的图像处理

本文介绍了如何使用scipy的ndimage模块进行图像处理,包括高斯滤波、最大值滤波、中值滤波、最小值滤波和百分位滤波等滤波方法,以及图像的仿射变换、旋转和缩放等插值操作。通过实例展示了如何实现这些图像处理技术,并给出了相应的代码示例。

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

导读

scipy中也提供了不少关于图像处理的方法,主要封装在ndimage包中,这篇文章主要向大家来介绍一下这里面的一些图像处理方法的使用

图像滤波

  • 获取图片
from scipy import misc,ndimage
from matplotlib import pyplot as plt

#从网站上获取一张图片
img = misc.ascent()
#显示灰度图
plt.gray()
#显示图片
plt.imshow(f)
plt.show()

在这里插入图片描述

  • 滤波
    滤波通常被用于图像的去噪,scipy中也提供了很多的滤波算法
#高斯滤波,sigma设置高斯核的标准差
gauss_f_img = ndimage.gaussian_filter(img,sigma=3)
#最大值滤波
max_f_img = ndimage.maximum_filter(img,size=20)
#中值滤波
median_f_img = ndimage.median_filter(img,size=20)
#最小值滤波
min_f_img = ndimage.minimum_filter(img,size=20)
#百分位滤波
percentile_f_img = ndimage.percentile_filter(img,percentile=20,size=20)

#设置显示图片的list
img_list = [img,gauss_f_img,max_f_img,median_f_img,min_f_img,percentile_f_img]
title_list = ["origin","guassian filter","maximum filter","median filter","minimum filter","percentile filter"]
for i in range(len(img_list)):
    plt.subplot(2,3,i+1)
    #设置显示图片的标题
    plt.title(title_list[i])
    #显示图片
    plt.imshow(img_list[i])
    #关闭坐标轴的显示
    plt.axis("off")

plt.show()

在这里插入图片描述

图像插值

图像的插值在图像处理中应用也是非常广泛,例如:透视变换仿射变换平移缩放旋转等都能够看到它的身影,scipy中也提供了几个函数用于图像插值,下面我们来看看

关于图像仿射变换的原理,大家可以参考我的这篇文章一文搞懂仿射变换

  • 仿射变换
#定义一个图像的平移矩阵
M = np.array([[1,0,10],[0,1,30]])
#仿射变换
affine_img = ndimage.affine_transform(img,M)
plt.imshow(affine_img)

plt.show()

在这里插入图片描述

  • 图像旋转
#旋转图像
#reshape设置是否显示所有的图像,True显示所有,False则会对图像进行裁剪
rotate_img = ndimage.rotate(img,45,reshape=False)
plt.imshow(rotate_img)
plt.show()

在这里插入图片描述

  • 图像的缩放
#将图像缩小到原来的1/2
zoom_img = ndimage.zoom(img,0.5)
plt.imshow(zoom_img)
plt.show()

在这里插入图片描述

  • 几何插值
    通过geometric_transform我们可以实现几何插值,只需要定义一个图像位置变换的函数即可,下面定义的shift_func函数可以将图像向左下角移动1个像素,越界的位置默认使用0进行填充
a = np.arange(0,12).reshape((4,3))
def shift_func(output_coords):
    #定义插值的函数
    return (output_coords[0] - 1,output_coords[1] - 1)
#根据数据的坐标来变换坐标的像素值
t_a = ndimage.geometric_transform(a,shift_func)
"""
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[[0 0 0]
 [0 0 1]
 [0 3 4]
 [0 6 7]]
"""
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

修炼之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值