【问题标题】:The decomposition and collection of back images to pieces 100x100 python100x100 python 背面图像的分解和收集
【发布时间】:2018-05-20 03:29:28
【问题描述】:

我有图像数组,我需要将其分解为 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  )     

收集碎片后我得到

【问题讨论】:

  • 什么是tqdm_notebook
  • tqdm_notebook 是查看进程状态的工具,例如下载行。 tqdm.github.io
  • 为什么要创建一维零数组,向其中添加行然后删除原始的一维零数组?
  • 您的 收集碎片 代码会产生 ValueError。请提供minimal reproducible example

标签: python arrays image numpy


【解决方案1】:

假图片

x, y = 800,800
img_array = np.arange(x*y).reshape((x,y))

解构后,main2_array.shape 为 (64,10000);每行都是一个扁平的 100x100 补丁。在解构过程中,您遍历图像从左到右从上到下,并将每个补丁滑到前一个补丁下方。

重构逆向过程:

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
>>> 

或者你可以重塑你的方式回到原来的方式

>>> b = main2_array.reshape(8,8,100,100)
>>> b[0,1].shape    # row zero column 1? 
(100, 100)
>>> np.all(b[0,1] == a[0:100, 100:200])
True
>>> 
>>> c = np.swapaxes(b, 1,2)
>>> c.shape
(8, 100, 8, 100)
>>> np.all(c[0,:,1,:] == a[0:100, 100:200])    # row zero column 1? 
True
>>> d = c.reshape(800,800)
>>> np.all(d==img_array)
True
>>>

【讨论】:

    【解决方案2】:

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

    您不需要缓慢的循环,您可以通过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)
    

    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,:,...])
    

    现在让我们对图块进行一些操作。

    清除一些随机图块

    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,:,...])
    

    现在要重建,你可以重新塑造成原来的形状

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

    【讨论】:

      猜你喜欢
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多