예제 #1
0
 def load_image_dir(self, dicom_dir):
     VTK_DATA_ROOT = vtkGetDataRoot()
     self.reader.SetDirectoryName(dicom_dir)
     # self.reader.SetFilePrefix(VTK_DATA_ROOT + "/Data/headsq/quarter") # UNIX FRIENDLY
     self.reader.SetFilePrefix(VTK_DATA_ROOT +
                               r"\Data\headsq\quarter")  # WINDOWS FRIENDLY
     self.reader.SetDataExtent(0, 63, 0, 63, 1, 93)
     self.reader.SetDataSpacing(3.2, 3.2, 1.5)
     self.reader.SetDataOrigin(0.0, 0.0, 0.0)
     self.reader.SetDataScalarTypeToUnsignedShort()
     self.reader.UpdateWholeExtent()
     self.reader.Update()
     self.current_image = self.reader.getOutput()
예제 #2
0
    def read_image(self, image_path=None, orientation="axial"):
        if image_path is None:
            # self.folder = "/Users/nandana/Downloads/image_ex"             #NANDANA PATH
            image_folder = r"O:\personal\wasil\supervise\dluximon\miu_viewer\image_ex"
        else:
            # dicom_seri = os.path.join(image_path, "dicom.seri")
            # if os.path.exists(dicom_seri):
            # QIA version of loading
            self.image_folder = image_path

        self.load_image_dir(image_folder)

        ### Temporary to try to change the voxel values
        image = self.reader.GetOutput()
        VTK_DATA_ROOT = vtkGetDataRoot()
        # self.reader2.SetDirectoryName(dicom_dir)
        # self.reader.SetFilePrefix(VTK_DATA_ROOT + "/Data/headsq/quarter") # UNIX FRIENDLY
        # self.reader2.SetFilePrefix(VTK_DATA_ROOT + r"\Data\headsq\quarter")  # WINDOWS FRIENDLY
        # self.reader2.SetDataExtent(0, 63, 0, 63, 1, 93)
        # self.reader2.SetDataSpacing(3.2, 3.2, 1.5)
        # self.reader2.SetDataOrigin(0.0, 0.0, 0.0)
        # self.reader2.SetDataScalarTypeToUnsignedShort()
        # self.reader2.UpdateWholeExtent()
        # self.reader2.Update()
        # image_temp = self.reader2.GetOutput()
        image2 = vtk.vtkImageData()
        # image2.CopyStructure(image)
        # image2.ShallowCopy(image_temp) # need this if i make from vtkimagedata
        image2.ShallowCopy(image)  # need this if i make from vtkimagedata
        image2.SetSpacing(.6, .6, 1)
        image2.SetExtent(0, 100, 0, 100, 0, 100)  #
        image2.SetOrigin(0, 0, 0)
        image2.SetDirectionMatrix(image.GetDirectionMatrix())  # hhmmmm
        extent = image2.GetExtent()

        imageData = vtk.vtkImageData()
        imageData.SetDimensions(100, 100, 100)
        if vtk.VTK_MAJOR_VERSION <= 5:
            imageData.SetNumberOfScalarComponents(1)
            imageData.SetScalarTypeToDouble()
        else:
            imageData.AllocateScalars(vtk.VTK_DOUBLE, 1)

        dims = imageData.GetDimensions()

        # Fill every entry of the image data with "2.0"
        for z in range(dims[2]):
            for y in range(dims[1]):
                for x in range(dims[0]):
                    if image.GetScalarComponentAsDouble(x, y, z, 0) > -100:
                        imageData.SetScalarComponentFromDouble(
                            x, y, z, 0, 1000)
                    else:
                        imageData.SetScalarComponentFromDouble(x, y, z, 0, 0)
        imageData.SetSpacing(.6, .6, 1)
        imageData.SetExtent(0, 100, 0, 100, 0, 100)  #
        imageData.SetOrigin(0, 0, 0)
        imageData.SetDirectionMatrix(image.GetDirectionMatrix())  # hhmmmm
        self.imageData = imageData

        self.img_reslice.SetInputData(0, self.imageData)
        self.img_reslice.SetOutputDimensionality(2)
        self.img_reslice.SetInterpolationModeToLinear()

        self.roi_reslice.SetInputData(0, self.reader.GetOutput())
        self.roi_reslice.SetOutputDimensionality(2)
        self.roi_reslice.SetInterpolationModeToLinear()

        self.window.Render()

        self.center = self.calculate_center()
        self.update_view(
            orientation=orientation)  #TODO: it is not update_view anymore
        if not self.image_loaded:
            self.interactor_style.AddObserver("MouseWheelForwardEvent",
                                              self.scroll_forward_callback)
            self.interactor_style.AddObserver("MouseWheelBackwardEvent",
                                              self.scroll_backward_callback)
            self.interactor_style.AddObserver("MouseMoveEvent",
                                              self.mouse_move_callback)
            self.interactor_style.AddObserver("KeyPressEvent",
                                              self.key_press_callback)
            self.interactor_style.AddObserver("LeftButtonPressEvent",
                                              self.left_press_callback)
            self.window.AddObserver("ModifiedEvent", self.window_mod_callback)
            self.image_loaded = True
