一只萌萌小番薯
你会想要这样的东西:mask = x == 0 # or `x != 255` where x is your arraycolumns_indices = np.where(np.any(mask, axis=0))[0]rows_indices = np.where(np.any(mask, axis=1))[0]first_column_index, last_column_index = columns_indices[0], columns_indices[-1]first_row_index, last_row_index = rows_indices[0], rows_indices[-1]解释:让我们使用1创建一个示例数组np.padimport numpy as npx = np.pad(array=np.zeros((3, 4)), pad_width=((1, 2), (3, 4)), mode='constant', constant_values=255)print(x)[[255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.] [255. 255. 255. 0. 0. 0. 0. 255. 255. 255. 255.] [255. 255. 255. 0. 0. 0. 0. 255. 255. 255. 255.] [255. 255. 255. 0. 0. 0. 0. 255. 255. 255. 255.] [255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.] [255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.]]从这里我们可以得到一个简单的零元素的布尔掩码数组:mask = x == 0print(mask)[[False False False False False False False False False False False] [False False False True True True True False False False False] [False False False True True True True False False False False] [False False False True True True True False False False False] [False False False False False False False False False False False] [False False False False False False False False False False False]]现在我们可以np.any用来获取那些至少有一个零元素的行。对于列:print(np.any(mask, axis=0))>>> [False False False True True True True False False False False]对于行:print(np.any(mask, axis=1))>>> [False True True True False False]现在我们只需要将布尔数组转换为索引数组2:columns_indices = np.where(np.any(mask, axis=0))[0]print(columns_indices)>>> [3 4 5 6]rows_indices = np.where(np.any(mask, axis=1))[0]print(rows_indices)>>> [1 2 3]从这里获取第一行和最后一行/列索引非常简单:first_column_index, last_column_index = columns_indices[0], columns_indices[-1]first_row_index, last_row_index = rows_indices[0], rows_indices[-1]计时:我使用以下代码来计算计时并绘制它们:绘制一系列输入的计时。将我的版本与您的版本进行比较,但重构如下:indices = np.where(x == 0)first_row_index, last_row_index = indices[0][0], indices[0][-1]first_column_index, last_column_index = indices[1][0], indices[1][-1]plot_times([georgy_solution, yodish_solution], np.arange(10, 200, 5), repeats=500)plot_times([georgy_solution, yodish_solution], np.arange(200, 10000, 800), repeats=1)
一只甜甜圈
这是一个应该与 python 2 或 python 3 一起使用的脚本。如果您使用 IPython 或 jupyter,“魔术”%pylab会为您完成所有导入,并且您不需要所有from...语句。在这些声明之后,我们会创建一张类似于您发布的图片。from __future__ import print_function # makes script work with python 2 and python 3from matplotlib.pyplot import show, imshow, imreadfrom matplotlib.mlab import findfrom numpy import zeros, int8, sumimg1 = zeros((256, 256), dtype=int8)img1[50:200, 100:150] = 100imshow(img1) show() # You don't need this call if you are using ipython or jupyter# You now see a figure like the first one you postedprint('Axis 0 blob limits', find(sum(img1, axis=0) != 0)[[0, -1]]) print('Axis 1 blob limits', find(sum(img1, axis=1) != 0)[[0, -1]])使用sum具有明确规范的函数axis使其返回沿给定方向的总和。该find函数返回条件为真的所有索引的数组,在这种情况下“列或行总和为零?” 最后,切片[0, -1]选择找到的第一列和最后一列。如果您的图像没有任何行或任何列全为零,则find返回一个空数组,并且索引尝试[0, -1] 引发IndexError. 如果你try...except在它周围包裹一个块,你可以美化错误条件。