Beispiel #1
0
def quickComputeFillFromEllipseCenters(inputVolume1, contourList, fillMethod):
    """
    Uses shellActiveContourWrapper to perform level set initiated at all contours in contourList.
    Parameters:
    inputVolume1: guides the level set operation
    contourList: list of seed contours
    fillMethod: specifies the fill method, use 'shellActiveContour'    
    """

    global numpyBufferFromPyBufferClass
    global img

    InternalPixelType = itk.F
    Dimension = 3
    ImageType = itk.Image[InternalPixelType, Dimension]
    converter = itk.PyBuffer[ImageType]
    IT = itk.Image.F3
    img = IT.New(Regions=[inputVolume1.shape[2], inputVolume1.shape[1], inputVolume1.shape[0]])
    img.Allocate()
    img.FillBuffer(0)
    InternalImageType = IT
    OutputImageType = IT

    numpyBufferFromPyBufferClass = itk.PyBuffer[IT].GetArrayFromImage(img)
    numpyBufferFromPyBufferClass[:, :, :] = inputVolume1
    
    centerList = []

    contourCount = 0
    print "number of contours", len(contourList)

    erodedContoursNode = GroupNode('ErodedContours')

    #file = open("o:\\temp\\contours.txt", 'w')
    for contour in contourList:
        center = contour.bestFitEllipse.center

        #centerList.append(center)

        #print "starting erosion", contourCount
        #print dir(copy_module)
        #locations = copy_module.deepcopy(contour.locations())
        #print locations
        #file.write(str(locations))
        #print "number of points", len(locations)
        erodedContourSet = erosion.erosion_polygon(contour.locations(), 10)
        erodedContourSet = erodedContourSet +\
            erosion.erosion_polygon(contour.locations(), 20)

        #print "completed erosion"
        for erodedContour in erodedContourSet:
            erodedContourObject = Contour()
            erodedContourObject.addNumpyPoints(erodedContour)
            erodedContoursNode.addObject(erodedContourObject)
            #for point in contour.locations():
            for point in erodedContour:
                #centerList.append(((array(point) - array(center)) / 2.0) + array(center))
                centerList.append(array(point))

        contourCount += 1

    #file.close()
    count = 0


    #todo: check if this center variable needs to be here
    if len(contourList) > 0:
        center = contour.bestFitEllipse.center

    print "compute fill", count, "total", len(contourList)

    inputVolume = converter.GetImageFromArray(numpyBufferFromPyBufferClass)
    inputVolumeFileName = os.path.join(default_path.defaultOutputPath, "hello.nrrd")
    # Writing and reading from file works around a WrapITK bug
    itk.write(inputVolume, inputVolumeFileName)
    reader1 = itk.ImageFileReader[InternalImageType].New(FileName=inputVolumeFileName)
    image1  = reader1.GetOutput()

    if fillMethod == 'fastMarch':
        #resultItkArray = fastMarch(image1, center, contour, inputVolume)
        raise Exception, "not implemented"
    elif fillMethod == 'shellActiveContour':
        #resultItkArray = shellActiveContourWrapper(image1, centerList, contour, inputVolume)
        resultItkArray = shellActiveContourWrapper(image1, centerList, None, inputVolume)
    else: raise Exception, "Invalid fill method"
    
    tempFileName = os.path.join(default_path.defaultOutputPath, "fillResult.nrrd")
    print "writing file", tempFileName
    itk.write(resultItkArray, tempFileName)

    resultVolume = numpy.array(converter.GetArrayFromImage(resultItkArray))
    if 1:
        #contour.features['fastMarchFromEllipseCenter'] = resultVolume
        blob = Blob()
        #binaryResult = resultVolume < 10000000
        binaryResult = resultVolume < 0.5
        if 0:
            print "starting flood fill"
            pointList = floodFill(binaryResult, center)
            print "flood fill finished"
            blob.setPoints(pointList)

        if count == 0:
            sumVolume = (1.0 * binaryResult)
        else:
            sumVolume += (1.0 * binaryResult)

        #contour.features['fastMarchBlobFromEllipseCenter'] = blob
        print "result volume"
    count += 1

    return sumVolume, erodedContoursNode
Beispiel #2
0
def computeFillFromEllipseCenters(inputVolume1, contourList, fillMethod):
    """
    Uses shellActiveContourWrapper to perform level set separately at each contour.
    Nolonger using this function.
    """

    global numpyBufferFromPyBufferClass
    global img

    InternalPixelType = itk.F
    Dimension = 3
    ImageType = itk.Image[InternalPixelType, Dimension]
    converter = itk.PyBuffer[ImageType]
    IT = itk.Image.F3
    img = IT.New(Regions=[inputVolume1.shape[2], inputVolume1.shape[1], inputVolume1.shape[0]])
    img.Allocate()
    img.FillBuffer(0)
    InternalImageType = IT
    OutputImageType = IT

    numpyBufferFromPyBufferClass = itk.PyBuffer[IT].GetArrayFromImage(img)
    numpyBufferFromPyBufferClass[:, :, :] = inputVolume1
    
    count = 0
    for contour in contourList:
        center = contour.bestFitEllipse.center
        print "compute fill", count, "total", len(contourList)
    
        inputVolume = converter.GetImageFromArray(numpyBufferFromPyBufferClass)
        inputVolumeFileName = os.path.join(default_path.defaultOutputPath, "hello.nrrd")
        # Writing and reading from file works around a WrapITK bug
        itk.write(inputVolume, inputVolumeFileName)
        reader1 = itk.ImageFileReader[InternalImageType].New(FileName=inputVolumeFileName)
        image1  = reader1.GetOutput()

        if fillMethod == 'fastMarch':
            resultItkArray = fastMarch(image1, center, contour, inputVolume)
        elif fillMethod == 'shellActiveContour':
            resultItkArray = shellActiveContourWrapper(image1, center, contour, inputVolume)
        else: raise Exception, "Invalid fill method"
        
        tempFileName = os.path.join(default_path.defaultOutputPath, "fillResult.nrrd")
        print "writing file", tempFileName
        itk.write(resultItkArray, tempFileName)

        resultVolume = numpy.array(converter.GetArrayFromImage(resultItkArray))
        if 1:
            #contour.features['fastMarchFromEllipseCenter'] = resultVolume
            blob = Blob()
            #binaryResult = resultVolume < 10000000
            binaryResult = resultVolume < 0.5
            if 0:
                print "starting flood fill"
                pointList = floodFill(binaryResult, center)
                print "flood fill finished"
                blob.setPoints(pointList)

            if count == 0:
                sumVolume = (1.0 * binaryResult)
            else:
                sumVolume += (1.0 * binaryResult)

            #contour.features['fastMarchBlobFromEllipseCenter'] = blob
            print "result volume"
        count += 1

    return sumVolume