Image processing commonly used in scipy

guide

scipyThere are also many methods about image processing, which are mainly encapsulated in ndimagethe package. This article mainly introduces the use of some image processing methods here.

image filtering

  • get picture
from scipy import misc,ndimage
from matplotlib import pyplot as plt

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

insert image description here

  • Filtering
    滤波is usually used for image denoising, and many filtering algorithms are also provided in 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()

insert image description here

image interpolation

Image interpolation is also widely used in image processing, for example: 透视变换, 仿射变换, 平移, 缩放, 旋转etc. can be seen. scipySeveral functions are also provided for image interpolation. Let's take a look below

Regarding the principle of image affine transformation, you can refer to my article to understand affine transformation

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

plt.show()

insert image description here

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

insert image description here

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

insert image description here

  • Geometric interpolation We can achieve geometric interpolation
    by geometric_transformdefining a function for image position transformation. The function defined below shift_funccan move the image to the lower left corner by 1 pixel, and the out-of-bounds position is filled with 0 by default.
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]]
"""

Guess you like

Origin blog.csdn.net/sinat_29957455/article/details/124660862