Python 切片numpy阵列的不同部分

Python 切片numpy阵列的不同部分,python,arrays,numpy,image-processing,numpy-ndarray,Python,Arrays,Numpy,Image Processing,Numpy Ndarray,我正在导入图像并将其转换为numpy阵列。我想对它们进行分析,但它们太大,无法在我的程序中进行下一步。输入图像是640x480,我正在分析400x400。我想裁剪出这些图像的部分,而不是调整大小,这样我就不会丢失任何细节。我想在整个图像上创建分析大小为400x400的“遮罩”。 例如,此图像中应该有4个遮罩 0:400,0:400 0:400,80:480 240:640,0:400 240:640:80:480 我目前的代码是: frame是图像的一个numpy数组 frmwidth = f

我正在导入图像并将其转换为numpy阵列。我想对它们进行分析,但它们太大,无法在我的程序中进行下一步。输入图像是640x480,我正在分析400x400。我想裁剪出这些图像的部分,而不是调整大小,这样我就不会丢失任何细节。我想在整个图像上创建分析大小为400x400的“遮罩”。 例如,此图像中应该有4个遮罩

0:400,0:400
0:400,80:480
240:640,0:400
240:640:80:480
我目前的代码是:
frame
是图像的一个numpy数组

frmwidth = frame.shape[0]
frmheight = frame.shape[1]
# print(frmwidth)
# print(frmheight)
res_width, res_height = 400,400

number_width = math.ceil(frmwidth/res_width)
number_height = math.ceil(frmheight/res_height)
iter = max(number_width, number_height)
print(number_width)
print(number_height)


print('\n\nBOUNDS')
topbound = 0
for i in range(number_width):
    leftbound = 0

    for j in range(number_height):
        if leftbound == 0 and topbound == 0:
            rightbound = leftbound + 400
            print('width')
            print(leftbound)
            print(rightbound)
            leftbound = rightbound

            bottombound = topbound+400
            print('heigth')
            print(topbound)
            print(bottombound)
            topbound = bottombound

        elif leftbound == 0 and topbound != 0:
            rightbound = leftbound + 400
            print('width')
            print(leftbound)
            print(rightbound)
            leftbound = rightbound

            topbound = topbound - ((res_height*number_height)-frmheight)/(number_height-1)
            bottombound = topbound+400
            print('height')
            print(topbound)
            print(bottombound)

        elif topbound == 0 and leftbound != 0:
            leftbound = leftbound - ((res_width*number_width)-frmwidth)/(number_width-1)
            rightbound = leftbound+400
            print('width')
            print(leftbound)
            print(rightbound)
            bottombound = topbound+400

            print('heigth')
            print(topbound)
            print(bottombound)
            topbound = bottombound
        else:
            leftbound = leftbound - ((res_width*number_width)-frmwidth)/(number_width-1)
            rightbound = leftbound+400
            print('width')
            print(leftbound)
            print(rightbound)

            topbound = topbound - ((res_height*number_height)-frmheight)/(number_height-1)
            bottombound = topbound+400
            print('height')
            print(topbound)
            print(bottombound)
我已将
leftbound=0
righbound=0
移入和移出for循环。这是我用3/4“面具”得到的最接近的正确答案

BOUNDS
width
0
400
heigth
0
400
width
80.0
480.0
height
240.0
640.0
width
0
400
height
80.0
480.0
width
80.0
480.0
height
-80.0
320.0
我为所有的打印声明道歉,它使一切井然有序


这部分
(res\u width*number\u width)-frmwidth)/(number\u width-1)
计算每种作物与前一种作物重叠的程度。

这难道不能回答您的问题吗?(如果尺寸和切口始终相同):

有时,简单的硬编码比概括案例更容易

编辑:对于一般情况,此代码为您提供一组图像窗口:

