gpt4 book ai didi

python - 如何检查 numpy 数组的维度?

转载 作者:行者123 更新时间:2023-11-28 20:15:38 27 4
gpt4 key购买 nike

我想在一个函数中做这样的事情:

if image.shape == 2 dimensions
return image # this image is grayscale
else if image.shape = 3 dimensions
return image # image is either RGB or YCbCr colorspace

这里,图像是一个 numpy 数组。我无法定义该检查条件。我真的被困在这一点上。有人可以帮忙吗?

最佳答案

numpy.array.shape是数组维度的元组。您可以计算元组的长度,这将给出维数。

if len(image.shape) == 2:
return image # this image is grayscale
elif len(image.shape) == 3:
return image # image is either RGB or YCbCr colorspace

Numpy 数组也有一个 ndim属性。

if image.ndim == 2:
return image # this image is grayscale
elif image.ndim == 3:
return image # image is either RGB or YCbCr colorspace

关于python - 如何检查 numpy 数组的维度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46101111/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com