从label_image绘制轮廓 [英] Drawing contours from label_image

查看:49
本文介绍了从label_image绘制轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 label_image 数组,并且正在派生该数组上对象的轮廓/边界.目前,我正在通过获取所有唯一标签,遍历它们然后找到每个对象的轮廓来进行此操作.就像在下面的循环中一样,我在其中用标签标签填充 dict 并确定轮廓的值

I have a label_image array and I am deriving the outlines/boundaries of the objects on that array. Currently I am doing that by getting all unique labels, iterating over them and then find the contours of each object. Like in the loop below, where I am populating a dict with keys the label and values the contours

import cv2
import pandas as pd
import numpy as np


def extract_borders(label_image):
    labels = np.unique(label_image[label_image > 0])
    d = {}
    for label in labels:
        y = label_image == label
        y = y * 255
        y = y.astype('uint8')
        contours, hierarchy = cv2.findContours(y, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        contours = np.squeeze(contours)
        d[label] = contours.tolist()
    df = pd.DataFrame([d]).T
    df = df.reset_index()
    df.columns = ['label', 'coords']
    return df


if __name__ == "__main__":
    label_img = np.array([
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 0, 0, 0],
        [0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 0, 0, 0],
        [0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 0, 0, 0],
        [0, 0, 0, 2, 2, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 4, 4, 0, 0, 0],
        [0, 0, 0, 2, 2, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 4, 4, 0, 0, 0],
        [0, 0, 0, 2, 2, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 4, 4, 0, 0, 0],
        [0, 0, 0, 2, 2, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 4, 4, 0, 0, 0],
        [0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 0, 0, 0],
        [0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 0, 0, 0],
        [0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    ])

    res = extract_borders(label_img)
    print(res)

labels 数以千计时,这可能是一个真正的瓶颈.请问有更有效的方法吗?也许有一个我不知道的功能...我希望能够将标签分配给相应的轮廓.

When labels are thousands this can be a real bottleneck. Is there a more efficient way to do this please? Maybe there is a function I am not aware of... I want to be able to assign the label to the corresponding contours.

上面的代码打印:

   label                                             coords
0      1                   [[5, 6], [5, 9], [9, 9], [9, 6]]
1      2  [[3, 3], [3, 12], [11, 12], [11, 10], [5, 10],...
2      3  [[12, 5], [11, 6], [10, 6], [10, 9], [11, 9], ...
3      4  [[12, 3], [12, 4], [14, 4], [15, 5], [15, 10],...

推荐答案

DIPlib库具有提取图像中每个对象的链码的功能.但是,这确实要求每个对象都已连接(具有相同标签的像素必须构成一个已连接的组件).使用Mark的大型示例图像,这将使计算时间从154.8s缩短到0.781s,速度提高了200倍.我认为大部分时间都致力于将链代码转换为多边形,numpy数组,列表以及最终的pandas表.很多转换...

The DIPlib library has a function to extract the chain code for each object in the image. It does require, however, that each object is connected (the pixels with the same label must form a connected component). Using Mark's large example image, this takes computation time from 154.8s to 0.781s, 200 times faster. And most of that time, I think, is dedicated to converting the chain code into a polygon, into a numpy array, into a list, and finally into a pandas table. Lots of conversions...

需要注意的一件事: dip.GetImageChainCodes 返回的链代码符合您的期望:它们跟踪每个对象的外部像素.但是,将其转换为多边形会有所不同:多边形不会不要链接外部像素,而是在裂纹"之后跟随它们 .像素之间.这样做可以减少像素点.这将导致多边形更好地描述实际对象,其面积恰好比对象中像素的数量少半个像素,并且其长度更接近基础对象的周长(在将其离散化为一组之前)像素).这个想法来自史蒂夫·埃丁斯(Steve Eddins),MathWorks .

One thing to note: the chain codes returned by dip.GetImageChainCodes are as you'd expect: they trace the outer pixels of each object. However, converting these to a polygon does something different: the polygon doesn't link the outer pixels, but goes around them, following the "crack" between the pixels. And it cuts pixel corners doing so. This leads to a polygon that much better describes the actual object, its area is exactly half a pixel smaller than the number of pixels in the object, and its length is much closer to the perimeter of the underlying object (before discretizing it into a set of pixels). This idea comes from Steve Eddins at the MathWorks.

import pandas as pd
import numpy as np
import diplib as dip
import cv2
import time

def extract_borders(label_image):
    labels = np.unique(label_image[label_image > 0])
    d = {}
    for label in labels:
        y = label_image == label
        y = y * 255
        y = y.astype('uint8')
        contours, hierarchy = cv2.findContours(y, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        contours = np.squeeze(contours)
        d[label] = contours.tolist()
    df = pd.DataFrame([d]).T
    df = df.reset_index()
    df.columns = ['label', 'coords']
    return df

def extract_borders_dip(label_image):
    cc = dip.GetImageChainCodes(label_img) # input must be an unsigned integer type
    d = {}
    for c in cc:
        d[c.objectID] = np.array(c.Polygon()).tolist()
    df = pd.DataFrame([d]).T
    df = df.reset_index()
    df.columns = ['label', 'coords']
    return df

if __name__ == "__main__":
    label_img = np.arange(2500, dtype=np.uint16).reshape((50,50))
    label_img = cv2.resize(label_img, (4000,4000), interpolation=cv2.INTER_NEAREST)
    start = time.process_time()
    res = extract_borders(label_img)
    print('OP code:', time.process_time() - start)
    print(res)
    start = time.process_time()
    res = extract_borders_dip(label_img)
    print('DIPlib code: ', time.process_time() - start)
    print(res)

这篇关于从label_image绘制轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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