如何从列表中查找和删除图像的重叠切片?
我使用unutbu和Joe Kington在这个问题上提供的方法将图像划分为对象(切片):使用python 在单色图像中的blob周围的矩形边界框,并列出这些对象,其格式如下:
I have divided an image into objects (slices) using the method kindly contributed by unutbu and Joe Kington at this question: Rectangular bounding box around blobs in a monochrome image using python and have a list of these objects which takes the following form:
the_blobs = [(slice(dy.start, dy.stop, None), slice(dx.start, dx.stop, None))]
dy.start给出起始的y像素值,dy.stop给出最终的y像素值,以及dx的相同交易。
dy.start gives the starting y-pixel value and dy.stop gives the final y-pixel value, and the same deal for dx.
在列表中有一些重叠的对象,即一个小对象(正方形)位于较大的对象(如圆圈)内。发生这种情况时,我想从列表中删除封闭对象(因为圆圈已包含它),例如
Within the list there are some objects which overlap i.e. one tiny object (a square) is within a larger object such as a circle. When this occurs I want to remove the "enclosed" object from the list (because the circle already has it included) e.g.
当前列表
the_blobs = [(slice(100L, 1000L, None), slice(100L, 1000L, None)),
(slice(150L, 220L, None), slice(150L, 220L, None)),
(slice(1001L, 2000L, None), slice(1500L, 1700L, None)),
(slice(2001L, 2200L, None), slice(1800L, 1890L, None))]
理想列表(已删除对象)
the_blobs = [(slice(100L, 1000L, None), slice(100L, 1000L, None)),
(slice(1001L, 2000L, None), slice(1500L, 1700L, None)),
(slice(2001L, 2200L, None), slice(1800L, 1890L, None))]
我应该注意,作为上述问题的一部分,建议使用以下代码:
I should note that as part of the aforementioned question a suggestion was made to use the following code:
data_slices = ndimage.find_objects(coded_paws)
for s in data_slices:
filled[s] = True
coded_paws, num_paws = ndimage.label(filled)
data_slices = ndimage.find_objects(coded_paws)
然而,这似乎不是100%的时间都有效,而且它确实是一个略微超出的贡献原问题的范围,所以我将这一部分作为一个单独的具体问题重新开放。
However this doesn't seem to work 100% of the time, and it really was a contribution slightly beyond the scope of the original question so I'm re-opening this part as a separate, specific question.
关于如何实现这一点的任何想法?
Any ideas on how I can achieve this?
编辑:这是一个实际的图片示例,不能与上面的代码一起使用
Here's an actual image example which doesn't work with the above code
处理此退货
和
理想情况下,我想从切片列表中删除最后一张图片
Ideally I'd like to remove the last image from the list of slices
显然您可以采用O(n ^ 2)方法检查每个blob与所有其他blob,并通过检查是否 blob1.dx.start>确定是否应该删除它。 blob2.dx.start和blob1.dy.start> blob2.dy.start和blob1.dx.stop< blob2.dx.stop和blob1.dy.stop< blob2.dy.stop
(如果此条件为真,则可以从列表中删除blob1)。如果您的总blob数量非常低,除非我遗漏了某些内容,否则这应该有效。
Obviously you can take an O(n^2) approach that checks each blob against all other blobs and determines if it should be removed by checking if blob1.dx.start > blob2.dx.start and blob1.dy.start > blob2.dy.start and blob1.dx.stop < blob2.dx.stop and blob1.dy.stop < blob2.dy.stop
(if this condition is true, it is ok to remove blob1 from the list). If your total blob count is pretty low, this should work unless I am missing something.
如果您正在寻找优化的解决方案,那么了解如何有很多斑点,条件有多常见。
If you are looking for an optimized solution, it would be helpful to know about how many blobs there are and how common the condition is.