Skip to content

ImageAccumulateGreyscale

vtk-examples/Cxx/Images/ImageAccumulateGreyscale


Question

If you have a question about this example, please use the VTK Discourse Forum

Code

ImageAccumulateGreyscale.cxx

#include <vtkActor.h>
#include <vtkBarChartActor.h>
#include <vtkFieldData.h>
#include <vtkImageAccumulate.h>
#include <vtkImageData.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageMagnitude.h>
#include <vtkImageReader2.h>
#include <vtkImageReader2Factory.h>
#include <vtkIntArray.h>
#include <vtkLegendBoxActor.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>

int main(int argc, char* argv[])
{
  // Handle the arguments
  if (argc < 2)
  {
    std::cout << "Required arguments: filename e.g. Ox.jpg" << std::endl;
    return EXIT_FAILURE;
  }

  vtkNew<vtkImageReader2Factory> readerFactory;
  vtkSmartPointer<vtkImageReader2> reader;
  reader.TakeReference(readerFactory->CreateImageReader2(argv[1]));
  reader->SetFileName(argv[1]);

  vtkNew<vtkImageMagnitude> magnitude;
  magnitude->SetInputConnection(reader->GetOutputPort());

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkIntArray> frequencies;

  vtkNew<vtkImageAccumulate> histogram;
  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();

  int numberOfTuples = 64;
  frequencies->SetNumberOfComponents(1);
  frequencies->SetNumberOfTuples(numberOfTuples);
  vtkIdType* output =
      static_cast<vtkIdType*>(histogram->GetOutput()->GetScalarPointer());

  for (int j = 0; j < numberOfTuples; ++j)
  {
    frequencies->SetTuple1(j, *output++);
  }

  vtkNew<vtkDataObject> dataObject;

  dataObject->GetFieldData()->AddArray(frequencies);

  // Create a vtkBarChartActor
  vtkNew<vtkBarChartActor> barChart;

  barChart->SetInput(dataObject);
  barChart->SetTitle("Histogram");
  barChart->GetPositionCoordinate()->SetValue(0.1, 0.05, 0.0);
  barChart->GetPosition2Coordinate()->SetValue(0.95, 0.85, 0.0);
  barChart->GetProperty()->SetColor(colors->GetColor3d("Banana").GetData());
  barChart->GetLegendActor()->SetNumberOfEntries(
      dataObject->GetFieldData()->GetArray(0)->GetNumberOfTuples());
  barChart->LegendVisibilityOff();
  barChart->LabelVisibilityOff();

  int count = 0;
  for (int i = 0; i < numberOfTuples; ++i)
  {
    barChart->SetBarColor(count++, colors->GetColor3d("Tomato").GetData());
  }

  // Visualize the histogram
  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(barChart);
  renderer->SetBackground(colors->GetColor3d("Peacock").GetData());
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetSize(640, 480);
  renderWindow->SetWindowName("ImageAccumulateGreyscale");

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  // Initialize the event loop and then start it
  renderWindow->Render();
  interactor->Initialize();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ImageAccumulateGreyscale)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkIOImage
  vtkImagingCore
  vtkImagingMath
  vtkImagingStatistics
  vtkInteractionStyle
  vtkRenderingAnnotation
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping ImageAccumulateGreyscale: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(ImageAccumulateGreyscale MACOSX_BUNDLE ImageAccumulateGreyscale.cxx )
  target_link_libraries(ImageAccumulateGreyscale PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ImageAccumulateGreyscale MACOSX_BUNDLE ImageAccumulateGreyscale.cxx )
  target_link_libraries(ImageAccumulateGreyscale PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ImageAccumulateGreyscale
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build ImageAccumulateGreyscale

Click here to download ImageAccumulateGreyscale and its CMakeLists.txt file. Once the tarball ImageAccumulateGreyscale.tar has been downloaded and extracted,

cd ImageAccumulateGreyscale/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./ImageAccumulateGreyscale

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.