How to render (visualize) histogram of 3D image

Hey,

I try to visualize my histogram in order to check peaks in my image (and later on to segment those pixels). However, I try to render it with ren.AddActor(plot), but it only shows a black screen (I think here is the issue). My code:

import vtk
import matplotlib.pyplot as plt

    def main():
        # Read dataset
        reader = vtk.vtkDICOMImageReader()
        reader.SetDirectoryName(dataset)
        reader.Update()

        # Create vtkPlotactor
        plot = vtk.vtkXYPlotActor()
        plot.SetXValuesToValue()

        # Histogram
        histogram = vtk.vtkImageHistogram()
        histogram.SetInputConnection(reader.GetOutputPort())
        histogram.GenerateHistogramImageOn()
        histogram.AutomaticBinningOn()
        histogram.Update()

        plot.AddDataSetInputConnection(histogram.GetOutputPort())

        # Visualize Histogram
        ren = vtk.vtkRenderer()
        ren.AddActor(plot)

        renWin = vtk.vtkRenderWindow()
        renWin.AddRenderer(ren)
        renWin.SetSize(600, 600)

        iren = vtk.vtkRenderWindowInteractor()
        iren.SetRenderWindow(renWin)

        # initialze event loop
        iren.Initialize()
        iren.Start()

    if __name__ == '__main__':
        main()

Thanks in advance :slight_smile:

You can use VTK plotting infrastructure to show plots. See examples here. They offer some interactivity, too.

Segmentation based on a histogram is a very basic feature (not in the sense that it is easy to implement, but you won’t impress anyone by having it), which is already available in existing applications, so redevelop it again from scratch may not be the best use of your time. As always, I would recommend to customize existing implementations in ParaView, 3D Slicer, or other VTK-based open-source applications.

Just an example, how this is implemented in 3D Slicer in Python (source code: simple global thresholding tool, variant that does local thresholding and connected component extraction) - all freely avaialable, without any restrictions, ready for customization and extension: