#include "vtkImageData.h"
#include "vtkVolume16Reader.h"
#include "vtkImageShiftScale.h"
#include "vtkImageViewer2.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSliderRepresentation.h"
#include "vtkSliderRepresentation2D.h"
#include "vtkSliderWidget.h"
#include "vtkCommand.h"
#include "vtkContourWidget.h"
#include "vtkOrientedGlyphContourRepresentation.h"
#include "vtkImageActorPointPlacer.h"
class vtkSliderCallback : public vtkCommand
{
public:
static vtkSliderCallback *New()
{ return new vtkSliderCallback; }
void Delete()
{ delete this; }
void SetImageViewer(vtkImageViewer2 *viewer)
{ m_Viewer = viewer; }
virtual void Execute(vtkObject *caller, unsigned long ul, void* vd)
{
vtkSliderWidget *slider = (vtkSliderWidget *)caller;
vtkSliderRepresentation *sliderRepres = (vtkSliderRepresentation *)slider->GetRepresentation();
int pos = (int)sliderRepres->GetValue();
m_Viewer->SetSlice(pos);
}
protected:
vtkImageViewer2 *m_Viewer;
};
int main(int argc, char *argv[])
{
char* fname = "D:/VTK/vtkdata/Data/headsq/quarter";
vtkVolume16Reader* v16 = vtkVolume16Reader::New();
v16->SetDataDimensions(64, 64);
v16->SetDataByteOrderToLittleEndian();
v16->SetImageRange(1, 93);
v16->SetDataSpacing(3.2, 3.2, 1.5);
v16->SetFilePrefix(fname);
v16->ReleaseDataFlagOn();
v16->SetDataMask(0x7fff);
v16->Update();
//delete[] fname;
double range[2];
v16->GetOutput()->GetScalarRange(range);
vtkImageShiftScale* shifter = vtkImageShiftScale::New();
shifter->SetShift(-1.0*range[0]);
shifter->SetScale(255.0/(range[1]-range[0]));
shifter->SetOutputScalarTypeToUnsignedChar();
shifter->SetInputConnection(v16->GetOutputPort());
shifter->ReleaseDataFlagOff();
shifter->Update();
vtkImageViewer2 *ImageViewer = vtkImageViewer2::New();
ImageViewer->SetInput(shifter->GetOutput());
ImageViewer->SetColorLevel(127);
ImageViewer->SetColorWindow(255);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
ImageViewer->SetupInteractor(iren);
ImageViewer->Render();
ImageViewer->GetRenderer()->ResetCamera();
ImageViewer->Render();
int *dims = v16->GetOutput()->GetDimensions();
std::cout << dims[0] << "," << dims[1] << "," << dims[2] << "\n";
vtkSliderRepresentation2D *SliderRepres = vtkSliderRepresentation2D::New();
int min = ImageViewer->GetSliceMin();
int max = ImageViewer->GetSliceMax();
SliderRepres->SetMinimumValue(min);
SliderRepres->SetMaximumValue(max);
SliderRepres->SetValue((int)((min + max) / 2));
SliderRepres->SetTitleText("Slice");
SliderRepres->GetPoint1Coordinate()->SetCoordinateSystemToNormalizedDisplay();
SliderRepres->GetPoint1Coordinate()->SetValue(0.3, 0.05);
SliderRepres->GetPoint2Coordinate()->SetCoordinateSystemToNormalizedDisplay();
SliderRepres->GetPoint2Coordinate()->SetValue(0.7, 0.05);
SliderRepres->SetSliderLength(0.02);
SliderRepres->SetSliderWidth(0.03);
SliderRepres->SetEndCapLength(0.01);
SliderRepres->SetEndCapWidth(0.03);
SliderRepres->SetTubeWidth(0.005);
SliderRepres->SetLabelFormat("%3.0lf");
SliderRepres->SetTitleHeight(0.02);
SliderRepres->SetLabelHeight(0.02);
vtkSliderWidget *SliderWidget = vtkSliderWidget::New();
SliderWidget->SetInteractor(iren);
SliderWidget->SetRepresentation(SliderRepres);
SliderWidget->KeyPressActivationOff();
SliderWidget->SetAnimationModeToAnimate();
SliderWidget->SetEnabled(true);
vtkSliderCallback *SliderCb = vtkSliderCallback::New();
SliderCb->SetImageViewer(ImageViewer);
SliderWidget->AddObserver(vtkCommand::InteractionEvent, SliderCb);
ImageViewer->SetSlice((int)SliderRepres->GetValue());
vtkContourWidget *ContourWidget = vtkContourWidget::New();
vtkOrientedGlyphContourRepresentation *rep =
vtkOrientedGlyphContourRepresentation::New();
ContourWidget->SetRepresentation(rep);
vtkImageActorPointPlacer * imageActorPointPlacer = vtkImageActorPointPlacer::New();
imageActorPointPlacer->SetImageActor(ImageViewer->GetImageActor());
rep->SetPointPlacer(imageActorPointPlacer);
imageActorPointPlacer->Delete();
rep->Delete();
ContourWidget->SetInteractor(iren);
ContourWidget->SetEnabled(true);
ContourWidget->ProcessEventsOn();
iren->Start();
return -1;
}
本文来源:http://www.vislab.cn/bbs/viewthread.php?tid=1467&extra=page%3D7