Exemplo n.º 1
0
def median_filter_with_mask(img, mask, radius_dilatation, radius_median):

    # debug
    debug = False
    if debug:
        input = img
        itk.imwrite(img, 'ctvi_before.mhd')
        ctvim = itk.median_image_filter(img, radius=radius_median)
        itk.imwrite(ctvim, 'ctvi_median.mhd')

    dimg = dilate_at_boundaries(img, radius_dilatation)

    if debug:
        itk.imwrite(dimg, 'ctvi_before_dilated.mhd')

    imgm = itk.median_image_filter(dimg, radius=radius_median)

    if debug:
        itk.imwrite(imgm, 'ctvi_median_after_dilated.mhd')

    # reapply mask after median
    imgm = itk.array_from_image(imgm)
    imgm = imgm.astype('float')
    imgm[mask == 0] = 0

    if debug:
        imgm = imgm.astype(np.float32)
        a = itk.image_from_array(imgm)
        a.CopyInformation(input)
        itk.imwrite(a, 'ctvi_median_final.mhd')

    return imgm
def visualize(argv):
    parser = argparse.ArgumentParser(
        description=
        'Apply median image filter to input image and visualize result')
    parser.add_argument('input_image', type=str, help='Input image file name')
    parser.add_argument('output_image',
                        type=str,
                        help='Output image file name')
    parser.add_argument('radius', type=int, help='Median filter radius')
    args = parser.parse_args()

    # Open image with ITK
    image = itk.imread(args.input_image)
    median_image = itk.median_image_filter(image, Radius=args.radius)
    itk.imwrite(median_image, args.output_image)
    itk.ViewImage[median_image].View(median_image)
Exemplo n.º 3
0
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#==========================================================================*/

#
#  Example on the use of the MedianImageFilter
#

import itk
from sys import argv

input_filename = argv[1]
output_filename = argv[2]
radius = int(argv[3])

reader = itk.ImageFileReader.IUC2.New(FileName=input_filename)

# test the deduction of the template parameter from the input
filt = itk.MedianImageFilter.New(reader, Radius=radius)
filt.Update()
result = filt.GetOutput()
watcher = itk.XMLFilterWatcher(filt, "filter")

# test the update of the filter with the snake case function
# and the setting of parameter inside it
result = itk.median_image_filter(reader, radius=radius)

# test the write method
itk.imwrite(result, output_filename)
Exemplo n.º 4
0
#!/usr/bin/env python3

import itk
import sys

input_filename = sys.argv[1]

# An apriori ImageType
PixelType = itk.F
ImageType = itk.Image[PixelType, 2]
image = itk.imread(input_filename, PixelType)

# An image type dynamically determined from the type on disk
image = itk.imread(input_filename)
ImageType = type(image)

# Functional interface
# The `ttype` keyword argument specifies the filter type.
smoothed = itk.median_image_filter(image, ttype=(ImageType, ImageType))

# Object-oriented interface
reader = itk.ImageFileReader[ImageType].New(file_name=input_filename)
median = itk.MedianImageFilter[ImageType, ImageType].New()
median.SetInput(reader.GetOutput())
median.Update()
smoothed = median.GetOutput()
Exemplo n.º 5
0
input_filename = argv[1]
output_filename = argv[2]
radius = int(argv[3])

reader = itk.ImageFileReader.IUC2.New(FileName=input_filename)

# test the deduction of the template parameter from the input
filt = itk.MedianImageFilter.New(reader, Radius=radius)
filt.Update()
filt_result = filt.GetOutput()
watcher = itk.XMLFilterWatcher(filt, "filter")

# test the update of the filter with the snake case function
# and the setting of parameter inside it
result_snake_case = itk.median_image_filter(reader, radius=radius)

# SetPrimaryInputName("ValidInput");
compare_filter = itk.ComparisonImageFilter.New(filt_result,
                                               TestInput=result_snake_case)
compare_filter.Update()
assert compare_filter.GetMaximumDifference() < 0.000000001

if version_info >= (3, 8):
    # Check the type hints
    type_hints = get_type_hints(itk.median_image_filter, globalns={"itk": itk})

    assert "args" in type_hints
    args_hints = type_hints["args"]
    assert get_origin(args_hints) is Union
    assert itk.ImageBase in get_args(args_hints)
Exemplo n.º 6
0
#!/usr/bin/env python3

import itk
import sys

input_filename = sys.argv[1]

image = itk.imread(input_filename)

# Use ITK's functional, Pythonic interface. The filter type is implied by the
# type of the input image. The filter is eagerly executed, and the output image
# is directly returned.
smoothed = itk.median_image_filter(image)

# Alternatively, create filter objects. These filter objects can be connected in
# a pipeline to stream-process large datasets. To generate the output of the
# pipeline, .Update() must explicitly be called on the last filter of the
# pipeline.
#
# We can implicitly instantiate the filter object based on the type
# of the input image in multiple ways.

# Use itk.ImageFileReader instead of the wrapping function,
# itk.imread to illustrate this example.
ImageType = itk.Image[itk.UC, 2]
reader = itk.ImageFileReader[ImageType].New(FileName=input_filename)
# Here we specify the filter input explicitly
median = itk.MedianImageFilter.New(reader.GetOutput())
# Same as above but shortened. Input does not have to be specified.
median = itk.MedianImageFilter.New(reader.GetOutput())
# Same as above. .GetOutput() does not have to be specified.
Exemplo n.º 7
0
#!/usr/bin/env python3

import itk
import sys

input_filename = sys.argv[1]
output_filename = sys.argv[2]

image = itk.imread(input_filename)

median = itk.median_image_filter(image, radius=2)

itk.imwrite(median, output_filename)
input_filename = argv[1]
output_filename = argv[2]
radius = int(argv[3])

reader = itk.ImageFileReader.IUC2.New(FileName=input_filename)

# test the deduction of the template parameter from the input
filt = itk.MedianImageFilter.New(reader, Radius=radius)
filt.Update()
filt_result = filt.GetOutput()
watcher = itk.XMLFilterWatcher(filt, "filter")

# test the update of the filter with the snake case function
# and the setting of parameter inside it
result_snake_case = itk.median_image_filter(reader, radius=radius)

# SetPrimaryInputName("ValidInput");
compare_filter = itk.ComparisonImageFilter.New(filt_result, TestInput=result_snake_case)
compare_filter.Update()
assert compare_filter.GetMaximumDifference() < 0.000000001

# Test that `__call__()` inside itkTemplate is deprecated. Replaced
# by snake_case functions
with warnings.catch_warnings(record=True) as w:
    # Cause all warnings to always be triggered.
    warnings.simplefilter("always")
    # Trigger a warning.
    result = itk.MedianImageFilter(reader, radius=radius)
    # Verify some things
    assert len(w) == 1