【问题标题】:Shifting an array in a special location using numpy.roll使用 numpy.roll 将数组移动到特殊位置
【发布时间】:2019-11-22 11:57:21
【问题描述】:

我有一个 256x256 数组,我想使用特殊的行和列向右移动。我正在使用 numpy.roll 来实现它。如果我使用以下代码,则 shift 是向右一列。

import cv2
import matplotlib.pyplot as plt
import numpy as np

# Input (256x256 0 values array)
array = np.zeros((256,256))

# Draw a square inside the array (1 values)
array[100:156,100:156] = 1

# Shift the square to the right (1 as shift value)
shifted_array = np.roll(array,1)

如果我无限重复该过程,它会在数组的整个宽度(256 列)上移动,然后重新启动。我想要做的是将数组从第 120 列移到第 130 列。这意味着最大移位是 10 列(而不是像以前那样的 256 列)。我怎样才能做到这一点?

[已解决] 最后,我找到了一种使用以下代码实现此目的的方法:

import cv2
import matplotlib.pyplot as plt
import numpy as np

# Input (256x256 0 values array)
array = np.zeros((256,256))

# Draw a square inside the array (1 values)
array[100:156,100:156] = 1

# Cropping the image between columns 120 and 130
cropped_image = array[:,120:130]

# Loop to shift the image every column and get the original dimensions  
i = 1
shift_value = 1

while i < len(cropped_image):
    shifted_array = np.roll(cropped_image, shift_value)
    shift_value = shift_value + 1
    i = i + 1
    new_array = np.zeros(array.shape)
    new_array[:,120:130] = shifted_array

【问题讨论】:

  • np.roll(array, 10) 你在找什么?
  • 请提供minimal reproducible example 一些示例输入和示例输出。
  • 你好奥斯巴克。感谢您的回复。不是真的因为我想保留 1 作为移位值并在数组的特殊列中进行移位。但无论如何,我想我找到了一种方法来实现这一点:(i)隔离新数组中的列,(ii)然后用新数组裁剪旧数组,(iii)移位,以及(iv)通过用 0 值填充新的移位数组。
  • @rdmato33 如果您自己解决了这个问题,请将其作为答案提供并接受,这样问题就解决了。它将帮助可能遇到相同问题的其他人。

标签: python arrays numpy shift


【解决方案1】:

上面的while 循环没有做任何事情。 array 永远不会改变它的状态,所以只有最后的迭代被保存在 shift_array 中。下面使用一个较小的数组来查看结果。

import numpy as np

arr = np.zeros((12,12), dtype=np.int)
arr[ 4:8, 6:10]  = 1
arr
# array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
#        [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
#        [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
#        [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

crop = np.s_[:, 5:11]  # crop is the selection to roll.

new_array = np.zeros_like(arr)
new_array[crop] = np.roll(arr[crop], len(arr)-1)

new_array
# array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
#        [0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
#        [0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
#        [0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

这具有将正方形向下移动两行并离开一列的效果。

根据您要实现的目标,最好使用np.roll(arr, 1, axis=1) 的轴参数来明确地向左/向右滚动,或使用axis=0 来向上/向下滚动。

【讨论】:

    【解决方案2】:

    谢谢克里斯。

    确实,这不是真正的预期结果,但我只需要移动正方形。 我的目标是比较两个图像(imageA 与中心正方形,imageB 与移动正方形)并移动 imageB 的正方形以匹配 imageA 的正方形。

    为了检测匹配位置,我使用了快速傅里叶变换 (FFT),如下所示。然后,我计算两个结果图像之间的差异。如果差值等于0,则找到匹配位置。所以我成功地使用下面的代码找到了它。

    import cv2
    import matplotlib.pyplot as plt
    import numpy as np
    
    img1 = np.zeros((256,256))
    img1[100:156,100:156] = 1
    
    img2 = np.zeros(img1.shape)
    img2[50:255, 50:255] = img1[:205,:205]
    
    f = np.fft.fft2(img1)
    fshift1 = np.fft.fftshift(f)
    
    f = np.fft.fft2(img2)
    fshift2 = np.fft.fftshift(f)
    
    cropped_image = img2[90:220, 90:220]
    
    i = 0
    shift_value = 1
    min_diff = 1000000000
    
    while i < np.size(cropped_image):
        img3 = np.roll(cropped_image, shift_value)
        shift_value = shift_value + 1
        img4 = np.zeros(img1.shape)
        img4[90:220, 90:220] = img3
        f = np.fft.fft2(img4)
        fshift3 = np.fft.fftshift(f)
        real_value = np.sum(np.abs(np.real(fshift1) - np.real(fshift3)))
        if min_diff > real_value:
            min_diff = real_value
            print(i)
        i = i + 1
    
    print(min_diff)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      • 2016-08-06
      • 1970-01-01
      • 2011-07-15
      • 2011-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多