Exemplo n.º 1
0
    def segmentBasedOnThreshold(self, inim, seg):
        init_ls = sitk.SignedMaurerDistanceMap(seg,
                                               insideIsPositive=False,
                                               useImageSpacing=True)
        stats = sitk.LabelStatisticsImageFilter()
        stats.Execute(inim, seg)

        factor = .1
        lower_threshold = stats.GetMinimum(1) - factor * stats.GetSigma(1)
        upper_threshold = 255  # np.min((255,stats.GetMean(1)+factor*stats.GetSigma(1)))

        lsFilter = sitk.ThresholdSegmentationLevelSetImageFilter()
        lsFilter.SetLowerThreshold(lower_threshold)
        lsFilter.SetUpperThreshold(upper_threshold)
        lsFilter.SetMaximumRMSError(0.02)
        lsFilter.SetNumberOfIterations(1000)
        lsFilter.SetCurvatureScaling(1.5)
        lsFilter.SetPropagationScaling(1)
        lsFilter.ReverseExpansionDirectionOff()
        ls = lsFilter.Execute(init_ls, sitk.Cast(inim, sitk.sitkFloat32))
        mask = sitk.Cast(255 * (ls < 0), sitk.sitkUInt8)
        if 0:
            simg_255 = sitk.Cast(sitk.RescaleIntensity(inim), sitk.sitkUInt8)
            idx = self.seeds[0]
            zslice_offset = 1
            # myshow3d(sitk.LabelOverlay(self.inputimage, seg),
            #          zslices=range(idx[2] - zslice_offset, idx[2] + zslice_offset + 1, zslice_offset), dpi=20, title='init')

            # myshow3d(sitk.LabelOverlay(simg_255, ls <= 0),
            #          zslices=range(idx[2] - zslice_offset, idx[2] + zslice_offset + 1, zslice_offset), dpi=20, title='test')

            myshow3d(sitk.LabelOverlay(self.inputimage, seg),
                     zslices=[17, 18, 19, 20, 21],
                     dpi=20,
                     title='init')
            myshow3d(sitk.LabelOverlay(simg_255, mask),
                     zslices=[17, 18, 19, 20, 21],
                     dpi=20,
                     title='init')

        return mask
Exemplo n.º 2
0
import SimpleITK as sitk
from myshow import myshow, myshow3d
from downloaddata import fetch_data as fdata

##############################################################################
# Load Images
# -----------

img_T1 = sitk.ReadImage(
    fdata("nac-hncma-atlas2013-Slicer4Version/Data/A1_grayT1.nrrd"))

# To visualize the labels image in RGB needs a image with 0-255 range
img_T1_255 = sitk.Cast(sitk.RescaleIntensity(img_T1), sitk.sitkUInt8)

size = img_T1.GetSize()
myshow3d(img_T1_255, zslices=range(50, size[2] - 50, 20), title='T1')

##############################################################################
# Seed selection
# --------------
#
# Earlier we used 3D Slicer to determine that index: (132,142,96) was a
# good seed for the left lateral ventricle.

seed = (132, 142, 96)

seg = sitk.Image(img_T1.GetSize(), sitk.sitkUInt8)
seg.CopyInformation(img_T1)
seg[seed] = 1

seg = sitk.BinaryDilate(seg, 3)
Exemplo n.º 3
0
    fact = statF.GetMean() / statF.GetSigma()
#RS=sitk.ConnectedThreshold(imFin2Cleaned, seedList=seed,lower=np.abs(statF.GetMinimum()),upper=statF.GetMean()/(np.abs(statF.GetMinimum()*(statF.GetSigma()/statF.GetMean()))))
RS = sitk.ConnectedThreshold(seedImage,
                             seedList=seed,
                             lower=np.sqrt(np.abs(statF.GetMinimum())),
                             upper=seedImage[seed[0]] * fact +
                             np.abs(statF.GetMinimum()))
RS = cleanImg(RS, tLargeClean2)
RSClean1 = sitk.BinaryOpeningByReconstruction(RS, [10, 10, 10])
RSClean2 = sitk.BinaryClosingByReconstruction(RSClean1, [10, 10, 10])
plotSlices(RSClean2, 'sagittal')
#%%
RSCleanInt = floatToInt(RSClean2)
size = imgResult.GetSize()
myshow3d(sitk.LabelOverlay(imgResult, RSCleanInt),
         yslices=range(50, size[1] - 50, 30),
         zslices=range(50, size[2] - 50, 20),
         dpi=30)
#%%

