将图像分解和收集到碎片100x100 python

时间:2018-05-20 03:29:29

标签: python arrays image numpy

我有图像阵列,我需要在100x100的碎片上分解它,然后用它进行操作收集回原始图像。 问题:我无法回收件,我有类似的东西,

但真实图像为800x800

我的代码:

将img作为数组,删除第三维

path_to_image = './la3.jpg'
image_array = plt.imread(path_to_image)
image_array = image_array[:, :, 0]

写入新的数组片100x100(工作正常):

main2_array = np.zeros(10000,)
for row in tqdm_notebook(range(0,8)):

    for col in range(0,8):
        main2_array = np.vstack((main2_array, image_array[0 + (100*row):100 + (100*row) ,0 + (100*col):100 + (100*col)].flatten()))

main2_array = np.delete(main2_array, main2_array[0] , axis=0  )

收回件(它不起作用)

main_array = np.zeros(100,)

for p in tqdm_notebook(range(0,100)):
    for i in range(0,64):
        main_array = np.vstack((main_array, main2_array[0 + (10000*i) + (100*p): 100 + (10000*i) + (100*p)]))

main_array = np.delete(main_array, main_array[0] , axis=0  )     

收集件后我得到了

image

2 个答案:

答案 0 :(得分:0)

假图像

In [7]: parser.parse('Tue, 27 May 2014 20:06:08 +0800 (GMT+08:00)', fuzzy=True)
Out[7]: datetime.datetime(2014, 5, 27, 20, 6, 8, tzinfo=tzoffset(None, 28800))

In [8]: parser.parse('Thu, 16 Dec 2010 12:14:05 +0000', fuzzy=True)
Out[8]: datetime.datetime(2010, 12, 16, 12, 14, 5, tzinfo=tzutc())

解构后,x, y = 800,800 img_array = np.arange(x*y).reshape((x,y)) 为(64,10000);每行是一个扁平的100x100补丁。在解构过程中,您遍历图像从左到右从上到下并将每个补丁滑到上一个补丁下面。

重建逆转过程:

main2_array.shape

或者你可以重新塑造回原来的

main_array = np.zeros((x,y))
for n, patch in enumerate(main2_array):
    patch = patch.reshape(100,100)
    # eight patches per row
    row, col = divmod(n, 8)
    row_offset, col_offset = row*100, col*100
    row_slice = slice(row_offset, 100 + row_offset)
    col_slice = slice(col_offset, 100 + col_offset)
    #print(np.all(patch == image_array[row_slice,col_slice]))
    main_array[row_slice, col_slice] = patch


>>> np.all(main_array == img_array)
True
>>> 

答案 1 :(得分:0)

有点晚了,但我相信这个问题应该得到一个更加图解的答案。

您不需要慢速循环,您可以使用numpy重塑和精美索引来完成所有操作。

让我们从示例图像开始

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt  
import skimage.transform
import skimage.data

img = skimage.data.chelsea()
# crop from (300, 451, 3) to (300, 300, 3)
img = img[:,80:380,:]
# resize to (800, 800)
img = skimage.transform.resize(img, (800,800))
plt.imshow(img)

enter image description here

64 100*100个图块中分解图像。新形状为(8, 100, 8, 100, 3),您可以使用img[i, :, j, :, ...]处理单张图片。除了可能更容易阅读之外,无需将它们存储在新阵列中。

img = img.reshape(8, 100, 8, 100, 3)
gs = mpl.gridspec.GridSpec(8,8)
for i in range(8):
    for j in range(8):
        ax = plt.subplot(gs[i,j])
        ax.imshow(img[i,:,j,:,...])

enter image description here

现在让我们对瓷砖进行一些操作。

清除一些随机磁贴

cells = np.random.randint(8, size=(20,2))
img[cells[:,0],:,cells[:,1],...] = 1

翻转并从左向右翻转

img = img[:,::-1,:,::-1,...]

添加黑色边框

img[:,:6,...] = 0
img[:,-6:,...] = 0
img[:,:,:,:6,...] = 0
img[:,:,:,-6:,...] = 0

并绘制他们

for i in range(8):
    for j in range(8):
        ax = plt.subplot(gs[i,j])
        ax.imshow(img[i,:,j,:,...])

enter image description here

现在重建你可以重塑到原始形状

img = img.reshape(800, 800, 3)
plt.imshow(img)

enter image description here