Beispiel #1
0
    def _getmask(pia, wm, polys, shape):
        from .. import polyutils
        mask = sparse.csr_matrix((len(wm), np.prod(shape)))

        from tvtk.api import tvtk
        measure = tvtk.MassProperties()
        planes = tvtk.PlaneCollection()
        for norm in np.vstack([-np.eye(3), np.eye(3)]):
            planes.append(tvtk.Plane(normal=norm))
        ccs = tvtk.ClipClosedSurface(clipping_planes=planes)
        feats = tvtk.FeatureEdges(boundary_edges=1,
                                  non_manifold_edges=0,
                                  manifold_edges=0,
                                  feature_edges=0)
        feats.set_input(ccs.output)

        surf = polyutils.Surface(pia, polys)
        for i, (pts, faces) in enumerate(surf.polyhedra(wm)):
            if len(pts) > 0:
                poly = tvtk.PolyData(points=pts, polys=faces)
                measure.set_input(poly)
                measure.update()
                totalvol = measure.volume
                ccs.set_input(poly)
                measure.set_input(ccs.output)

                bmin = pts.min(0).round().astype(int)
                bmax = (pts.max(0).round() + 1).astype(int)
                vidx = np.mgrid[bmin[0]:bmax[0], bmin[1]:bmax[1],
                                bmin[2]:bmax[2]]
                for vox in vidx.reshape(3, -1).T:
                    try:
                        idx = np.ravel_multi_index(vox[::-1], shape)
                        for plane, m in zip(planes,
                                            [.5, .5, .5, -.5, -.5, -.5]):
                            plane.origin = vox + m

                        ccs.update()
                        if ccs.output.number_of_cells > 2:
                            measure.update()
                            mask[i, idx] = measure.volume

                    except ValueError:
                        print('Voxel not in volume: (%d, %d, %d)' % tuple(vox))

                mask.data[mask.indptr[i]:mask.indptr[i + 1]] /= mask[i].sum()

        return mask
def volumen_voxels(imagen):
    mask3d = imagen > int(sys.argv[2])
    mask3d = mask3d.astype(int)
    objeto = tvtk.ImageData(spacing=(1, 1, 1), origin=(0, 0, 0))
    objeto.point_data.scalars = mask3d.ravel()
    objeto.point_data.scalars.name = 'scalars'
    objeto.dimensions = imagen.shape
    iso = tvtk.ImageMarchingCubes()
    configure_input(iso, objeto)
    mass = tvtk.MassProperties()
    configure_input(mass, iso)
    volumen3d = mass.volume

    print "volumen en voxels por tratamiento 3d:", volumen3d

    nGFPfinal = (mask3d == 1).sum()
    fGmascarafinal = np.multiply(mask3d, imagen)
    intensidadmedia = float((fGmascarafinal).sum()) / volumen3d
    print "la intensidad media con el tratamiento 3d es:", intensidadmedia
    print "el volumen haciendo la suma de los voxels de la mask es:", nGFPfinal
    intensidadmedia = float((fGmascarafinal).sum()) / nGFPfinal
    print "la intensidad media con la suma de voxels de la mask es:", intensidadmedia

    return mask3d
Beispiel #3
0
def measure_volume(pts, polys):
    from tvtk.api import tvtk
    pd = tvtk.PolyData(points=pts, polys=polys)
    mp = tvtk.MassProperties(input=pd)
    return mp.volume
Beispiel #4
0
def volumen_voxels(imagen):
    nGFP = [0 for x in range(0, int(sys.argv[2]) / int(sys.argv[3]) + 1)]
    meanGFP = [0 for x in range(0, int(sys.argv[2]) / int(sys.argv[3]) + 1)]
    ndatos = 0
    for umbral in range(0,
                        int(sys.argv[2]) + int(sys.argv[3]), int(sys.argv[3])):
        mascaraG = imagen > umbral
        nGFP[ndatos] = (mascaraG == 1).sum()
        if nGFP[ndatos] != 0:
            fGmascara = np.multiply(mascaraG, imagen)
            meanGFP[ndatos] = float((fGmascara).sum()) / nGFP[ndatos]
        else:
            meanGFP[ndatos] = 0

        ndatos += 1

    f = open(sys.argv[1], "w")
    for nG in nGFP:
        f.write(str(nG))
        f.write(" ")
    f.write("\n")
    for mG in meanGFP:
        f.write(str(mG))
        f.write(" ")
    f.write("\n")

    ndatosmenos1 = ndatos - 1
    comparacion = np.zeros((ndatosmenos1))

    for i in range(ndatosmenos1):
        if nGFP[i] != 0:
            comparacion[i] = float(nGFP[i + 1]) / nGFP[i]
        else:
            comparacion[i] = 0
    difumb = 1.0
    encontrado = 0
    while encontrado == 0:
        encontrado = 0
        difumb = difumb - 0.005
        contador = 0
        for i in range(ndatosmenos1):

            if comparacion[i] > difumb:
                contador += 1

                if contador == 3:
                    umbrales = (i - 1) * int(sys.argv[3])
                    encontrado = 1
            else:
                contador = 0

    mask3d = imagen > umbrales
    mask3d = mask3d.astype(int)
    objeto = tvtk.ImageData(spacing=(1, 1, 1), origin=(0, 0, 0))
    objeto.point_data.scalars = mask3d.ravel()
    objeto.point_data.scalars.name = 'scalars'
    objeto.dimensions = imagen.shape
    iso = tvtk.ImageMarchingCubes()
    configure_input(iso, objeto)
    mass = tvtk.MassProperties()
    configure_input(mass, iso)
    volumen3d = mass.volume
    f.write(str(umbrales))
    f.write("\n")
    f.close()

    print "el umbral final es:", umbrales
    print "volumen en voxels por tratamiento 3d:", volumen3d

    nGFPfinal = (mask3d == 1).sum()
    fGmascarafinal = np.multiply(mask3d, imagen)
    intensidadmedia = float((fGmascarafinal).sum()) / volumen3d
    print "la intensidad media con el tratamiento 3d es:", intensidadmedia
    print "el volumen haciendo la suma de los voxels de la mask es:", nGFPfinal
    intensidadmedia = float((fGmascarafinal).sum()) / nGFPfinal
    print "la intensidad media con la suma de voxels de la mask es:", intensidadmedia

    return mask3d