繁体   English   中英

使用 numpy 和 Z94756EA97E3998E52B121DA8ZBB2DE 在 python 中裁剪 3D 图像基于 om 2D 蒙版

[英]Crop 3D image based om 2D mask in python using numpy and opencv

假设您有一个 3D 图像(numpy 数组),例如:

arr = np.random.random((4,4,3))

此外,您还有一个形状为 (4,4) 的 2D 蒙版,例如:

mask = np.array([[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]])

如何使用 numpy 和/或 Z94756EA97E3998582B121DA8BBDEZ 将形状为 (4,4) 的 2D 蒙版应用于形状为 (4,4,3) 的 3D 阵列并裁剪出不为零的图像?

这是否有助于回答您的问题?:

arr = np.random.random((4,4,3))
mask = np.array([[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]])
#[[0 1 1 0]
# [0 1 1 0]
# [0 1 1 0]
# [0 1 1 0]]
#mask array
masked_arr = mask[...,None]*arr
#crop edges
true_points = np.argwhere(masked_arr)
top_left = true_points.min(axis=0)
bottom_right = true_points.max(axis=0)
cropped_arr = masked_arr[top_left[0]:bottom_right[0]+1,top_left[1]:bottom_right[1]+1]

#cropped_arr.shape (4,2,3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM