[vtkusers] Python histogram

Martin Dulovits martin.dulovits at woogieworks.at
Fri Nov 9 14:16:59 EST 2012


I converted the sourcecode of the grayscale histogram to python, but I 
do not understand the section around
output = o.GetScalarPointer()
What is the python equivalent of this construct ??


from vtk.vtkCommonPython import vtkIntArray
from vtk.vtkFilteringPython import vtkDataObject
from vtk.vtkHybridPython import vtkBarChartActor
import vtk.vtkIOPython
import vtk
from vtk.vtkImagingPython import vtkImageMagnitude, vtkImageAccumulate
from vtk.vtkRenderingPython import vtkRenderer, vtkRenderWindow, 
vtkRenderWindowInteractor

# Read a jpeg image
reader = vtk.vtkJPEGReader()
reader.SetFileName( 't:\organ58_dilated0.5.3b.jpg' )
reader.Update()

magnitude = vtkImageMagnitude()
magnitude.SetInput(reader.GetOutput())
magnitude.Update()

red = [1, 0, 0 ]

frequencies = vtkIntArray()

histogram = vtkImageAccumulate()
histogram.SetInputConnection(magnitude.GetOutputPort())
histogram.SetComponentExtent(0,255,0,0,0,0)
histogram.SetComponentOrigin(0,0,0)
histogram.SetComponentSpacing(1,0,0)
histogram.IgnoreZeroOn()
histogram.Update()

frequencies.SetNumberOfComponents(1)
frequencies.SetNumberOfTuples(256)
o = histogram.GetOutput()
output = o.GetScalarPointer()


for j in range ( 0 , 256):
     frequencies.SetTuple1(j, float(output[j]))


dataObject = vtkDataObject()

dataObject.GetFieldData().AddArray( frequencies )

# Create a vtkBarChartActor
barChart = vtkBarChartActor()

barChart.SetInput(dataObject)
barChart.SetTitle("Histogram")
barChart.GetPositionCoordinate().SetValue(0.05,0.05,0.0)
barChart.GetPosition2Coordinate().SetValue(0.95,0.85,0.0)
barChart.GetProperty().SetColor(1,1,1)

barChart.GetLegendActor().SetNumberOfEntries(dataObject.GetFieldData().GetArray(0).GetNumberOfTuples())
barChart.LegendVisibilityOff()
barChart.LabelVisibilityOff()

count = 0
for  i in range ( 0, 256):
     barChart.SetBarColor( count, red )
     count +=1


# Visualize the histogram
renderer = vtkRenderer ()
renderer.AddActor(barChart)

renderWindow = vtkRenderWindow ()
renderWindow.AddRenderer(renderer)
renderWindow.SetSize(640, 480)

interactor = vtkRenderWindowInteractor()
interactor.SetRenderWindow(renderWindow)

# Initialize the event loop and then start it
interactor.Initialize()
interactor.Start()




More information about the vtkusers mailing list