图像处理利用PCA算法的图像预处理问题:Convert image into 4x4 non-overlapping blocks
为了进一步结合Numpy进行处理,推荐使用scikit-image库,该库有专门的view_as_blocks实现该功能
Code:
from skimage import data
from skimage import color
from skimage.util import view_as_blocks
# get astronaut from skimage.data in grayscale
l = color.rgb2gray(data.astronaut())
# size of blocks
block_shape = (4, 4)
# see astronaut as a matrix of blocks (of shape block_shape)
view = view_as_blocks(l, block_shape)
# collapse the last two dimensions in one
flatten_view = view.reshape(view.shape[0], view.shape[1], -1)
print(flatten_view.shape)
Output
(128, 128, 16) # 128x128 blocks a 16 elements each
color(http://scikit-image.org/docs/dev/api/skimage.color.html)
util(http://scikit-image.org/docs/dev/api/skimage.util.html)模块主要为处理图像格式的小工具库
view_as_blocks函数示例分析
import numpy as np
from scipy import ndimage as ndi
#ndimage用于多维图像的滤波、转换、测算、形态学处理
from matplotlib import pyplot as plt
import matplotlib.cm as cm
from skimage import data
from skimage import color
from skimage.util import view_as_blocks
# get astronaut from skimage.data in grayscale
l = color.rgb2gray(data.astronaut())
# size of blocks
block_shape = (4, 4)
# see astronaut as a matrix of blocks (of shape block_shape)
view = view_as_blocks(l, block_shape)
# collapse the last two dimensions in one
flatten_view = view.reshape(view.shape[0], view.shape[1], -1)
# resampling the image by taking either the `mean`,
# the `max` or the `median` value of each blocks.
mean_view = np.mean(flatten_view, axis=2)
max_view = np.max(flatten_view, axis=2)
median_view = np.median(flatten_view, axis=2)
# display resampled images
fig, axes = plt.subplots(2, 2, figsize=(8, 8), sharex=True, sharey=True)
ax = axes.ravel()
l_resized = ndi.zoom(l, 2, order=3)
ax[0].set_title("Original rescaled with\n spline interpolation (order=3)")
ax[0].imshow(l_resized, extent=(0, 128, 128, 0), interpolation='nearest',
cmap=cm.Greys_r)
ax[1].set_title("Block view with\n local mean pooling")
ax[1].imshow(mean_view, interpolation='nearest', cmap=cm.Greys_r)
ax[2].set_title("Block view with\n local max pooling")
ax[2].imshow(max_view, interpolation='nearest', cmap=cm.Greys_r)
ax[3].set_title("Block view with\n local median pooling")
ax[3].imshow(median_view, interpolation='nearest', cmap=cm.Greys_r)
for a in ax:
a.set_axis_off()
fig.tight_layout()
plt.show()
Numpy.reshape函数中参数-1的理解
Numpy.reshape函数有两种形式如下
b=np.reshape(a,(2,-1))
c=a.reshape(2,-1)
示例代码中参数-1的含义是根据原shape的元素格式和参数给出的行数或者列数推算相应的newshape
具体可参考:what-does-1-mean-in-numpy-reshape
关于参数axis
以m * n矩阵举例:
axis 不设置值,对 m*n 个数求均值,返回一个实数
axis = 0:压缩行,对各列求均值,返回 1* n 矩阵
axis =1 :压缩列,对各行求均值,返回 m *1 矩阵
举例:
>>> import numpy as np
>>> num1 = np.array([[1,2,3],[2,3,4],[3,4,5],[4,5,6]])
>>> now2 = np.mat(num1)
>>> now2
matrix([[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6]])
>>> np.mean(now2) # 对所有元素求均值
3.5
>>> np.mean(now2,0) # 压缩行,对各列求均值
matrix([[ 2.5, 3.5, 4.5]])
>>> np.mean(now2,1) # 压缩列,对各行求均值
matrix([[ 2.],
[ 3.],
[ 4.],
[ 5.]])
参考资料:https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html
https://blog.csdn.net/xiaoqinting2015/article/details/69936952
https://blog.csdn.net/taotiezhengfeng/article/details/72397282
https://stackoverflow.com/questions/18691084/what-does-1-mean-in-numpy-reshape
http://scikit-image.org/docs/dev/auto_examples/numpy_operations/plot_view_as_blocks.html?highlight=block