如何使用 OpenCV 将图像分割为对象(“岛”)

发布于 2025-01-17 03:41:30 字数 1448 浏览 0 评论 0 原文

我有一个图像。它由黑色背景上的 3 个白色“岛屿”组成。我想把这个图像分成那些岛屿。 opencv 或 numpy 中是否有函数可以执行类似的操作?我有该功能的实现。它适用于 2d bool numpy 数组:

def get_island(img, x, y):
    island = numpy.zeros_like(img, dtype=bool)
    neibourghood = [(0, -1), (1, 0), (0, 1), (-1, 0)]
    island[x, y] = True
    img[x, y] = False
    dots = [(x, y),]
    while len(dots) > 0:
        dots2 = dots
        dots = []
        for x, y in dots2:
            for xs, ys in neibourghood:
                x2, y2 = x + xs, y + ys
                if 0 <= x2 < img.shape[0] and 0 <= y2 < img.shape[1]:
                    if img[x2, y2]:
                        img[x2, y2] = False
                        island[x2, y2] = True
                        dots.append((x2, y2),)
    return island


def get_islands(img:numpy.ndarray) -> list: # <- that function
    img = numpy.copy(img)
    islands = []
    while 1:
        xa, ya = numpy.where(img)
        if xa.shape[0] == 0: break
        x, y = xa[0], ya[0]
        islands.append(get_island(img, x, y))
    return islands

结果将为 list([img1, img2img3])

但速度很慢。我想找到一种更快的方法来做到这一点。

对不起我的英语。

I have an image. It is made of 3 white "islands" on black background. I want to split this image into thouse islands. Is there a function in opencv or in numpy that does something like that? I have my implementation of that function. It works with 2d bool numpy arrays:

def get_island(img, x, y):
    island = numpy.zeros_like(img, dtype=bool)
    neibourghood = [(0, -1), (1, 0), (0, 1), (-1, 0)]
    island[x, y] = True
    img[x, y] = False
    dots = [(x, y),]
    while len(dots) > 0:
        dots2 = dots
        dots = []
        for x, y in dots2:
            for xs, ys in neibourghood:
                x2, y2 = x + xs, y + ys
                if 0 <= x2 < img.shape[0] and 0 <= y2 < img.shape[1]:
                    if img[x2, y2]:
                        img[x2, y2] = False
                        island[x2, y2] = True
                        dots.append((x2, y2),)
    return island


def get_islands(img:numpy.ndarray) -> list: # <- that function
    img = numpy.copy(img)
    islands = []
    while 1:
        xa, ya = numpy.where(img)
        if xa.shape[0] == 0: break
        x, y = xa[0], ya[0]
        islands.append(get_island(img, x, y))
    return islands

Result will be list([img1, img2, img3])

But it is slow. I want to find a faster way to do that.

Sorry for my English.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ペ泪落弦音 2025-01-24 03:41:30

opencv中有cv2.connectedComponents函数。它满足我的需要。

def get_islands(img):
    n, labels = cv2.connectedComponents(img.astype('uint8'))
    islands = [labels == i for i in range(1, n)]
    return islands

There is cv2.connectedComponents function in opencv. It does what I need.

def get_islands(img):
    n, labels = cv2.connectedComponents(img.astype('uint8'))
    islands = [labels == i for i in range(1, n)]
    return islands
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文