从label_image绘制轮廓

时间:2020-10-15 18:05:47

标签: python opencv image-processing scikit-image

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

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成千上万时,这可能是一个真正的瓶颈。请问有更有效的方法吗?也许有一个我不知道的功能...我希望能够将标签分配给相应的轮廓。

上面的代码打印:

   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],...

2 个答案:

答案 0 :(得分:2)

我尝试进行以下多处理尝试,并使用6个CPU的速度提高了2.5倍:

#!/usr/bin/env python3

from multiprocessing import Pool, cpu_count, freeze_support
from functools import partial
import pandas as pd
import numpy as np
import cv2
import os

def worker(labels, label_image):
    """One worker started per CPU. Receives the label image once and a list of the labels to look for."""
    pid = os.getpid()
    print(f'Worker pid: {pid}, processing {len(labels)} labels')
    d = {}
    for label in labels:
        y = label_image == label
        y = y * 255
        y = y.astype('uint8')
        contours, _ = cv2.findContours(y, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        contours = np.squeeze(contours)
        d[label] = contours.tolist()
    return d

if __name__ == '__main__':

    freeze_support()

    # Synthesize label image: 2500 labels arranged as 50x50 image, then scaled up to some realistic size
    label_image = np.arange(2500, dtype=np.uint16).reshape((50,50))
    label_image = cv2.resize(label_image, (4000,4000), interpolation=cv2.INTER_NEAREST)

    # Get number of cores and split labels across that many workers
    processes = cpu_count()

    # UNCOMMENT FOLLOWING LINE FOR SINGLE CPU
    # processes = 1
    print(f'Using {processes} processes')

    labels = np.unique(label_image[label_image > 0])
    print(f'Unique labels detected: {labels}')

    # Chunk up the labels across the processes
    chunks = np.array_split(labels, processes)

    # Map the labels across the processes
    with Pool(processes=processes) as pool:
        result = pool.map(partial(worker, label_image=label_image), chunks)

    #print(result)

具有12个内核的采样输出

Using 12 processes
Unique labels detected: [   1    2    3 ... 2497 2498 2499]
Worker pid: 76884, processing 209 labels
Worker pid: 76886, processing 209 labels
Worker pid: 76885, processing 209 labels
Worker pid: 76888, processing 208 labels
Worker pid: 76887, processing 208 labels
Worker pid: 76889, processing 208 labels
Worker pid: 76890, processing 208 labels
Worker pid: 76893, processing 208 labels
Worker pid: 76891, processing 208 labels
Worker pid: 76892, processing 208 labels
Worker pid: 76895, processing 208 labels
Worker pid: 76894, processing 208 labels

关键字:Python,图像处理,多处理,并行,块。

答案 1 :(得分:2)

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

要注意的一件事:dip.GetImageChainCodes返回的链代码符合您的期望:它们跟踪每个对象的外部像素。但是,converting these to a polygon的作用有所不同:多边形不链接外部像素,而是在像素之间“开裂”后围绕它们 。这样做可以减少像素点。这将导致多边形更好地描述实际对象,其面积恰好比对象中像素的数量少半个像素,并且其长度更接近基础对象的周长(在离散化为一组之前)像素)。这个想法来自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)