예제 #1
0
def label_volumetric_vessel_tree(oseg,
                                 vessel_label=None,
                                 write_to_oseg=True,
                                 new_label_str_format="{}{:03d}"):
    """
    Split vessel by branches and put it in segmentation and slab.

    :param oseg: OrganSegmentation object with segmentation, voxelsize_mm and slab
    :param vessel_label: int or string label with vessel. Everything above zero is used if vessel_label is set None.
    :param write_to_oseg: Store output into oseg.segmentation if True. The slab is also updated.
    :param new_label_str_format: format of new slab
    :return:
    """
    import skelet3d
    if vessel_label is None:
        vessel_volume = oseg.segmentation > 0
    else:
        vessel_volume = oseg.select_label(vessel_label)

    # print(np.unique(vessel_volume))
    skel = skelet3d.skelet3d(vessel_volume)
    skan = skelet3d.SkeletonAnalyser(skel, volume_data=vessel_volume)
    skan.skeleton_analysis()
    bl = skan.get_branch_label()
    un = np.unique(bl)
    if write_to_oseg:
        for lb in un:
            if lb != 0:
                new_slabel = new_label_str_format.format(vessel_label, lb)
                new_nlabel = oseg.nlabels(new_slabel)
                oseg.segmentation[bl == lb] = new_nlabel

    # ima.distance_segmentation(oseg.select_label(vessel_label))
    return bl
예제 #2
0
def label_volumetric_vessel_tree(oseg,
                                 vessel_label=None,
                                 write_to_oseg=True,
                                 new_label_str_format="{}{:03d}"):
    """
    Split vessel by branches and put it in segmentation and slab.

    :param oseg: OrganSegmentation object with segmentation, voxelsize_mm and slab
    :param vessel_label: int or string label with vessel. Everything above zero is used if vessel_label is set None.
    :param write_to_oseg: Store output into oseg.segmentation if True. The slab is also updated.
    :param new_label_str_format: format of new slab
    :return:
    """
    logger.debug("vessel_label {}".format(vessel_label))
    logger.debug("python version {} {}".format(sys.version_info,
                                               sys.executable))
    import skelet3d
    if vessel_label is None:
        vessel_volume = oseg.segmentation > 0
    else:
        vessel_volume = oseg.select_label(vessel_label)

    # print(np.unique(vessel_volume))
    skel = skelet3d.skelet3d(vessel_volume)
    skan = skelet3d.SkeletonAnalyser(skel, volume_data=vessel_volume)
    skan.skeleton_analysis()
    bl = skan.get_branch_label()
    un = np.unique(bl)
    logger.debug("skelet3d branch label min: {}, max: {}, dtype: {}".format(
        np.min(bl), np.max(bl), bl.dtype))
    if write_to_oseg:
        if 127 < np.max(bl) and ((oseg.segmentation.dtype == np.int8) or
                                 (oseg.segmentation.dtype == np.uint8)):
            oseg.segmentation = oseg.segmentation.astype(np.int16)
        for lb in un:
            if lb != 0:
                new_slabel = new_label_str_format.format(vessel_label, lb)
                new_nlabel = oseg.nlabels(new_slabel)
                oseg.segmentation[bl == lb] = new_nlabel

    # ima.distance_segmentation(oseg.select_label(vessel_label))
    return bl
