ImageHistogram
Repository source: ImageHistogram
Other languages
See (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ImageHistogram.cxx
#include <vtkCamera.h>
#include <vtkImageData.h>
#include <vtkImageHistogram.h>
#include <vtkImageProperty.h>
#include <vtkImageReader2.h>
#include <vtkImageReader2Factory.h>
#include <vtkImageSlice.h>
#include <vtkImageSliceMapper.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " filename e.g. Pileated.jpg"
<< std::endl;
return EXIT_FAILURE;
}
vtkNew<vtkRenderWindowInteractor> iren;
vtkNew<vtkInteractorStyle> style;
vtkNew<vtkRenderWindow> renWin;
iren->SetRenderWindow(renWin);
iren->SetInteractorStyle(style);
vtkNew<vtkImageReader2Factory> readerFactory;
vtkSmartPointer<vtkImageReader2> reader;
reader.TakeReference(readerFactory->CreateImageReader2(argv[1]));
reader->SetFileName(argv[1]);
vtkNew<vtkImageHistogram> histogram;
histogram->SetInputConnection(reader->GetOutputPort());
histogram->GenerateHistogramImageOn();
histogram->SetHistogramImageSize(256, 256);
histogram->SetHistogramImageScaleToSqrt();
histogram->AutomaticBinningOn();
histogram->Update();
vtkIdType nbins = histogram->GetNumberOfBins();
double range[2];
range[0] = histogram->GetBinOrigin();
range[1] = range[0] + (nbins - 1) * histogram->GetBinSpacing();
for (int i = 0; i < 2; i++)
{
vtkNew<vtkRenderer> renderer;
vtkCamera* camera = renderer->GetActiveCamera();
renderer->SetViewport(0.5 * (i & 1), 0.0, 0.5 + 0.5 * (i & 1), 1.0);
renWin->AddRenderer(renderer);
vtkNew<vtkImageSliceMapper> imageMapper;
// compute y range
double yd;
if ((i & 1) == 0)
{
imageMapper->SetInputConnection(reader->GetOutputPort());
int extent[6];
reader->GetOutput()->GetExtent(extent);
yd = (extent[3] - extent[2] + 1);
}
else
{
imageMapper->SetInputConnection(histogram->GetOutputPort());
imageMapper->BorderOn();
int extent[6];
histogram->GetOutput()->GetExtent(extent);
yd = (extent[3] - extent[2] + 1);
}
const double* bounds = imageMapper->GetBounds();
double point[3];
point[0] = 0.5 * (bounds[0] + bounds[1]);
point[1] = 0.5 * (bounds[2] + bounds[3]);
point[2] = 0.5 * (bounds[4] + bounds[5]);
camera->SetFocalPoint(point);
point[imageMapper->GetOrientation()] += 1000;
camera->SetPosition(point);
camera->SetViewUp(0.0, 1.0, 0.0);
camera->ParallelProjectionOn();
// Set scale so that vertical dimension fills the window
camera->SetParallelScale(0.5 * yd);
vtkNew<vtkImageSlice> image;
image->SetMapper(imageMapper);
renderer->AddViewProp(image);
if ((i & 1) == 0)
{
image->GetProperty()->SetColorWindow(range[1] - range[0]);
image->GetProperty()->SetColorLevel(0.5 * (range[0] + range[1]));
}
else
{
image->GetProperty()->SetInterpolationTypeToNearest();
image->GetProperty()->SetColorWindow(255.0);
image->GetProperty()->SetColorLevel(127.5);
}
}
renWin->SetSize(640, 480);
renWin->SetWindowName("ImageHistogram");
iren->Initialize();
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ImageHistogram)
find_package(VTK COMPONENTS
CommonCore
CommonDataModel
IOImage
ImagingStatistics
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ImageHistogram: 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(ImageHistogram MACOSX_BUNDLE ImageHistogram.cxx )
target_link_libraries(ImageHistogram PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ImageHistogram
MODULES ${VTK_LIBRARIES}
)
Download and Build ImageHistogram¶
Click here to download ImageHistogram and its CMakeLists.txt file. Once the tarball ImageHistogram.tar has been downloaded and extracted,
cd ImageHistogram/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:
./ImageHistogram
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.