快速查找大图像中连接组件的最小和最大坐标 [英] Quickly find the min and max coordinates of connected component in a large image

查看:99
本文介绍了快速查找大图像中连接组件的最小和最大坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图片大小 50000x50000 。它有大约 25000 连接不同的连接组件。我正在使用 ndimage.label 来标记每个,然后我找到非零点,最后得到min x,max x,min y和max y值。但是,我必须找到每个 25000 连接组件的坐标。这是昂贵的,因为我必须在 50000x50000 图像 25000 25000 25000 c> c000 code>次。这是我刚才提到的代码片段。

I have an image that is of size 50000x50000. It has around 25000 connected different connected components. I'm using ndimage.label to label each of them and then I find the non zero points and finally get the min x, max x, min y and max y values. However, I have to find these coordinates is for each of the 25000 connected components. This is expensive as I have to run np.nonzero on the 50000x50000 image 25000 times. Here is a snippet of the code doing what I just mentioned.

im, _ = ndimage.label(im)
num_instances = np.max(np.max(im))
for instance_id in range(1,num_instances+1):
    im_inst = im == instance_id 
    points = np.nonzero(im_inst) # running this is expensive as im is 50000x50000

    cropped_min_x_1 = np.min(points[0])
    cropped_min_y_1 = np.min(points[1]) 
    cropped_max_x_1 = np.max(points[0])+1 
    cropped_max_y_1 = np.max(points[1])+1

有谁知道我能做些什么来大大加快这个过程?

Does anyone know what I can do to significantly speed up this process?

推荐答案

如果标记像素的分数不是太大:

If the fraction of labelled pixels is not too large:

nz = np.flatnonzero(im)
order = np.argsort(im.ravel()[nz])
nz = nz[order]
blocks = np.searchsorted(im.ravel()[nz], np.arange(2, num_instances+1))
# or (which is faster will depend on numbers)
blocks = 1 + np.where(np.diff(im.ravel()[nz]))[0]
coords = np.array(np.unravel_index(nz, (50000, 50000)))
groups = np.split(coords, blocks, axis=-1)

groups将是2xn_i坐标的列表其中n_i是组件i的大小。

groups will be a list of 2xn_i coordinates where n_i is the size of component i.

这篇关于快速查找大图像中连接组件的最小和最大坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