예제 #3
0
def main():
    VTK_DATA_ROOT = vtkGetDataRoot()
    folder = "/Users/nandana/Downloads/image_ex"

    #read dicom files from specified directory
    reader = vtkDICOMImageReader()
    reader.SetDirectoryName(folder)
    reader.SetFilePrefix(VTK_DATA_ROOT + "/Data/headsq/quarter")

    reader.SetDataExtent(0, 63, 0, 63, 1, 93)
    reader.SetDataSpacing(3.2, 3.2, 1.5)
    reader.SetDataOrigin(-150.0, 150.0, 3.0)
    reader.SetDataScalarTypeToUnsignedShort()
    reader.UpdateWholeExtent()

    # Calculate the center of the volume
    reader.Update()

    (xMin, xMax, yMin, yMax, zMin,
     zMax) = reader.GetExecutive().GetWholeExtent(
         reader.GetOutputInformation(0))
    (xSpacing, ySpacing, zSpacing) = reader.GetOutput().GetSpacing()
    (x0, y0, z0) = reader.GetOutput().GetOrigin()

    center = [
        x0 + xSpacing * 0.5 * (xMin + xMax),
        y0 + ySpacing * 0.5 * (yMin + yMax),
        z0 + zSpacing * 0.5 * (zMin + zMax)
    ]
    yd = ((yMax - yMin) + 1) * ySpacing
    """
    # Matrices for axial, coronal, sagittal, oblique view orientations
    axial = vtkMatrix4x4()
    axial.DeepCopy((1, 0, 0, center[0],
                    0, 1, 0, center[1],
                    0, 0, 1, center[2],
                    0, 0, 0, 1))

    coronal = vtkMatrix4x4()
    coronal.DeepCopy((1, 0, 0, center[0],
                      0, 0, 1, center[1],
                      0,-1, 0, center[2],
                      0, 0, 0, 1))

    sagittal = vtkMatrix4x4
    sagittal.DeepCopy((0, 0,-1, center[0],
                       1, 0, 0, center[1],
                       0,-1, 0, center[2],
                       0, 0, 0, 1))

    oblique = vtkMatrix4x4()
    oblique.DeepCopy((1, 0, 0, center[0],
                      0, 0.866025, -0.5, center[1],
                      0, 0.5, 0.866025, center[2],
                      0, 0, 0, 1))
    
    reslice = vtkImageReslice()
    outputPort = reader.GetOutputPort()
    #reslice.SetInputConnection(reader.GetOutputPort())
    reslice.SetInputConnection(0, reader.GetOutputPort())
    print(reader.GetOutput().GetExtent())
    reslice.SetOutputExtent(reader.GetOutput().GetExtent())
    reslice.SetOutputDimensionality(2)
    reslice.SetResliceAxes(coronal)
    reslice.SetInterpolationModeToLinear()
    
    """
    # Visualize
    imageViewer = vtkResliceImageViewer()
    imageViewer.SetSliceOrientationToXY()
    #imageViewer.SetSlice(9)

    imageViewer.SetResliceModeToAxisAligned()
    imageViewer.SliceScrollOnMouseWheelOff()
    imageViewer.SetInputData(reader.GetOutput())

    #imageViewer.Render()
    camera = imageViewer.GetRenderer().GetActiveCamera()

    print(camera.GetOrientationWXYZ())

    # slice status message
    sliceTextProp = vtkTextProperty()
    sliceTextProp.SetFontFamilyToCourier()
    sliceTextProp.SetFontSize(20)
    sliceTextProp.SetVerticalJustificationToBottom()
    sliceTextProp.SetJustificationToLeft()
    sliceTextMapper = vtkTextMapper()
    msg = "Slice {} out of {}".format(imageViewer.GetSlice() + 1, \
                                     imageViewer.GetSliceMax() + 1)
    sliceTextMapper.SetInput(msg)
    sliceTextMapper.SetTextProperty(sliceTextProp)

    sliceTextActor = vtkActor2D()
    sliceTextActor.SetMapper(sliceTextMapper)
    sliceTextActor.SetPosition(100, 10)

    # coordinate display
    coordTextProp = vtkTextProperty()
    coordTextProp.SetFontFamilyToCourier()
    coordTextProp.SetFontSize(20)
    coordTextProp.SetVerticalJustificationToBottom()
    coordTextProp.SetJustificationToLeft()

    coordTextMapper = vtkTextMapper()
    coordTextMapper.SetInput("Pixel Coordinates: (--, --)")
    coordTextMapper.SetTextProperty(coordTextProp)

    coordTextActor = vtkActor2D()
    coordTextActor.SetMapper(coordTextMapper)
    coordTextActor.SetPosition(500, 10)

    worldCoordTextProp = vtkTextProperty()
    worldCoordTextProp.SetFontFamilyToCourier()
    worldCoordTextProp.SetFontSize(20)
    worldCoordTextProp.SetVerticalJustificationToBottom()
    worldCoordTextProp.SetJustificationToLeft()

    worldCoordTextMapper = vtkTextMapper()
    worldCoordTextMapper.SetInput("World Coordinates: (--, --)")
    worldCoordTextMapper.SetTextProperty(worldCoordTextProp)

    worldCoordTextActor = vtkActor2D()
    worldCoordTextActor.SetMapper(worldCoordTextMapper)
    worldCoordTextActor.SetPosition(500, 30)

    # usage hint message
    usageTextProp = vtkTextProperty()
    usageTextProp.SetFontFamilyToCourier()
    usageTextProp.SetFontSize(14)
    usageTextProp.SetVerticalJustificationToTop()
    usageTextProp.SetJustificationToLeft()

    usageTextMapper = vtkTextMapper()
    usageTextMapper.SetInput(
        "- Slice with mouse wheel\n- Zoom with pressed right\n  mouse button while dragging\n- Press i to toggle cursor line on/off"
    )
    usageTextMapper.SetTextProperty(usageTextProp)

    usageTextActor = vtkActor2D()
    usageTextActor.SetMapper(usageTextMapper)
    usageTextActor.GetPositionCoordinate(
    ).SetCoordinateSystemToNormalizedDisplay()
    usageTextActor.GetPositionCoordinate().SetValue(0.05, 0.95)

    actor = imageViewer.GetImageActor()
    #image = vtkImageActor()
    #actor.GetMapper().SetInputData(reader.GetOutput())

    image = imageViewer.GetInput()

    roiData = vtkImageData()
    roiImage = vtkImageActor()

    roiData.DeepCopy(image)
    extent = roiData.GetExtent()

    for i in range(extent[0], extent[1]):
        for j in range(extent[2], extent[3]):
            for k in range(extent[4], extent[5]):
                if image.GetScalarComponentAsDouble(i, j, k, 0) > -100:
                    roiData.SetScalarComponentFromDouble(i, j, k, 0, 1)
                    #roiData.SetScalarComponentFromDouble(0, i, j, k, 1)

                else:  #just in case
                    roiData.SetScalarComponentFromDouble(i, j, k, 0, 0.0)
                    #roiData.SetScalarComponentFromDouble(0, i, j, k, 0.0)
    """            
    for i in range(extent[0], extent[1]):
        for j in range(extent[2], extent[3]):
            #for k in range(extent[4], extent[5]):
            #k = 0
            roiData.SetScalarComponentFromDouble(i, j, k, 0, 0.0)
            #roiData.SetScalarComponentFromDouble(0, i, j, k, 0.0)
    """

    print(extent)

    table = vtkLookupTable()
    table.SetNumberOfTableValues(2)
    table.SetRange(0.0, 1.0)
    table.SetTableValue(0, 0.0, 0.0, 0.0, 0.0)
    table.SetTableValue(1, 0.0, 1.0, 0.0, 1.0)
    table.Build()

    mapToColor = vtkImageMapToColors()
    mapToColor.SetLookupTable(table)
    mapToColor.PassAlphaToOutputOn()

    mapToColor.SetInputData(roiData)

    #actor.GetMapper().SetInputConnection(mapToColor.GetOutputPort())
    roiImage.GetMapper().SetInputConnection(mapToColor.GetOutputPort())

    imageViewer.SetInputData(image)

    interactorStyle = vtkInteractorStyleImage()
    interactor = vtkRenderWindowInteractor()

    imageViewer.SetupInteractor(interactor)
    interactor.SetInteractorStyle(interactorStyle)

    # add slice status message and usage hint message to the renderer
    imageViewer.GetRenderer().AddActor2D(coordTextActor)
    imageViewer.GetRenderer().AddActor2D(sliceTextActor)
    imageViewer.GetRenderer().AddActor2D(usageTextActor)
    imageViewer.GetRenderer().AddActor2D(worldCoordTextActor)

    imageViewer.GetRenderer().AddActor(roiImage)
    #imageViewer.GetRenderer().AddViewProp(stack)

    # initialize rendering and interaction

    #imageViewer.SetSlice(35)

    imageViewer.GetRenderWindow().SetSize(1000, 1000)
    imageViewer.GetRenderer().SetBackground(0.2, 0.3, 0.4)

    imageViewer.GetWindowLevel().SetWindow(1000)
    imageViewer.GetWindowLevel().SetLevel(-1000)

    imageViewer.Render()

    yd = (yMax - yMin + 1) * ySpacing
    xd = (xMax - xMin + 1) * xSpacing

    d = camera.GetDistance()
    camera.SetParallelScale(0.5 * xd)
    camera.SetFocalPoint(center[0], center[1], 0)
    camera.SetPosition(center[0], center[1], +d)

    actions = {}
    actions["Dolly"] = -1
    actions["Cursor"] = 0

    def middlePressCallback(obj, event):
        # if middle + ctrl pressed, zoom in/out
        # otherwise slice through image (handled by mouseMoveCallback)

        if (interactor.GetControlKey()):
            actions["Dolly"] = 0
            interactorStyle.OnRightButtonDown()
        else:
            actions["Dolly"] = 1

    def middleReleaseCallback(obj, event):
        if (actions["Dolly"] == 0):
            interactorStyle.OnRightButtonUp()
        elif (actions["Dolly"] == 1):
            actions["Dolly"] = 0

    def mouseMoveCallback(obj, event):
        # if the middle button is pressed + mouse is moved, slice through image
        # otherwise, update world/pixel coords as mouse is moved

        if (actions["Dolly"] == 1):
            (lastX, lastY) = interactor.GetLastEventPosition()
            (curX, curY) = interactor.GetEventPosition()
            deltaY = curY - lastY

            if (deltaY > 0):
                imageViewer.IncrementSlice(1)
            elif (deltaY < 0):
                imageViewer.IncrementSlice(-1)

            msg = "Slice {} out of {}".format(imageViewer.GetSlice() + 1, \
                                     imageViewer.GetSliceMax() + 1)
            sliceTextMapper.SetInput(msg)
            imageViewer.Render()

        else:

            (mouseX, mouseY) = interactor.GetEventPosition()
            bounds = actor.GetMapper().GetInput().GetBounds()

            testCoord = vtkCoordinate()
            testCoord.SetCoordinateSystemToDisplay()
            testCoord.SetValue(mouseX, mouseY, 0)

            (posX, posY,
             posZ) = testCoord.GetComputedWorldValue(imageViewer.GetRenderer())

            inBounds = True
            if posX < bounds[0] or posX > bounds[1] or posY < bounds[
                    2] or posY > bounds[3]:
                inBounds = False

            if inBounds:
                wMousePos = "World Coordinates: (" + "{:.2f}".format(
                    posX) + ", " + "{:.2f}".format(
                        posY) + ", " + "{:.2f}".format(posZ) + ")"
                pMousePos = "Pixel Coordinates: (" + "{:.2f}".format(
                    mouseX) + ", " + "{:.2f}".format(mouseY) + ")"
                worldCoordTextMapper.SetInput(wMousePos)
                coordTextMapper.SetInput(pMousePos)

                imageViewer.Render()

            interactorStyle.OnMouseMove()

    def scrollForwardCallback(obj, event):
        # slice through image on scroll, update slice text

        imageViewer.IncrementSlice(1)

        msg = "Slice {} out of {}".format(imageViewer.GetSlice() + 1, \
                                     imageViewer.GetSliceMax() + 1)
        sliceTextMapper.SetInput(msg)
        imageViewer.Render()

    def scrollBackwardCallback(obj, event):
        imageViewer.IncrementSlice(-1)

        msg = "Slice {} out of {}".format(imageViewer.GetSlice() + 1, \
                                     imageViewer.GetSliceMax() + 1)
        sliceTextMapper.SetInput(msg)
        imageViewer.Render()

    def windowModifiedCallback(obj, event):
        # track render window width so coordinate text aligns itself
        # to the right side of the screen

        width = imageViewer.GetRenderWindow().GetSize()[0]
        coordTextActor.SetPosition(width - 550, 10)
        worldCoordTextActor.SetPosition(width - 550, 30)

        imageViewer.Render()

    def keyPressCallback(obj, event):
        # toggle cursor on/off when t key is pressed

        key = interactor.GetKeySym()
        if (key == "t"):
            if (actions["Cursor"] == 0):
                imageViewer.GetRenderWindow().HideCursor()
                actions["Cursor"] = 1
            elif (actions["Cursor"] == 1):
                imageViewer.GetRenderWindow().ShowCursor()
                actions["Cursor"] = 0

    interactorStyle.AddObserver("MiddleButtonPressEvent", middlePressCallback)
    interactorStyle.AddObserver("MiddleButtonReleaseEvent",
                                middleReleaseCallback)
    interactorStyle.AddObserver("MouseMoveEvent", mouseMoveCallback)
    interactorStyle.AddObserver("MouseWheelForwardEvent",
                                scrollForwardCallback)
    interactorStyle.AddObserver("MouseWheelBackwardEvent",
                                scrollBackwardCallback)
    interactorStyle.AddObserver("KeyPressEvent", keyPressCallback)
    imageViewer.GetRenderWindow().AddObserver("ModifiedEvent",
                                              windowModifiedCallback)

    interactor.Start()
