from skimage import transform,data
import matplotlib.pyplot as plt
img = data.camera()
dst=transform.resize(img, (80, 60))
plt.figure('resize')
<Figure size 640x480 with 0 Axes>
<Figure size 640x480 with 0 Axes>
将 camera
图片由原来的 512*512
大小,
变成了 80*60
大小。从下图中的坐标尺,我们能够看出来:
plt.subplot(121)
plt.title('before resize')
plt.imshow(img,plt.cm.gray)
plt.subplot(122)
plt.title('before resize')
plt.imshow(dst,plt.cm.gray)
plt.show()
img = data.camera()
图片原始大小:
print(img.shape)
(512, 512)
缩小为原来图片大小的0.1倍:
print(transform.rescale(img, 0.1).shape)
(51, 51)
缩小为原来图片行数一半,列数四分之一:
print(transform.rescale(img, [0.5,0.25]).shape)
(256, 128)
放大为原来图片大小的2倍:
print(transform.rescale(img, 2).shape)
(1024, 1024)
import matplotlib.pyplot as plt
img = data.camera()
图片原始大小:
print(img.shape)
(512, 512)
旋转60度,不改变大小:
img1=transform.rotate(img, 60)
print(img1.shape)
(512, 512)
旋转30度,同时改变大小:
img2=transform.rotate(img, 30,resize=True)
print(img2.shape)
(699, 699)
plt.figure('resize')
<Figure size 640x480 with 0 Axes>
<Figure size 640x480 with 0 Axes>
plt.subplot(121)
plt.title('rotate 60')
plt.imshow(img1,plt.cm.gray)
plt.subplot(122)
plt.title('rotate 30')
plt.imshow(img2,plt.cm.gray)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
载入宇航员图片:
image = data.astronaut()
plt.imshow(image)
<matplotlib.image.AxesImage at 0x7f931aaabb60>
image.shape
(512, 512, 3)
获取图片的行数,列数和通道数:
rows, cols, dim = image.shape
产生高斯金字塔图像,共生成了 log(512)=9
幅金字塔图像,
加上原始图像共10幅, pyramid[0]-pyramid[1]
:
pyramid = tuple(transform.pyramid_gaussian(image, downscale=2))
composite_image = np.ones((rows, cols + int(cols / 2), 3), dtype=np.double)
生成背景:
plt.imshow(composite_image)
<matplotlib.image.AxesImage at 0x7f931ab83a70>
composite_image[:rows, :cols, :] = pyramid[0]
融合原始图像
plt.imshow(composite_image)
<matplotlib.image.AxesImage at 0x7f931ab484a0>
循环融合9幅金字塔图像:
i_row = 0
for p in pyramid[3:]:
# print(p)
n_rows, n_cols = p.shape[:2]
composite_image[i_row:i_row + n_rows, cols:cols + n_cols] = p
i_row += n_rows
plt.imshow(composite_image)
plt.show()
上图,就是10张金字塔图像,下标为0的表示原始图像,后面每层的图像行和列变为上一层的一半,直至变为1。
除了高斯金字塔外,还有其它的金字塔,如:
import skimage
skimage.transform.pyramid_laplacian(image, downscale=2)
<generator object pyramid_laplacian at 0x7f0ee558b480>
plt.imshow(image)
plt.show()