[vtkusers] plotting bar chart with vtkBarChartActor and vtkXYPlotActor

David Doria daviddoria+vtk at gmail.com
Tue Jan 5 09:52:53 EST 2010


On Mon, Jan 4, 2010 at 7:15 AM, michiel mentink
<michael.mentink at st-hughs.ox.ac.uk> wrote:
> Dear All,
>
> I'd like to plot a histogram of an image volume, file type metaImage.
>
> I've managed to plot a histogram using the xyPlotActor, yet I have no way of
> verifying if it is alright.
> It also produces a segmentation fault.
>
> I can't get the same histogram plotted using the BarChartActor.
>
> Could anybody verify my code of both plots? Sorry, I'm a beginner, and at
> this point I'm stuck.
>
> Kind regards,
>
> Michael
>
> #include <vtkImageData.h>
> #include <vtkMetaImageReader.h>
> #include <vtkImageAccumulate.h>
> #include <vtkSmartPointer.h>
> #include <vtkBarChartActor.h>
> #include "vtkRenderer.h"
> #include "vtkRenderWindow.h"
> #include "vtkRenderWindowInteractor.h"
> #include "vtkPolyDataMapper.h"
> #include "vtkActor.h"
> #include "vtkContourFilter.h"
> #include "vtkImageAccumulate.h"
> #include "vtkXYPlotActor.h"
> #include "vtkImageThreshold.h"
>
> int main(int argc, char *argv[])
> {
> // handle the arguments
> if(argc != 2)
>     {
>     vtkstd::cout << "Required arguments: filename.mhd" << vtkstd::endl;
>     }
>
> // open a metaimage
> vtkMetaImageReader * metaImageReader = vtkMetaImageReader::New();
> metaImageReader->SetFileName(argv[1]);
>
> //metaImageReader->Update();
> vtkSmartPointer<vtkImageAccumulate> histogram =
> vtkSmartPointer<vtkImageAccumulate>::New();
>
> histogram->SetInput(metaImageReader->GetOutput());
> // sets number of bins: 10 = N# bins
>   histogram->SetComponentExtent(0, 4000, 0, 0, 0, 0);
>   histogram->SetComponentOrigin(0, 0, 0);
>   histogram->SetComponentSpacing(0.16, 0.16, 3.26); // component spacing:
> depends on spacing of input volume
>   histogram->UpdateWholeExtent();
>
>     vtkXYPlotActor* plot = vtkXYPlotActor::New();
>     plot->ExchangeAxesOff();
>     plot->SetLabelFormat("%g");
>
>  //   plot->SetXRange(0, 320);
>  //   plot->SetYRange(0, 10000);
>     plot->SetXTitle("Iso Surface Level");
>     plot->SetYTitle("Frequency");
>     plot->AddInput(histogram->GetOutput());
>     plot->SetXValuesToValue();
>
>     vtkBarChartActor* plot2 = vtkBarChartActor::New();
>     plot2->SetInput(histogram->GetOutput());
>
>   vtkRenderer *aRenderer = vtkRenderer::New();
>     vtkRenderWindow *renWin = vtkRenderWindow::New();
>     renWin->AddRenderer(aRenderer);
>     vtkRenderWindowInteractor *iren =
>     vtkRenderWindowInteractor::New();
>     iren->SetRenderWindow(renWin);
>
>   aRenderer->AddActor(plot);
>
>   renWin->SetSize(640, 480);
>
>   // Initialize the event loop and then start it.
>   iren->Initialize();
>   iren->Start();
>
>    vtkRenderer *aRenderer2 = vtkRenderer::New();
>     vtkRenderWindow *renWin2 = vtkRenderWindow::New();
>     renWin2->AddRenderer(aRenderer2);
>     vtkRenderWindowInteractor *iren2 =
>     vtkRenderWindowInteractor::New();
>     iren2->SetRenderWindow(renWin2);
>
>   aRenderer2->AddActor(plot2);
>
>   renWin2->SetSize(640, 480);
>
>   // Initialize the event loop and then start it.
>   iren2->Initialize();
>   iren2->Start();
>
>   histogram->Delete();
>   plot->Delete();
>   plot2->Delete();
>   iren->Delete();
>   renWin->Delete();
>   aRenderer->Delete();
>
> return (0);
> }
>
>
>

I reorganized the code a little bit, changed the input to a jpeg, and
posted an example in the "broken" section:
http://www.cmake.org/Wiki/VTK/Examples/Histogram

Maybe this will double your chances of someone seeing it and figuring
out what is wrong :)

Thanks,

David



More information about the vtkusers mailing list