简体   繁体   English

如何获得仅由 0 和 1 组成的 3D 矩阵的 3D 坐标

[英]How can I get 3D coordinates of a 3D matrix consisting of only 0s and 1s

I have a binarized mask, consisting of only 1s and 0s.我有一个二值化掩码,仅由 1 和 0 组成。 I want to get all of the locations in the image for the 0s so that I can compare it to the image background.我想获取图像中 0 的所有位置,以便将其与图像背景进行比较。

I am loading the masks by:我正在通过以下方式加载面具:

import PIL
from PIL import ImageOps, Image
from tensorflow.keras.preprocessing.image import load_img
from numpy import asarray

mask_path = '/Users/mshah/Downloads/8_24_Test2/Masks/3_Agar_Site_3_cp_masks.png'


loaded_mask = PIL.ImageOps.autocontrast(load_img(mask_path))
mask_array = asarray(loaded_mask)

zero_array = np.where(mask_array == 0)

This gives me a tuple with 2 indexes which I don't understand how to use.这给了我一个带有 2 个索引的元组,我不明白如何使用。 shape for mask_array is (160, 160) and dtype is uint8 . mask_arrayshape(160, 160)并且dtypeuint8

Note: the reason that I am using autocontrast instead of just Image.open is because Image.open doesn't work with binarized PNGs (at least the way I'm using).注意:我使用自动对比度而不是 Image.open 的原因是因为 Image.open 不适用于二值化 PNG(至少我使用的方式)。

Any suggestions/help?有什么建议/帮助吗?

EDIT: I found the answer, it is posted below编辑:我找到了答案,它发布在下面

I found the answer via How to retrieve, in Python, row and column numbers of the elements in a matrix that match a certain value?我通过如何在 Python 中检索矩阵中与某个值匹配的元素的行号和列号找到了答案? . .

I created an np.ones array with size (160, 160) and then subtracted the mask image from the ones array to get an array that contained 1s for where the background was.我创建了一个大小为(160, 160)np.ones数组,然后从 one 数组中减去掩码图像,得到一个包含 1 的数组,表示背景所在的位置。

shape = mask_array.shape

ones_array = np.ones(shape, dtype='uint8')

sub_array = ones_array - mask_array

Then I used list(zip(*np.where(sub_array == 1))) to create a list of where the coordinates are by zipping the x and y's together.然后我使用list(zip(*np.where(sub_array == 1)))通过将 x 和 y 压缩在一起来创建坐标所在位置的列表。

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

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