【问题标题】:How to reshape this image array in python?如何在python中重塑这个图像数组?
【发布时间】:2016-10-27 03:58:39
【问题描述】:

我有一个像这样的 8X8 图像数组:

a = np.array([[1,1,1,1,2,2,2,2],
              [1,1,1,1,2,2,2,2],
              [1,1,1,1,2,2,2,2],
              [1,1,1,1,2,2,2,2],
              [3,3,3,3,4,4,4,4],
              [3,3,3,3,4,4,4,4],
              [3,3,3,3,4,4,4,4],
              [3,3,3,3,4,4,4,4]])

我想将它重新整形为一个数组,每个部分彼此分开,如下所示:

a = np.array([
              [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]],
              [[2,2,2,2],[2,2,2,2],[2,2,2,2],[2,2,2,2]],
              [[3,3,3,3],[3,3,3,3],[3,3,3,3],[3,3,3,3]],
              [[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]]
             ])

这是一个 4X4X4 数组,我可以单独绘制图像的一部分。我该怎么做?

【问题讨论】:

    标签: python python-3.x numpy


    【解决方案1】:

    这样就可以了:

    >>> b = np.split(np.hstack(np.split(a, 2)), 4, axis=1)
    >>> np.array(b)
    array([[[1, 1, 1, 1],
            [1, 1, 1, 1],
            [1, 1, 1, 1],
            [1, 1, 1, 1]],
    
           [[2, 2, 2, 2],
            [2, 2, 2, 2],
            [2, 2, 2, 2],
            [2, 2, 2, 2]],
    
           [[3, 3, 3, 3],
            [3, 3, 3, 3],
            [3, 3, 3, 3],
            [3, 3, 3, 3]],
    
           [[4, 4, 4, 4],
            [4, 4, 4, 4],
            [4, 4, 4, 4],
            [4, 4, 4, 4]]])
    

    【讨论】:

      【解决方案2】:

      重新排列数组的步幅:

      import numpy as np
      from numpy.lib.stride_tricks import as_strided
      
      def windows(a, window = (2,2), ss = None, flatten = True):
          '''
          Return a sliding window over a.
      
          a - numpy ndarray
          window - shape of the window, int for 1d or tuple for 2d+
          ss - int for 1d or tuple for 2d+ how much to slide the window
               defaults to window (no overlap)
          flatten - if True, all slices are flattened, otherwise, there is an 
                        extra dimension for each dimension of the input.
      
          Returns
              an array containing each n-dimensional window from a
          '''
          if ss is None:
              ss = window
          data_shape = np.array(a.shape)
      
          # how many windows are there?
          windowed_array_shape = tuple(((data_shape - window) // window) + 1)
          nbr_windows = np.product(windowed_array_shape)
      
          # the shape of the windowed array
          newshape = windowed_array_shape + window
      
          # calculate the strides for the windowed array
          newstrides =  tuple(np.array(a.strides) * window) + a.strides
      
          # use as_strided to 'transform' the array
          windowed_array = as_strided(a, shape = newshape, strides = newstrides)
      
          if not flatten:
              return windowed_array
      
          # flatten the windowed array for iteration
          dim = (nbr_windows,) + window
          windowed_array = windowed_array.reshape(dim)
          return windowed_array
      
      a = np.array([[1,1,1,1,2,2,2,2],
                    [1,1,1,1,2,2,2,2],
                    [1,1,1,1,2,2,2,2],
                    [1,1,1,1,2,2,2,2],
                    [3,3,3,3,4,4,4,4],
                    [3,3,3,3,4,4,4,4],
                    [3,3,3,3,4,4,4,4],
                    [3,3,3,3,4,4,4,4]])
      
      >>> b = windows(a, (4,4))
      >>> b
      array([[[1, 1, 1, 1],
              [1, 1, 1, 1],
              [1, 1, 1, 1],
              [1, 1, 1, 1]],
      
             [[2, 2, 2, 2],
              [2, 2, 2, 2],
              [2, 2, 2, 2],
              [2, 2, 2, 2]],
      
             [[3, 3, 3, 3],
              [3, 3, 3, 3],
              [3, 3, 3, 3],
              [3, 3, 3, 3]],
      
             [[4, 4, 4, 4],
              [4, 4, 4, 4],
              [4, 4, 4, 4],
              [4, 4, 4, 4]]])
      >>>
      

      this SO q&a 中的其他几个选项

      【讨论】:

        【解决方案3】:

        这是一种使用reshapeswapaxes 的方法-

        B = 4 # Blocksize
        m,n = a.shape
        out = a.reshape(m//B,B,n//B,B).swapaxes(1,2).reshape(-1,B,B)
        

        【讨论】:

        • 我得到 [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4] ] 有没有我错过的交换?不应该是 swapaxes(1,2) 吗?
        • @NaN 是的,我第一次对它的解释不同。修复。感谢您指出!
        【解决方案4】:

        你也可以试试这个:

        np.column_stack((a[:4,:4].ravel(),a[:4,4:8].ravel(),a[4:8,:4].ravel(),a[4:8,4:8].ravel())).T.reshape((4,4,4))
        

        或者这个:

        np.concatenate(a.reshape(2,4,8).T).T.reshape((4,4,4))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-04-18
          • 2019-05-19
          • 2016-05-20
          • 2020-04-04
          • 1970-01-01
          • 2019-05-05
          • 2020-10-05
          • 2017-05-29
          相关资源
          最近更新 更多