[vtkusers] Place landmark on 2D image by vtkSeedWidget
Xiaopeng Yang
yxp233 at postech.ac.kr
Mon Feb 7 23:59:48 EST 2011
Dear Members,
In my 2D image viewer, users can click on an image and mark several points
in this image. When users change the slices, the markers should disappear.
But actually the markers never disappear. Could anybody share his or her
ideas?
I searched a lot through Google and spent much time on it. Still I could not
get any solution.
Attached please check my code. Could you point out where and how to change
the code? Thank you so much!
//This callback is responsible for setting the seed label
class vtkSeedCallback : public vtkCommand
{
public:
static vtkSeedCallback *New()
{
return new vtkSeedCallback;
}
vtkSeedCallback() {}
virtual void Execute(vtkObject*, unsigned long event, void *calldata)
{
if(event == vtkCommand::PlacePointEvent)
{
std::cout << "Point placed, total of: "
<< this->SeedRepresentation->GetNumberOfSeeds() << std::endl;
}
if(event == vtkCommand::InteractionEvent)
{
if(calldata)
{
std::cout << "Interacting with seed : "
<< *(static_cast< int * >(calldata)) << std::endl;
}
}
std::cout << "List of seeds (Display coordinates):" << std::endl;
for(vtkIdType i = 0; i < this->SeedRepresentation->GetNumberOfSeeds();
i++)
{
double pos[3];
this->SeedRepresentation->GetSeedWorldPosition(i, pos);
std::cout << "(" << pos[0] << " " << pos[1] << " " << pos[2] << ")"
<< std::endl;
}
}
void SetRepresentation(vtkSmartPointer<vtkSeedRepresentation> rep)
{this->SeedRepresentation = rep;}
private:
vtkSmartPointer<vtkSeedRepresentation> SeedRepresentation;
};
class vtkSliderCallback2 : public vtkCommand
{
public:
static vtkSliderCallback2 *New()
{ return new vtkSliderCallback2; }
void SetImageViewer(vtkImageViewer2 *viewer)
{ this->Viewer = viewer; }
virtual void Execute(vtkObject *caller, unsigned long , void* )
{
vtkSliderWidget *slider = static_cast<vtkSliderWidget *>(caller);
vtkSliderRepresentation *sliderRepres =
static_cast<vtkSliderRepresentation *>(slider->GetRepresentation());
int pos = static_cast<int>(sliderRepres->GetValue());
this->Viewer->SetSlice(pos);
}
protected:
vtkImageViewer2 *Viewer;
};
int main (int argc, char *argv[])
{
typedef float InputPixelType;
typedef float OutputPixelType;
typedef itk::Image< InputPixelType, 3 > InputImageType;
typedef itk::Image< OutputPixelType, 3 > OutputImageType;
// Load DICOM files
typedef itk::ImageSeriesReader< InputImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
typedef itk::GDCMImageIO ImageIOType;
typedef itk::GDCMSeriesFileNames NamesGeneratorType;
ImageIOType::Pointer gdcmIO = ImageIOType::New();
NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
namesGenerator->SetInputDirectory( "D:/Hybrid
Method/Hybrid/KimJaeHuan/Diffusion_filter" );
const ReaderType::FileNamesContainer & filenames =
namesGenerator->GetInputFileNames();
unsigned int numberOfFilenames = filenames.size();
std::cout << numberOfFilenames << " images loaded" << std::endl;
for(unsigned int fni = 0; fni<numberOfFilenames; fni++)
{
std::cout << "filename # " << fni << " = ";
std::cout << filenames[fni] << std::endl;
}
reader->SetImageIO( gdcmIO );
reader->SetFileNames( filenames );
typedef itk::ImageToVTKImageFilter<InputImageType>FilterType;
FilterType::Pointer connector = FilterType::New();
reader->Update();
connector->SetInput(reader->GetOutput());
vtkSmartPointer<vtkLookupTable> table =
vtkSmartPointer<vtkLookupTable>::New();
table->SetRange(129, 137); // image intensity range
table->SetValueRange(0.0, 1.0); // from black to white
table->SetSaturationRange(0.0, 0.0); // no color saturation
table->SetRampToLinear();
table->Build();
vtkSmartPointer<vtkImageMapToColors> color =
vtkSmartPointer<vtkImageMapToColors>::New();
color->SetLookupTable(table);
color->SetInput(connector->GetOutput());
vtkImageViewer2 *imageViewer = vtkImageViewer2::New();
imageViewer->SetInput(color->GetOutput());
imageViewer->SetColorLevel(127);
imageViewer->SetColorWindow(255);
// Create the representation
vtkSmartPointer<vtkPointHandleRepresentation2D> handle =
vtkSmartPointer<vtkPointHandleRepresentation2D>::New();
handle->GetProperty()->SetColor(1,0,0);
vtkSmartPointer<vtkSeedRepresentation> rep =
vtkSmartPointer<vtkSeedRepresentation>::New();
rep->SetHandleRepresentation(handle);
// Seed widget
vtkSmartPointer<vtkSeedWidget> seedWidget =
vtkSmartPointer<vtkSeedWidget>::New();
seedWidget->SetInteractor(iren);
seedWidget->SetRepresentation(rep);
vtkSmartPointer<vtkSeedCallback> seedCallback =
vtkSmartPointer<vtkSeedCallback>::New();
seedCallback->SetRepresentation(rep);
seedWidget->AddObserver(vtkCommand::PlacePointEvent,seedCallback);
seedWidget->AddObserver(vtkCommand::InteractionEvent,seedCallback);
seedWidget->On();
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
imageViewer->SetupInteractor(iren);
imageViewer->GetRenderWindow()->SetMultiSamples(0);
imageViewer->GetRenderWindow()->SetSize(512, 512);
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
vtkSliderRepresentation2D *SliderRepres =
vtkSliderRepresentation2D::New();
int min = imageViewer->GetSliceMin();
int max = imageViewer->GetSliceMax();
SliderRepres->SetMinimumValue(min);
SliderRepres->SetMaximumValue(max);
SliderRepres->SetValue(static_cast<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);
vtkSliderCallback2 *SliderCb = vtkSliderCallback2::New();
SliderCb->SetImageViewer(imageViewer);
SliderWidget->AddObserver(vtkCommand::InteractionEvent, SliderCb);
imageViewer->SetSlice(static_cast<int>(SliderRepres->GetValue()));
imageViewer->SetSliceOrientationToXY();
iren->Start();
SliderRepres->Delete();
SliderCb->Delete();
imageViewer->Delete();
SliderWidget->Delete();
iren->Delete();
return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110208/36b1d11f/attachment.htm>
More information about the vtkusers
mailing list