[vtkusers] stretching pixels in one dimension

Michael Hansen michaelsvenna at gmail.com
Mon Nov 21 10:24:38 EST 2016


Hi Sankhesh,

Thank you for your comment. Can you tell me how I can adjust the spacing of
the image data along the z-axis?
Below is my code:

from PyQt5.QtWidgets import (QMainWindow,
                             QFrame, QVBoxLayout, QApplication)
from PyQt5 import QtCore
from QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import random
import vtk
import numpy as np
import sys
import h5py
import skimage
import matplotlib.pyplot as plt


def misc2stdimage(img):
    '''
    Transform arbitrarily scaled image into
    something scikit image understands,
    ie. a ubyte numpy array
    '''
    # rescale to [x E -1.0;1.0], xER
    img_rescaled = (((img - img.min()) / (img.max() - img.min()))*2)-1
    return skimage.img_as_ubyte(img_rescaled)

class MyEventFilter(QtCore.QObject):
    def __init__(self, vtkwindow):
        self.vtkwindow = vtkwindow
        QtCore.QObject.__init__(self)

    def eventFilter(self, receiver, event):
        if(event.type() == QtCore.QEvent.KeyPress):
            if event.text() == 'x':
                self.vtkwindow.slideX(1)
            elif event.text() == 'X':
                self.vtkwindow.slideX(-1)
            elif event.text() == 'y':
                self.vtkwindow.slideY(1)
            elif event.text() == 'Y':
                self.vtkwindow.slideY(-1)
            elif event.text() == 'z':
                self.vtkwindow.slideZ(1)
            elif event.text() == 'Z':
                self.vtkwindow.slideZ(-1)
        return super(MyEventFilter,self).eventFilter(receiver, event)

def MakeLUTFromCTF(tableSize):
    '''
    Use a color transfer Function to generate the colors in the lookup
table.
    See:
http://www.vtk.org/doc/nightly/html/classvtkColorTransferFunction.html
    :param: tableSize - The table size
    :return: The lookup table.
    '''
    ctf = vtk.vtkColorTransferFunction()
    ctf.SetColorSpaceToDiverging()
    # Green to tan.
    ctf.AddRGBPoint(0.0, 0.085, 0.532, 0.201)
    ctf.AddRGBPoint(0.5, 0.865, 0.865, 0.865)
    ctf.AddRGBPoint(1.0, 0.677, 0.492, 0.093)

    lut = vtk.vtkLookupTable()
    lut.SetNumberOfTableValues(tableSize)
    lut.Build()

    for i in range(0,tableSize):
        rgb = list(ctf.GetColor(float(i)/tableSize))+[1]
        lut.SetTableValue(i,rgb)

    return lut