예제 #4
0
import numpy as np
from vtkmodules.vtkIOImage import vtkDICOMImageReader
from vtkmodules.util.misc import vtkGetDataRoot
from vtkmodules.vtkCommonMath import vtkMatrix4x4
from vtkmodules.vtkCommonCore import vtkLookupTable
from vtkmodules.vtkImagingColor import vtkImageMapToWindowLevelColors
from vtkmodules.vtkImagingCore import vtkImageMapToColors, vtkImageReslice
from vtkmodules.vtkRenderingCore import vtkImageActor, vtkRenderer, \
    vtkRenderWindow, vtkRenderWindowInteractor, vtkCoordinate, vtkTextProperty, vtkTextMapper, vtkActor2D
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleImage
from vtkmodules.vtkCommonDataModel import vtkImageData
from math import floor, sqrt
from timeit import default_timer as timer

Z_SLICE = None
VTK_DATA_ROOT = vtkGetDataRoot()
folder = "/Users/nandana/Downloads/image_ex"

# Start by loading some data.
reader = vtkDICOMImageReader()
reader.SetDirectoryName(folder)
reader.SetFilePrefix(VTK_DATA_ROOT + "/Data/headsq/quarter")
reader.SetDataExtent(0, 63, 0, 63, 1, 93)
reader.SetDataSpacing(3.2, 3.2, 1.5)
reader.SetDataOrigin(0.0, 0.0, 0.0)
reader.SetDataScalarTypeToUnsignedShort()
reader.UpdateWholeExtent()