예제 #3
0
파일: livseg.py 프로젝트: pilseneyes/vebase
def voda_sk(organ_seg, liver_seg, voxelsize, cr):
    """
    build skeleton of porta
    voda_sk(xyz) build distance 3D map and compute volumes of liverpart for each skeleton element of chosen dataset
    voda_sk(xyz) takes argument from load_vdata - it need volumetric data of porta, liver and vox size (not ncsry..)
    """
    l_d = [organ_seg, liver_seg, voxelsize, cr]

    tempsz = (np.shape(l_d[0][1]))
    ylab = tempsz[1]
    xlab = tempsz[0]
    
    volume_data = l_d[0]
    #skeletonization - params are saved in var stats
    skelet = skelet3d.skelet3d(volume_data)
    skan = skelet3d.SkeletonAnalyser(skelet, volume_data = volume_data, voxelsize_mm = [1, 1, 1])
    stats = skan.skeleton_analysis()
    edge_number = 1

    #build of 3D distance map
    linesarr_x = []
    linesarr_y = []
    linesarr_z = []
    temparr = skan.sklabel
    counter = 0
    tempslx_l = 0
    for i in range(0,len(l_d[0])):
        for j in range(0, xlab):
            for k in range(0, ylab):
                if (temparr[i][j][k] != 0):
                    linesarr_x.append(k)
                    linesarr_y.append(j)
                    counter = counter + 1
        for z in range(0, counter):
            tempslx_l = i * l_d[2] #third value in l_data is space between slices of current datasete
            linesarr_z.append(tempslx_l)
            counter = 0
    distance_map = imlb.distance_segmentation(temparr)
    nodeArr = []
    for i in range(1, (len(stats)) + 1):
        #print(i)
        try:
            nodeArr.append((stats[i]['nodeA_ZYX']))
        except:
            None
        try:
            nodeArr.append((stats[i]['nodeB_ZYX']))
        except:
            None
    zdata = []
    xdata = []
    ydata = []
    temp_xp = 2
    temp_yp = 1
    temp_zp = 0
    for i in range(len(nodeArr)):
        zdata.append(nodeArr[i][temp_zp])
        ydata.append(nodeArr[i][temp_yp])
        xdata.append(nodeArr[i][temp_xp])
        zdata[i] = zdata[i]*l_d[2]
    #build list of all elements in skeleton
    list_= []
    for slx in range(len(l_d[1])):
        for x in range(0, xlab):
            for y in range(0, ylab):
                if(distance_map[slx, x, y]) < 1e5:
                    list_.append(distance_map[slx, x, y])
    set(list_)
    list_of_areas = set(list_)
    list_of_areas_arr = list(list_of_areas)
    list_of_areas_arr_edges = []
    for x in range(0, len(list_of_areas_arr)):
        if(list_of_areas_arr[x] > 0):
            list_of_areas_arr_edges.append(list_of_areas_arr[x])

    #plot data if needed          
    slicearr_p = []
    zte_p = []
    xte_p = []
    yte_p = []
    counter = 0

    for slx in range(len(l_d[0])):
        for x in range(0, xlab):
            for y in range(0, ylab):
                if(l_d[0][slx, x, y]) == 1:
                    xte_p.append(y)
                    yte_p.append(x)
                    counter = counter + 1
        for z in range(0, counter):
            tempslx_p = slx * l_d[2]
            zte_p.append(tempslx_p)
        counter = 0
    slicearr_p.append(xte_p)
    slicearr_p.append(yte_p)
    slicearr_p.append(zte_p)

    slicearr_l = []
    zte_l = []
    xte_l = []
    yte_l = []
    counter = 0

    for slx in range(len(l_d[1])):
        for x in range(0, xlab):
            for y in range(0, ylab):
                if(l_d[1][slx, x, y]) == 1:
                    xte_l.append(y)
                    yte_l.append(x)
                    counter = counter + 1
        for z in range(0, counter):
            tempslx_l = slx * l_d[2]
            zte_l.append(tempslx_l)
        counter = 0
    slicearr_l.append(xte_l)
    slicearr_l.append(yte_l)
    slicearr_l.append(zte_l)

    dist_map_x = []
    dist_map_y = []
    dist_map_z = []
    counter_sl = 0

    #compute of ALL VOLUMES - takes time - control print for each skelet element
    dist_map_final_liver_vol_temp = []
    dist_map_final_liver_vol = []

    loar_l = len(list_of_areas_arr)
    if(loar_l % 2 == 0):
        loar_l = loar_l/2
    else:
        loar_ = loar_l/2 + 1


    for area in range(0, loar_l):
        temp_area = list_of_areas_arr[area]
        for slc in range(0, len(l_d[1])):
            for x in range(0, xlab):
                for y in range(0, ylab):
                    if ((l_d[1][slc, x, y]) == 1):
                        if(((distance_map[slc, x, y]) == temp_area)):
                            dist_map_x.append(y)
                            dist_map_y.append(x)
                            counter_sl = counter_sl + 1
            for slic in range(0, counter_sl):
                dist_map_z.append(slc)
            counter_sl = 0
        dist_map_final_liver_vol_temp.append(dist_map_z)
        dist_map_final_liver_vol_temp.append(dist_map_y)
        dist_map_final_liver_vol_temp.append(dist_map_x)
        dist_map_final_liver_vol_temp.append(temp_area)
        dist_map_final_liver_vol.append(dist_map_final_liver_vol_temp)
        dist_map_final_liver_vol_temp = []
        dist_map_x = []
        dist_map_y = []
        dist_map_z = []
        print("computing volume of area: ", temp_area, "Num. of areas in this dataset: ", len(list_of_areas_arr))
        print("$$$$$-------------------------------------------------------------------$$$$$")

    # ret_array = []
    # ret_array.append(stats)
    # ret_array.append(list_of_areas_arr_edges)
    # ret_array.append(dist_map_final_liver_vol)
    return stats, list_of_areas_arr_edges, dist_map_final_liver_vol
예제 #4
0
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © %YEAR% %USER% <%MAIL%>
#
# Distributed under terms of the %LICENSE% license.
"""
%HERE%
"""

import skelet3d
import numpy as np
# Create donut shape
volume_data = np.zeros([3, 7, 9], dtype=np.int)
volume_data[:, :, 1:3] = 1
volume_data[:, 5, 2:9] = 1
volume_data[:, 0:7, 5] = 1
skelet = skelet3d.skelet3d(volume_data)

skan = skelet3d.SkeletonAnalyser(skelet,
                                 volume_data=volume_data,
                                 voxelsize_mm=[1, 1, 1])
stats = skan.skeleton_analysis()
edge_number = 1
print(stats[edge_number]['radius_mm'])