Python 使用numpy.roll在特殊位置移动数组
我有一个256x256数组,我想使用特殊的行和列向右移动。我正在使用numpy.roll来实现它。如果我使用下面的代码,shift是右边的一列Python 使用numpy.roll在特殊位置移动数组,python,arrays,numpy,shift,Python,Arrays,Numpy,Shift,我有一个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
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
导入cv2
将matplotlib.pyplot作为plt导入
将numpy作为np导入
#输入(256x256 0值数组)
数组=np.零((256256))
#在数组内绘制正方形(1个值)
数组[100:156100:156]=1
#在第120列和第130列之间剪切图像
裁剪图像=数组[:,120:130]
#循环以移动图像的每一列并获得原始尺寸
i=1
移位值=1
而i
上面的while
循环没有做任何事情<代码>数组从不更改其状态,因此只有最终迭代保存在移位数组中。下面使用较小的数组查看结果
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]])
这样做的效果是将正方形向下移动两行,向左移动一列
根据您试图实现的目标,最好使用axis参数np.roll(arr,1,axis=1)
明确向左/向右滚动,或axis=0
向上/向下滚动 谢谢克里斯
事实上,这并不是预期的结果,但我只需要改变平方。
我的目标是比较两幅图像(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)
导入cv2
将matplotlib.pyplot作为plt导入
将numpy作为np导入
img1=np.零((256256))
img1[100:156100:156]=1
img2=np.零(img1.形状)
img2[50:255,50:255]=img1[:205,:205]
f=np.fft.fft2(img1)
fshift 1=np.fft.fft移位(f)
f=np.fft.fft2(img2)
fshift 2=np.fft.fft移位(f)
裁剪的图像=img2[90:220,90:220]
i=0
移位值=1
最小差值=100000000
当i实际值:
最小差值=实际值
印刷品(一)
i=i+1
打印(最小差异)
是np.roll(数组,10)
您在寻找什么?请提供一些输入和输出示例。您好。谢谢你的回复。不是因为我想保留1作为移位值,并在数组的特殊列中进行移位。但无论如何,我想我找到了一种方法来实现这一点:(I)在新数组中隔离列,(ii)然后用新数组裁剪旧数组,(iii)移位,以及(iv)通过用0值填充新移位数组来获得原始维度。@rdmato33如果您自己解决了这个问题,请提供它作为答案并接受它,这样问题就解决了。它将帮助其他可能有同样问题的人。