# Calculate the center of the volume
reader.Update()
(xMin, xMax, yMin, yMax, zMin,
# Tests paraview/paraview#18391

from vtkmodules.util.misc import vtkGetDataRoot, vtkGetTempDir
from vtk import vtkXMLGenericDataObjectReader, vtkDoubleArray, vtkDataSetWriter, vtkPDataSetReader

from os.path import join

reader = vtkXMLGenericDataObjectReader()
reader.SetFileName(join(vtkGetDataRoot(), "Data/multicomb_0.vts"))
reader.Update()


a1 = vtkDoubleArray()
a1.SetName("field-1")
a1.SetNumberOfTuples(1)
a1.SetValue(0, 100.0)
a1.GetRange()

a2 = vtkDoubleArray()
a2.SetName("field-2")
a2.SetNumberOfTuples(2)
a2.SetValue(0, 1.0)
a2.SetValue(1, 2.0)
a2.GetRange()

dobj = reader.GetOutputDataObject(0)
dobj.GetFieldData().AddArray(a1)
dobj.GetFieldData().AddArray(a2)

writer = vtkDataSetWriter()
writer.SetFileName(join(vtkGetTempDir(), "TestPDataSetReaderWriterWithFieldData.vtk"))
# Tests paraview/paraview#18391

from vtkmodules.util.misc import vtkGetDataRoot, vtkGetTempDir
from vtk import vtkXMLGenericDataObjectReader, vtkDoubleArray, vtkDataSetWriter, vtkPDataSetReader

from os.path import join

reader = vtkXMLGenericDataObjectReader()
reader.SetFileName(join(vtkGetDataRoot(), "Data/multicomb_0.vts"))
reader.Update()

a1 = vtkDoubleArray()
a1.SetName("field-1")
a1.SetNumberOfTuples(1)
a1.SetValue(0, 100.0)
a1.GetRange()

a2 = vtkDoubleArray()
a2.SetName("field-2")
a2.SetNumberOfTuples(2)
a2.SetValue(0, 1.0)
a2.SetValue(1, 2.0)
a2.GetRange()

dobj = reader.GetOutputDataObject(0)
dobj.GetFieldData().AddArray(a1)
dobj.GetFieldData().AddArray(a2)

writer = vtkDataSetWriter()
writer.SetFileName(
    join(vtkGetTempDir(), "TestPDataSetReaderWriterWithFieldData.vtk"))
예제 #7
0
    def __init__(self):
        VTK_DATA_ROOT = vtkGetDataRoot()
        self.reader = vtkDICOMImageReader()
        self.folder = "/Users/nandana/Downloads/image_ex"

        self.reader.SetDirectoryName(self.folder)
        self.reader.SetFilePrefix(VTK_DATA_ROOT + "/Data/headsq/quarter")
        self.reader.SetDataExtent(0, 63, 0, 63, 1, 93)
        self.reader.SetDataSpacing(3.2, 3.2, 1.5)
        self.reader.SetDataOrigin(0.0, 0.0, 0.0)
        self.reader.SetDataScalarTypeToUnsignedShort()
        self.reader.UpdateWholeExtent()
        self.reader.Update()

        self.center = self.calculate_center()

        self.axial = vtkMatrix4x4()
        self.axial.DeepCopy((1, 0, 0, self.center[0], 0, 1, 0, self.center[1],
                             0, 0, 1, self.center[2], 0, 0, 0, 1))

        self.coronal = vtkMatrix4x4()
        self.coronal.DeepCopy(
            (1, 0, 0, self.center[0], 0, 0, 1, self.center[1], 0, -1, 0,
             self.center[2], 0, 0, 0, 1))

        self.sagittal = vtkMatrix4x4()
        self.sagittal.DeepCopy(
            (0, 0, -1, self.center[0], 1, 0, 0, self.center[1], 0, -1, 0,
             self.center[2], 0, 0, 0, 1))

        self.oblique = vtkMatrix4x4()
        self.oblique.DeepCopy(
            (1, 0, 0, self.center[0], 0, 0.866025, -0.5, self.center[1], 0,
             0.5, 0.866025, self.center[2], 0, 0, 0, 1))

        self.img_reslice = vtkImageReslice()
        self.roi_reslice = vtkImageReslice()

        self.img_reslice.SetInputConnection(0, self.reader.GetOutputPort())
        self.img_reslice.SetOutputDimensionality(2)
        self.img_reslice.SetInterpolationModeToLinear()

        self.roi_reslice.SetInputConnection(0, self.reader.GetOutputPort())
        self.roi_reslice.SetOutputDimensionality(2)
        self.roi_reslice.SetInterpolationModeToLinear()

        self.set_orientation(self.axial)

        self.img_table = vtkLookupTable()
        self.roi_table = vtkLookupTable()
        self.window_level = vtkImageMapToWindowLevelColors()
        self.img_color = vtkImageMapToColors()
        self.roi_color = vtkImageMapToColors()

        self.img = self.map_img()
        self.roi = self.map_roi()

        self.px_coord_text_prop = vtkTextProperty()
        self.px_coord_text_mapper = vtkTextMapper()
        self.px_coord_text_actor = vtkActor2D()
        self.world_coord_text_prop = vtkTextProperty()
        self.world_coord_text_mapper = vtkTextMapper()
        self.world_coord_text_actor = vtkActor2D()
        self.usage_text_prop = vtkTextProperty()
        self.usage_text_mapper = vtkTextMapper()
        self.usage_text_actor = vtkActor2D()

        self.renderer = vtkRenderer()
        self.add_text()
        self.renderer.AddActor(self.img)
        self.renderer.AddActor(self.roi)

        self.renderer.SetBackground(0.2, 0.3, 0.4)

        self.window = vtkRenderWindow()
        self.window.AddRenderer(self.renderer)

        self.window.SetSize(1000, 1000)

        self.interactor_style = vtkInteractorStyleImage()
        self.interactor_style.SetInteractionModeToImageSlicing()

        self.interactor = vtkRenderWindowInteractor()
        self.interactor.SetInteractorStyle(self.interactor_style)
        self.window.SetInteractor(self.interactor)

        self.window_level.SetWindow(1000)
        self.window_level.SetLevel(200)
        self.window_level.Update()

        self.window.Render()

        self.interactor_style.AddObserver("MouseWheelForwardEvent",
                                          self.scroll_forward_callback)
        self.interactor_style.AddObserver("MouseWheelBackwardEvent",
                                          self.scroll_backward_callback)
        self.interactor_style.AddObserver("MouseMoveEvent",
                                          self.mouse_move_callback)
        self.interactor_style.AddObserver("KeyPressEvent",
                                          self.key_press_callback)
        self.interactor_style.AddObserver("LeftButtonPressEvent",
                                          self.left_press_callback)
        self.window.AddObserver("ModifiedEvent", self.window_mod_callback)

        self.actions = {
            "Slicing": 0,
            "Cursor": 0,
            "CurrentPos": -1,
            "LastPos": -1,
            "DoubleClick": 0
        }
예제 #8
0
    def load_image(self, file):
        # (1) Read File
        # (2) Get min, max, window-level
        # (3) Update views 
        
        # (1) 
        
        if self.demo_mode:
            print(file)
            print("......")
            self.reader = vtkDICOMImageReader()
            VTK_DATA_ROOT = vtkGetDataRoot()
            self.reader.SetDirectoryName(file)
            self.reader.SetFilePrefix(VTK_DATA_ROOT + "/Data/headsq/quarter") # UNIX FRIENDLY
            # self.reader.SetFilePrefix(VTK_DATA_ROOT + r"\Data\headsq\quarter")  # WINDOWS FRIENDLY
            
            ### Extract 
            # TODO: double check that the image attributes are correct when using vtkDICOMimageReader
            
            
            # self.reader.SetDataExtent(0, 63, 0, 63, 1, 93)
            # self.reader.SetDataSpacing(3.2, 3.2, 1.5)
            # self.reader.SetDataOrigin(0.0, 0.0, 0.0)
            self.reader.SetDataScalarTypeToUnsignedShort()
            # self.reader.UpdateWholeExtent()
            self.reader.Update()
            imageData = self.reader.GetOutput()
            self.image = imageData
            image_info = dict(image_data=imageData, spacing=imageData.GetSpacing(), origin=imageData.GetOrigin())
            minv, maxv = (-1024, 1024)  #close enough estimate
        else:
            if qia_loaded:
                print(file)
                self.image = qimage.read(file)

                ### QIA version
                image_info = dict(image_data=self.image, spacing=self.image.get_spacing(), origin=self.image.get_origin())
                dims = image_info["image_data"].get_size()
            else:   #assume numpy array
                print("Not implemented yet")
                # load numpy array
            
                ### numpy version >>> for future compatibility 
                # image_npy = np.array(self.image.get_array())
                # image_npy = np.moveaxis(image_npy, 0, 2) #because I saved the numpy files (z, x, y) -_-
                # image_npy = np.rot90(image_npy, 3)
                # image_npy = np.flip(image_npy, 1)
                # image_info = dict(image_data=image_npy, spacing=self.image.get_spacing(), origin=self.image.get_origin())
                # dims = image_info["image_data"].shape


            imageData = vtk.vtkImageData()
            
            print("dims", dims)
            #instantiating imageData obj
            imageData.SetDimensions(dims[0], dims[1], dims[2])
            if vtk.VTK_MAJOR_VERSION <= 5:
                imageData.SetNumberOfScalarComponents(1)
                imageData.SetScalarTypeToDouble()
            else:
                imageData.AllocateScalars(vtk.VTK_DOUBLE, 1)

            # (2) Update the vtk obj image information
            imageData.SetSpacing(image_info["spacing"][0], image_info["spacing"][1], image_info["spacing"][2])
            
            ### QIA Version 
            imageData.SetExtent(0, dims[0]-1, 0, dims[1]-1, 1, dims[2]) # with QIA it goes from [ 1 to z_max ] instead of [0 to z_max - 1]
            imageData.SetOrigin(image_info["origin"][0], image_info["origin"][1], image_info["origin"][2]) #unsure if it's this one
            # imageData.SetOrigin(image_info["origin"][0], image_info["origin"][1], 0) #unsure if it's this one

            for z in range(dims[2]):
                z+=1
                for y in range(dims[1]):
                    for x in range(dims[0]):
                        # print(x,y, z)
                        # double check if x,y,z is the appropriate order after you get the numpy array
                        # imageData.SetScalarComponentFromDouble(x, y, z, 0, image_info["image_data"][x,y,z] )  # numpy
                        imageData.SetScalarComponentFromDouble(x, y, z, 0, image_info["image_data"].get_value((x,y,z)) ) # QIA
            minv, maxv = self.image.get_min_max()
        # (2)
        self.window_level.SetWindow(maxv-minv)
        self.window_level.SetLevel((maxv+minv)/2)
        self.window_level.Update()
        
        
        # (3) 
        # TODO: Make blockSignals useful (to avoid reloading things if we click too often, etc.)
        for v in self.views:  
            v.blockSignals(True)    
        self.viewer_center = self.views[0].calculate_center(extent=imageData.GetExtent(), spacing=image_info["spacing"], origin=image_info["origin"])
        print("viewer center", self.viewer_center)
        for v in self.views:
            v.update_image(imageData)
            if not v.viewer_initialized:
                # orientation = "axial"
                orientation = "sagittal"
                #   before this, orientation has of the viewer has not been defined yet.
                #   aka it doesn't set the viewer orientation until the first image has loaded
                v.init_view(orientation=orientation, center=self.viewer_center)
            else:
                # if viewer has been initialized already, then just update the center
                v.set_position(self.viewer_center)
            v.connect_window_level(self.window_level)   #updating window level
            # v.resetCamera()   ### TODO: Implement and activate
        for v in self.views:
            v.blockSignals(False) 
        self.render()