python - 切片numpy数组的不同部分
问题描述
我正在导入图像并将它们转换为 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)
计算每个作物与前一个作物重叠的程度。
解决方案
这不能回答你的问题吗?(如果尺寸和切口始终相同):
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]
有时简单的硬编码比概括情况更容易。
编辑:对于一般情况,此代码为您提供了一组图像窗口:
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)))
推荐阅读
- node.js - 如何从 mongoDB 文档数组中删除一个项目?
- javascript - 如何使用 Marked 将我的文本转换为降价 - 转换不起作用
- typescript - 如何在 Typescript 中将泛型类型键入为接口的子集?
- maven - 如何从 Maven 获取 OpenJDK 11?
- date - 我如何在 xquery 1.0 中获取日期间隔
- kotlin - 多个 Apache Camel REST 服务的 Apache Camel 响应类型不匹配
- r - 如何按 R 中两列中的名称聚合?
- rascal - 是否可以使用 vis 将用户重定向到 java 文件中的代码块?
- eclipse - Eclipse 中新类的更改未显示在 git 存储库中
- android - 单个 RecyclerView 中的多个适配器