如何在python中获取给定像素标签的对象边界框?

您所在的位置:网站首页 在python中append的用法 如何在python中获取给定像素标签的对象边界框?

如何在python中获取给定像素标签的对象边界框?

#如何在python中获取给定像素标签的对象边界框?| 来源: 网络整理| 查看: 265

假设我有一个图像的场景解析图,该场景解析图中的每个像素都指示该像素属于哪个对象。现在我想获取每个对象的边界框,如何在python中实现呢?对于一个详细的例子,说我有一个像这样的场景解析图:

0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

因此边界框为:

0 0 0 0 0 0 0 1 1 1 1 1 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

实际上,在我的任务中,只要知道该对象的宽度和高度就足够了。

一个基本思路是从上,下,左和右方向搜索场景解析图中的四个边缘。但是图像中可能有很多小物体,这种方式效率不高。

第二种方法是计算所有非零元素的坐标并找到最大/最小x / y。然后使用这些x和y计算体重和身高。

还有其他更有效的方法吗?谢谢。

1> Marcos..:

如果要处理图像,则可以使用scipy的ndimage库。

如果图像中只有一个对象,则可以使用scipy.ndimage.measurements.find_objects(http://docs.scipy.org/doc/scipy-0.16.1/reference/generation/scipy.ndimage进行测量。 measurement.find_objects.html):

import numpy as np from scipy import ndimage a = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) # Find the location of all objects objs = ndimage.find_objects(a) # Get the height and width height = int(objs[0][0].stop - objs[0][0].start) width = int(objs[0][1].stop - objs[0][1].start)

如果图像中有很多对象,则必须首先标记每个对象,然后获取测量值:

import numpy as np from scipy import ndimage a = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0]]) # Second object here # Label objects labeled_image, num_features = ndimage.label(a) # Find the location of all objects objs = ndimage.find_objects(labeled_image) # Get the height and width measurements = [] for ob in objs: measurements.append((int(ob[0].stop - ob[0].start), int(ob[1].stop - ob[1].start)))

如果检查ndimage.measurements,则可以获得更多测量结果:质心,面积...



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3