【发布时间】:2020-09-24 00:45:42
【问题描述】:
我正在导入图像并将它们转换为 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=0 和 rightbound=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) 计算每个作物与前一个作物的重叠程度。
【问题讨论】:
-
640x480 太大了怎么办?弄清楚 那个 可能更容易和更健壮。此外,如果您有 400x400 的 640x480 部分,为什么还需要图像的其余部分?你能指定 4 个子部分吗?目前尚不清楚您要做什么。
-
它总是固定大小和窗口吗?如果是这样,为什么不使用硬编码 4 子图像?
-
您的代码的实际问题是什么?您期望的确切输入和输出是什么?
-
@asylumax 我将它们输入到输入大小为 400x400 的卷积神经网络中。
-
你说你想概括,但我不知道你提出的一般解决方案是什么,或者为什么它不起作用。
标签: python arrays numpy image-processing numpy-ndarray