VTK/Examples/Cxx/Images/ImageValueRange

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Revision as of 23:03, 8 December 2012 by Lorensen (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

ImageValueRange.cxx

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkDoubleArray.h>
#include <vtkImageData.h>
#include <vtkPointData.h>
 
int main(int, char *[])
{
  // Create an image data
  vtkSmartPointer<vtkImageData> imageData = vtkSmartPointer<vtkImageData>::New();
 
  // Specify the size of the image data
  imageData->SetDimensions(5,1,1);
#if VTK_MAJOR_VERSION <= 5
  imageData->SetNumberOfScalarComponents(1);
  imageData->SetScalarTypeToDouble();
  imageData->AllocateScalars();
#else
  imageData->AllocateScalars(VTK_DOUBLE,1);
#endif 
  int* dims = imageData->GetDimensions();
 
  for (int x = 0; x < dims[0]; x++)
    {
    double* pixel = static_cast<double*>(imageData->GetScalarPointer(x,0,0));
    pixel[0] = x*10;
    }
 
  double valuesRange[2];
  vtkDoubleArray::SafeDownCast(imageData->GetPointData()->GetArray("ImageScalars"))->GetValueRange(valuesRange);
  std::cout << "valuesRange = " << valuesRange[0] << " " << valuesRange[1] << std::endl;
 
  // This should also work: 
  // min = vtkImageData->GetOutput()->GetScalarRange()[0] and max = GetScalarRange()[1].
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
PROJECT(ImageValueRange)
 
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
 
add_executable(ImageValueRange MACOSX_BUNDLE ImageValueRange.cxx)
 
if(VTK_LIBRARIES)
  target_link_libraries(ImageValueRange ${VTK_LIBRARIES})
else()
  target_link_libraries(ImageValueRange vtkHybrid vtkWidgets)
endif()

Download and Build ImageValueRange

Click here to download ImageValueRange. and its CMakeLists.txt file.

Once the tarball ImageValueRange.tar has been downloaded and extracted,

cd ImageValueRange/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:

./ImageValueRange

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