使用 numpy 和像素位置查找图像的高度和宽度

我已将两个图像转换为 numpy 数组

image1Array

http://img4.mukewang.com/60ffb69900016ffa01260128.jpg

image2Array

http://img2.mukewang.com/60ffb6a30001f29701260123.jpg

两个图像都已转换为灰度,因此只有0255值。

在这两个示例中,顶部(和底部)有许多行白色:

[255 255 255 255 .... 255 255 255 255]

我相信我所说的“行”实际上是一个数组。我是使用 Numpy 的新手。因此,图像中的每一行都有一个数组,该行中的每个像素都用0或表示255

如何找到包含黑色0像素的第一行和包含黑色像素的最后一行0?我应该能够用它来计算高度。在这些示例中,这应该是大致相同的数字。

我相信numpy.where(image1Array == 0)[0]正在返回每个黑色像素的行;min()max()那似乎正是我要找的,但我还不确定。

相反,我如何找到每个图像的宽度?在这些示例中,Image 2宽度数应大于Image 1

编辑

我想我所需要的只是这样的:

高度(第一行黑色像素与最后一行黑色像素的差值):

(max(numpy.where(image1Array == 0)[0])) - (min(numpy.where(image1Array == 0)[0]))

宽度(黑色像素的最低列值与黑色像素的最高列值之间的差异):

(max(numpy.where(image1Array == 0)[1])) - (min(numpy.where(image1Array == 0)[1]))

到目前为止,我的测试表明这是正确的。比较上例中的两个图像,它们的高度相等,而 image2Array 的宽度是 image1Array 的两倍。



慕后森
浏览 277回答 2
2回答

一只萌萌小番薯

你会想要这样的东西: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在它周围包裹一个块,你可以美化错误条件。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python