class MainWindow(QMainWindow):

    def slideX(self, x):
        self.planeWidgetX.SetSliceIndex(
            self.planeWidgetX.GetSliceIndex() + x
        )
        print( self.planeWidgetX.GetSliceIndex() )
        self.ren.ResetCameraClippingRange()
        self.vtkWidget.GetRenderWindow().Render()

    def slideY(self, y):
        self.planeWidgetY.SetSliceIndex(
            self.planeWidgetY.GetSliceIndex() + y
        )
        self.ren.ResetCameraClippingRange()
        self.vtkWidget.GetRenderWindow().Render()

    def slideZ(self, z):
        self.planeWidgetZ.SetSliceIndex(
            self.planeWidgetZ.GetSliceIndex() + z
        )
        self.ren.ResetCameraClippingRange()
        self.vtkWidget.GetRenderWindow().Render()

    def __init__(self, parent = None):
        QMainWindow.__init__(self, parent)

        self.frame = QFrame()

        self.vl = QVBoxLayout()
        self.vtkWidget = QVTKRenderWindowInteractor(self.frame)

        self.vl.addWidget(self.vtkWidget)

        self.ren = vtk.vtkRenderer()
        self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
        self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()

        F = h5py.File('../realdata.h5','r')
        volume = np.dstack(
            (
                F['02']['03']['db'],
                F['02']['04']['db'],
                F['02']['05']['db'],
                F['02']['06']['db'],
                F['02']['07']['db'],
                F['02']['08']['db']
            )
        )
        data_matrix = misc2stdimage(volume)
        F.close()
        m, n, p = data_matrix.shape

        print(data_matrix.shape, data_matrix.dtype,
              np.min(data_matrix), np.max(data_matrix)
        )

        v16 = vtk.vtkImageImport()
        v16.CopyImportVoidPointer(data_matrix, data_matrix.nbytes)
        v16.SetDataScalarTypeToUnsignedChar()
        v16.SetNumberOfScalarComponents(1)
        v16.SetDataExtent(0, p-1, 0, n-1, 0, m-1)
        v16.SetWholeExtent(0, p-1, 0, n-1, 0, m-1)

        xMin, xMax, yMin, yMax, zMin, zMax = v16.GetExecutive().\
                                             GetWholeExtent(
                                                 v16.GetOutputInformation(0)
                                             )

        spacing = v16.GetOutput().GetSpacing()
        sx, sy, sz = spacing

        origin = v16.GetOutput().GetOrigin()
        ox, oy, oz = origin

        # An outline is shown for context.
        outline = vtk.vtkOutlineFilter()
        outline.SetInputConnection(v16.GetOutputPort())

        outlineMapper = vtk.vtkPolyDataMapper()
        outlineMapper.SetInputConnection(outline.GetOutputPort())

        outlineActor = vtk.vtkActor()
        outlineActor.SetMapper(outlineMapper)

        # The shared picker enables us to use 3 planes at one time
        # and gets the picking order right
        picker = vtk.vtkCellPicker()
        picker.SetTolerance(0.005)

        norm = plt.Normalize()
        colors = plt.cm.jet(norm(range(0, 255)))
        colorFunc = vtk.vtkColorTransferFunction()
        [colorFunc.AddRGBPoint(i, colors[i,0], colors[i,1], colors[i,2])
         for i in range(0,255)]


        # The 3 image plane widgets are used to probe the dataset.
        self.planeWidgetX = vtk.vtkImagePlaneWidget()
        self.planeWidgetX.DisplayTextOn()
        self.planeWidgetX.SetInputConnection(v16.GetOutputPort())
        self.planeWidgetX.SetPlaneOrientationToXAxes()
        self.planeWidgetX.SetSliceIndex(32)
        self.planeWidgetX.SetPicker(picker)
        self.planeWidgetX.SetKeyPressActivationValue("x")
        self.planeWidgetX.SetLookupTable(MakeLUTFromCTF(200))

        prop1 = self.planeWidgetX.GetPlaneProperty()
        prop1.SetColor(1, 0, 0)

        self.planeWidgetY = vtk.vtkImagePlaneWidget()
        self.planeWidgetY.DisplayTextOn()
        self.planeWidgetY.SetInputConnection(v16.GetOutputPort())
        self.planeWidgetY.SetPlaneOrientationToYAxes()
        self.planeWidgetY.SetSliceIndex(32)
        self.planeWidgetY.SetPicker(picker)
        self.planeWidgetY.SetKeyPressActivationValue("y")
        self.planeWidgetY.SetLookupTable(MakeLUTFromCTF(200))
        prop2 = self.planeWidgetY.GetPlaneProperty()
        prop2.SetColor(1, 1, 0)
        self.planeWidgetY.SetLookupTable(self.planeWidgetX.GetLookupTable())

        self.planeWidgetZ = vtk.vtkImagePlaneWidget()
        self.planeWidgetZ.DisplayTextOn()
        self.planeWidgetZ.SetInputConnection(v16.GetOutputPort())
        self.planeWidgetZ.SetPlaneOrientationToZAxes()
        self.planeWidgetZ.SetSliceIndex(46)
        self.planeWidgetZ.SetPicker(picker)
        self.planeWidgetZ.SetKeyPressActivationValue("z")
        self.planeWidgetZ.SetLookupTable(MakeLUTFromCTF(200))
        prop3 = self.planeWidgetZ.GetPlaneProperty()
        prop3.SetColor(0, 0, 1)
        self.planeWidgetZ.SetLookupTable(self.planeWidgetX.GetLookupTable())

        self.ren.AddActor(outlineActor)

        self.planeWidgetX.SetInteractor(self.iren)
        self.planeWidgetX.On()
        self.planeWidgetY.SetInteractor(self.iren)
        self.planeWidgetY.On()
        self.planeWidgetZ.SetInteractor(self.iren)
        self.planeWidgetZ.On()

        self.ren.ResetCamera()
        self.ren.ResetCameraClippingRange()
        self.ren.SetViewport(0,0,1,1)
        self.vtkWidget.GetRenderWindow().SetSize(400, 400)

        self.frame.setLayout(self.vl)
        self.setCentralWidget(self.frame)

        self.show()
        self.iren.Initialize()

if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = MainWindow()
    myFilter = MyEventFilter(window)
    app.installEventFilter(myFilter)
    window.ren.ResetCamera()

    sys.exit(app.exec_())


On Mon, Nov 21, 2016 at 3:00 PM, Sankhesh Jhaveri <
sankhesh.jhaveri at kitware.com> wrote:

> Hi Michael,
>
> Depending on your use case, you could either use the vtkCamera API (or an
> appropriate interactor style) to zoom in on a section of the data or modify
> the spacing of the image data along the Z axis.
>
> Hope that helps.
> Sankhesh
>>
> On Mon, Nov 21, 2016 at 4:57 AM Michael Hansen <michaelsvenna at gmail.com>
> wrote:
>
>> Hello. I have volumes which are of size 800x30000x6
>>
>> I am visualizing this using 3 vtkImagePlaneWidget.
>>
>> This is working ok, however when i am viewing the data in the dimension
>> which only has columns its pretty much impossible to see the content of the
>> 6 pixels....
>> My question is... is there any way to stretch/zoom/scale the pixels in
>> this dimension such that it becomes more visible to the viewer?
>>
>> Thanks
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at http://www.kitware.com/
>> opensource/opensource.html
>>
>> Please keep messages on-topic and check the VTK FAQ at:
>> http://www.vtk.org/Wiki/VTK_FAQ
>>
>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/vtkusers
>>
> --
> Sankhesh Jhaveri *Sr. Research & Development Engineer* | Kitware
> <http://www.kitware.com/> | (518) 881-4417
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20161121/73ba0e23/attachment.html>


More information about the vtkusers mailing list