from skimage.util.shape import view_as_windows
window_shape = (400,400)
step = 400
B1 = view_as_windows(frame, window_shape, step)
B2 = view_as_windows(frame[-window_shape[0]:,:], window_shape, step)
B3 = view_as_windows(frame[:,-window_shape[1]:], window_shape, step)
B4 = view_as_windows(frame[-window_shape[0]:,-window_shape[1]:], window_shape, step)
arr_sub_images = np.vstack((B1.reshape(-1,*window_shape),B2.reshape(-1,*window_shape),B3.reshape(-1,*window_shape),B4.reshape(-1,*window_shape)))

我用
np.linspace
找到了一个解决方案
linspace
查找每个裁剪的中心点,然后我对每个x轴和y轴迭代
linspace
,并将它们作为组合使用以获得位置。然后我们加上或减去200(在本例中为400x400作物)得到作物

frmwidth = frame.shape[0]
frmheight = frame.shape[1]
print(frame.shape)
res_width, res_height = 400,400

number_width = math.ceil(frmwidth/res_width)
number_height = math.ceil(frmheight/res_height)
iter = max(number_width, number_height)

y_centers = np.linspace((res_height/2), (frmheight-(res_height/2)), math.ceil(frmheight/res_height))
x_centers = np.linspace((res_width/2), (frmwidth-(res_width/2)), math.ceil(frmwidth/res_width))
for i in y_centers:
    for j in x_centers:
        i = int(i)
        j = int(j)
        topbound = i-200
        bottombound = i+200
        leftbound = j-200
        rightbound = j+200
        frame = frame[i-200:i+200,j-200:j+200]

640x480太大了,你在干什么?弄清楚这一点可能更容易、更可靠。此外,如果您有部分640x480是400x400,为什么需要图像的其余部分?你能指定4个子部分吗?还不清楚你想做什么。它总是固定大小和窗口吗?如果是这样,为什么不使用硬编码的4子图像?你的代码的实际问题是什么?你期望什么样的精确输入和输出?@asylumax我正在将它们输入一个输入大小为400x400的卷积神经网络。你说你在尝试推广,但我不知道你提出的一般解决方案是什么,或者为什么不起作用。这是一个快速的解决方案,但我希望能够灵活地处理多个大小的图像,始终使用400x400切片,您可能有一个1280x960的图像,您希望将其分解为400x400块,并进行概括。由于400x400可能不适合,因此可以尽可能多次使用平铺图像,对于最后一个元素,可以从最大尺寸重叠。如果这是正确的,那么第一个“整数”平铺很容易,并且您可以从最大值返回。或者,是否每个图像都必须重叠?在1D;如果从0到1279,是要0-399400-799等,还是需要0-399、399重叠、700重叠等。?试图在这里找到一般情况,这是不清楚的。任何一种方法都适用于我,只要覆盖了整个图像。对于您的1D示例,我的方法将执行1-400293-693589-986879-1279。在这种情况下,它们相等地重叠。然而,如果更简单的话,做1-400400-800800-1200879-1279也可以。@TheAstrononomist请在文章中找到我对一般情况的编辑。如果这回答了您的问题,请继续并接受答案以结束问题。谢谢,出了什么问题?请注意,它会返回所需的子映像数组。该解决方案仅适用于需要4种作物的情况。有人帮我找到的解决方案,我在这里发布的,对任何数量的作物都是灵活的。添加的编辑可以对任何数量的作物进行编辑,而不使用任何循环。请随意查看。
frmwidth = frame.shape[0]
frmheight = frame.shape[1]
print(frame.shape)
res_width, res_height = 400,400

number_width = math.ceil(frmwidth/res_width)
number_height = math.ceil(frmheight/res_height)
iter = max(number_width, number_height)

y_centers = np.linspace((res_height/2), (frmheight-(res_height/2)), math.ceil(frmheight/res_height))
x_centers = np.linspace((res_width/2), (frmwidth-(res_width/2)), math.ceil(frmwidth/res_width))
for i in y_centers:
    for j in x_centers:
        i = int(i)
        j = int(j)
        topbound = i-200
        bottombound = i+200
        leftbound = j-200
        rightbound = j+200
        frame = frame[i-200:i+200,j-200:j+200]