x = findBladderCenter(imFin2Cleaned, sobGauss2Cleaned, tLarge)
gaussian.SetSigma(3)
xy = gaussian.Execute(x)
statF3 = sitk.StatisticsImageFilter()
statF3.Execute(xy)
ndimage.measurements.center_of_mass(sitk.GetArrayFromImage(xy))
###Need to be numpy Arrays, are sitk imgs
xz = xy == -1000
xy[102, 89, 87] = 1000
plotSlices(xy < np.square(np.abs(statF3.GetMinimum())), 'sagittal')
plotSlices(xy, 'sagittal')
Exemplo n.º 4
0
def getQ(imageFile, r = 2, multi = 7.5):
    ##############################################################################
    # Load Images
    # -----------
    # r = 2 # resampling factor
    resSlope = 1 #from dcm file
    reIntercep = 0 #from dcm file
    # u = slope * signal + intercept
    img_T1 = sitk.ReadImage(imageFile)
    img_T1 = sitk.Expand(img_T1, [r,r,1] * 3, sitk.sitkLinear)
    imArray = sitk.GetArrayFromImage(img_T1)
    imArray = imArray[0, :, :]
    # To visualize the labels image in RGB needs a image with 0-255 range
    img_T1_255 = sitk.Cast(sitk.RescaleIntensity(img_T1), sitk.sitkUInt8)


    ny, nx = img_T1_255.GetSize()[0], img_T1_255.GetSize()[1]
    imArray255 = sitk.GetArrayFromImage(img_T1_255)
    imArray255 = imArray255[0, :, :]
    print(imArray255.shape)
    imArray255 = imArray255.reshape((nx, ny))
    size = img_T1_255.GetSize()
    print('size = ', size)
    # myshow(img_T1_255, title='T1')  # , zslices=range(50, size[2] - 50, 20), title='T1')

    ##############################################################################
    # Seed selection
    # --------------
    print('getpos po ok')

    x, y, val = getPos(imArray255)
    print('getpos ok')
    seed = (x, y, 0)
    print('seed: ', seed)
    seg = sitk.Image(img_T1.GetSize(), sitk.sitkUInt8)
    seg.CopyInformation(img_T1)
    seg[seed] = 1

    seg = sitk.BinaryDilate(seg, 3)

    # myshow3d(sitk.LabelOverlay(img_T1_255, seg),
    #          xslices=range(img_T1.GetSize()[0], img_T1.GetSize()[1]), title="Initial Seed")

    ##############################################################################
    # ``ConfidenceConnected``
    # ^^^^^^^^^^^^^^^^^^^^^^^
    #
    #
    # Unlike in ``ConnectedThreshold``, you need not select the bounds in
    # ``ConfidenceConnected`` filter. Bounds are implicitly specified as
    # :math:`\mu\pm c\sigma`, where :math:`\mu` is the mean intensity of the seed
    # points, :math:`\sigma` their staimArray255rd deviation and :math:`c` a user specified
    # constant.
    #
    # This algorithm has some flexibility which you should familiarize yourself with:
    #
    # * The ``multiplier`` parameter is the constant :math:`c` from the formula above.
    # * You can specify a region around each seed point ``initialNeighborhoodRadius``
    #   from which the statistics are estimated, see what happens when you set it to zero.
    # * The ``numberOfIterations`` allows you to rerun the algorithm. In the first
    #   run the bounds are defined by the seed voxels you specified, in the
    #   following iterations :math:`\mu` and :math:`\sigma` are estimated from
    #   the segmented points and the region growing is updated accordingly.


    seg_conf = sitk.ConfidenceConnected(img_T1, seedList=[seed],
                                        numberOfIterations=1,
                                        multiplier=multi,
                                        initialNeighborhoodRadius=1,
                                        replaceValue=1)
    #
    # myshow3d(sitk.LabelOverlay(img_T1_255, seg_conf),
    #          xslices=range(img_T1.GetSize()[0], img_T1.GetSize()[1]), title="ConfidenceConnected")

    # Clean up, clean up
    # ------------------
    #
    # Use of low level segmentation algorithms such as region growing is often followed by a clean up step. In this step we fill holes and remove small connected components. Both of these operations are achieved by using binary morphological operations, opening (``BinaryMorphologicalOpening``) to remove small connected components and closing (``BinaryMorphologicalClosing``) to fill holes.
    #
    # SimpleITK supports several shapes for the structuring elements (kernels) including:
    #
    # - sitkAnnulus
    # - sitkBall
    # - sitkBox
    # - sitkCross
    #
    # The size of the kernel can be specified as a scalar (same for all dimensions) or as a vector of values, size per dimension.
    #
    # The following code illustrates the results of such a clean up, using
    # closing to remove holes in the original segmentation.


    vectorRadius = (2, 2, 1)
    kernel = sitk.sitkBall
    seg_clean = sitk.BinaryMorphologicalClosing(seg_conf,
                                                vectorRadius,
                                                kernel)

    myshow3d(sitk.LabelOverlay(img_T1_255, seg_clean), title="Cleaned up segmentation")

    # print seg_clean[234, 256,1]
    segArray = sitk.GetArrayFromImage(seg_clean)
    # print(segArray.shape)
    segArray = segArray[0,:,:]
    segImArray = imArray[segArray>0]
    # print(segImArray.shape, len(segImArray))
    # u = slope * signal + intercept
    sumPix = np.sum(segImArray)
    vag = np.mean(segImArray)
    # print('Q =', sumPix, 'Vavg = ', vag)
    # segImArrayPhysical = segImArray * resSlope + reIntercep #in physical units
    # sumPix = np.sum(segImArrayPhysical)
    # vag = np.mean(segImArrayPhysical)
    # print('Q =', sumPix, 'Vavg = ', vag)

    return  segArray
