ImageAccumulate
vtk-examples/Cxx/Images/ImageAccumulate
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ImageAccumulate.cxx
/*
* In this example, the resulting histogram has 6 pixels (all of them) in the
* 3rd bin (value = 2) and zero in the rest of the bins
*/
#include <vtkImageAccumulate.h>
#include <vtkImageData.h>
#include <vtkNew.h>
#include <vtkSmartPointer.h>
namespace {
void CreateImage(vtkSmartPointer<vtkImageData> image);
}
int main(int, char*[])
{
vtkNew<vtkImageData> image;
CreateImage(image);
vtkNew<vtkImageAccumulate> imageAccumulate;
imageAccumulate->SetInputData(image);
imageAccumulate->SetComponentExtent(0, 255, 0, 0, 0, 0);
imageAccumulate->SetComponentOrigin(0, 0, 0);
imageAccumulate->SetComponentSpacing(
1, 0, 0); // This will count exactly the number of pixels of each color.
// Use (10,0,0) to make bins of width 10 instead.
imageAccumulate->Update();
int dims[3];
imageAccumulate->GetOutput()->GetDimensions(dims);
for (vtkIdType bin = 0; bin < dims[0]; ++bin)
{
std::cout << *(static_cast<int*>(
imageAccumulate->GetOutput()->GetScalarPointer(bin, 0, 0)))
<< " ";
}
std::cout << std::endl;
double mean[3];
imageAccumulate->GetMean(mean);
std::cout << "Mean: " << mean[0] << " " << mean[1] << " " << mean[2]
<< std::endl;
return EXIT_SUCCESS;
}
namespace {
void CreateImage(vtkSmartPointer<vtkImageData> image)
{
// Specify the size of the image data
image->SetDimensions(2, 3, 1);
// image->SetExtent(0,2,0,3,0,1);
image->AllocateScalars(VTK_DOUBLE, 1);
int* dims = image->GetDimensions();
// int dims[3]; // can't do this
std::cout << "Dims: "
<< " x: " << dims[0] << " y: " << dims[1] << " z: " << dims[2]
<< std::endl;
// Fill every entry of the image data with "2.0"
for (int z = 0; z < dims[2]; z++)
{
for (int y = 0; y < dims[1]; y++)
{
for (int x = 0; x < dims[0]; x++)
{
double* pixel = static_cast<double*>(image->GetScalarPointer(x, y, z));
pixel[0] = 2.0;
}
}
}
}
} // namespace
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ImageAccumulate)
find_package(VTK COMPONENTS
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ImageAccumulate: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(ImageAccumulate MACOSX_BUNDLE ImageAccumulate.cxx )
target_link_libraries(ImageAccumulate PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ImageAccumulate
MODULES ${VTK_LIBRARIES}
)
Download and Build ImageAccumulate¶
Click here to download ImageAccumulate and its CMakeLists.txt file. Once the tarball ImageAccumulate.tar has been downloaded and extracted,
cd ImageAccumulate/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:
./ImageAccumulate
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.