分割numpy数组的不同部分

问题描述 投票:0回答:1

我正在导入图像并将其转换为numpy数组。我想分析它们,但是对于我程序的下一步来说,它们太大了。输入图像为640 x 480,我正在分析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=0rightbound=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_width*number_width)-frmwidth)/(number_width-1)计算每种作物与前一个作物重叠的量。

python arrays numpy image-processing numpy-ndarray
1个回答
0
投票

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

sub_img1 = frame[0:400,0:400]
sub_img2 = frame[0:400,80:480]
sub_img3 = frame[240:640,0:400]
sub_img4 = frame[240:640:80:480]

有时简单的硬编码比一般情况更容易。

© www.soinside.com 2019 - 2024. All rights reserved.