Exemplo n.º 5
0
seed = (185, 217, 407)

seg = sitk.Image(img16.GetSize(), sitk.sitkUInt8)
seg.CopyInformation(img)
seg[seed] = 1

seg = sitk.BinaryDilate(seg, 3)

# sitk.LabelOverlay(img1, sitk.LabelContour(img1_seg), 1.0)

# ImageToVTKImageFilter[itk.Image[itk.UC,3]].New()

if 1:
    myshow3d(sitk.LabelOverlay(img255, seg, colormap=[255, 0, 0]),
             xslices=range(seed[0], seed[0] + 1),
             yslices=range(seed[1], seed[1] + 1),
             zslices=range(seed[2], seed[2] + 1),
             title="Initial Seed")

    # using FilterType = itk::BinaryMask3DMeshSource<ImageType, MeshType>;
    # FilterType::Pointer filter = FilterType::New();
    # filter->SetInput(threshold->GetOutput());
    # filter->SetObjectValue(255);
    #
    # using WriterType = itk::MeshFileWriter<MeshType>;
    # WriterType::Pointer writer = WriterType::New();
    # writer->SetFileName(outputFileName);
    # writer->SetInput(filter->GetOutput());

# 181 - 576 (MITK)
# 168 - 576
Exemplo n.º 6
0
                                     slice_fraction = 0.75, 
                                     vert_fract = 0.50, 
                                     horiz_fract = 0.50,
                                     larger_contour_offset_fract = 0.4) 

#Displaying only one of the two seeds. 
seed = seed_1
seg = sitk.Image(img_T1.GetSize(), sitk.sitkUInt8)
seg.CopyInformation(img_T1)
seg[seed] = 1


seg = sitk.BinaryDilate(seg, 3)

myshow3d(sitk.LabelOverlay(img_T1, seg),
         xslices=range(seed[0], seed[0]+1), yslices=range(seed[1], seed[1]+1),
         zslices=range(seed[2], seed[2]+1), title="Initial Seed")


'''Getting the intensity level value from Otsu's thresholding '''
def intensity_threshold(img):
    otsu_filter = sitk.OtsuThresholdImageFilter()
    otsu_filter.SetInsideValue(0)
    otsu_filter.SetOutsideValue(1)
    seg = otsu_filter.Execute(img)
    threshold = otsu_filter.GetThreshold()
    return threshold
threshold = intensity_threshold(img_T1)


'''Applying the 3D region growing algorithm: Neighborhood Connected'''
Exemplo n.º 7
0
myshow(img_T1, title='T1')
myshow(img_T2, title='T2')
myshow(sitk.LabelToRGB(img_labels), title='lables')

##############################################################################
# Physical Space Issues
# ---------------------
# Why doesn't this work? The images do not overlap in physical space.
#
# All the functions in SimpleITK work on underlying physical space. Thefore,
# mismatch in physical space like image origin etc. will raise errors.
try:
    size = img_T1.GetSize()
    myshow3d(sitk.LabelOverlay(img_T1, img_labels),
             yslices=range(50, size[1] - 50, 20),
             zslices=range(50, size[2] - 50, 20), dpi=30)
except Exception as e:
    print(e)

##############################################################################
# Two ways to solve our problem:
#
# 1. resample the labels onto the image grid
# 2. resample the image onto the label grid.
#
# The difference between the two from a computation standpoint depends on the
# grid sizes and on the interpolator used to estimate values at non-grid
# locations.
#
# Note interpolating a label image with an interpolator that can generate
Exemplo n.º 8
0
import SimpleITK as sitk
from myshow import myshow, myshow3d
from downloaddata import fetch_data as fdata

##############################################################################
# Load Image
# ----------

img_T1 = sitk.ReadImage(
    fdata("nac-hncma-atlas2013-Slicer4Version/Data/A1_grayT1.nrrd"))

# To visualize the labels image in RGB needs a image with 0-255 range
img_T1_255 = sitk.Cast(sitk.RescaleIntensity(img_T1), sitk.sitkUInt8)

size = img_T1.GetSize()
myshow3d(img_T1_255, zslices=range(50, size[2] - 50, 20))

##############################################################################
# Threshold
# ---------
# Let's pick a threshold 200 for thresholding.

seg = img_T1 > 200
myshow(sitk.LabelOverlay(img_T1_255, seg), "Basic Thresholding")

##############################################################################
# You can also use a upper and lower threshold.

seg = sitk.BinaryThreshold(img_T1,
                           lowerThreshold=100,
                           upperThreshold=400,