[vtkusers] Re: vtkImageTracerWidget & vtkBMPWriter (Flo)

Flo snrf at no-log.org
Fri Aug 31 09:11:22 EDT 2007


Hi Mark:

Here is the kind of pipeline I'm using -

/*
vtkImageTracerWidget->vtkSplineWidget
vtkPolyData->vtkLinearExtrusionFilter
*/

// FOLLOWING THE TestImageTracerWidget.CXX AS A REFERENCE //

	vtkLinearExtrusionFilter* extrude = vtkLinearExtrusionFilter::New();
	extrude->SetInput(splinePoly);
	extrude->SetScaleFactor(1);
	extrude->SetExtrusionTypeToNormalExtrusion();
	extrude->SetVector(0, 0, 1);
	
	vtkPolyDataToImageStencil* dataToStencil =  
vtkPolyDataToImageStencil::New();
	dataToStencil->SetInputConnection(extrude->GetOutputPort());
	
	stencil = vtkImageStencil::New();
	stencil->SetInputConnection(extract->GetOutputPort());
	stencil->SetStencil(dataToStencil->GetOutput());
	stencil->ReverseStencilOff();
	stencil->SetBackgroundValue(128);

  	stencil->UpdateWholeExtent();

	  vtkImageData *ROIData = vtkImageData::New();
	  ROIData->DeepCopy(stencil->GetOutput());

HTH
Flo.

On 30-Aug-07, at 11:22 PM, JohnMark wrote:

> Hello , Flo ,
>     thank you for your reply , but I have tested it for the whole  
> morning , but I still get nothing . I writer a small test code  
> using a vtkImageActor and making it shown in the render window ,  
> but what I get is just a line . I have tried vtkImageSlice to  
> transform it , unfortunately , if I use it , my program will stop  
> at once . maybe vtkImageSlice can not be shown in the render  
> window . this is my code as follows , could you help me to test  
> what is wrong with it ? thank you very much !
>
>
> vtkBMPWriter *bmp;
> vtkLinearExtrusionFilter* extrude;
> vtkPolyDataToImageStencil* dataToStencil;
> vtkImageStencil* stencil;
> vtkImageCast * readerImageCast;
> vtkJPEGWriter * jpg;
> class SaveCommand : public vtkCommand
> {
> public:
>  static SaveCommand *New() { return new SaveCommand; }
>  void Delete() { delete this; }
>  virtual void Execute(vtkObject *caller, unsigned long l, void  
> *callData)
>  {
>   bmp->Update();
>   bmp->Write();
>  }
> };
> class vtkITWCallback : public vtkCommand
> {
> public:
>   static vtkITWCallback *New()
>     { return new vtkITWCallback; }
>   virtual void Execute(vtkObject *caller, unsigned long, void*)
>     {
>       vtkImageTracerWidget *tracerWidget =
>         reinterpret_cast<vtkImageTracerWidget*>(caller);
>       if(!tracerWidget) { return; }
>       int closed = tracerWidget->IsClosed();
>       SplineWidget->SetClosed(closed);
>       if (!closed)
>         {
>         Actor->SetInput(Extract->GetOutput());
>         }
>       int npts = tracerWidget->GetNumberOfHandles();
>       if (npts < 2) { return; }
>       tracerWidget->GetPath(PathPoly);
>       vtkPoints* points = PathPoly->GetPoints();
>       if (!points){ return; }
>       SplineWidget->InitializeHandles(points);
>       if (closed)
>         {
>         SplineWidget->GetPolyData(SplinePoly);
>         Stencil->Update();
>         Actor->SetInput(Stencil->GetOutput());
>  vtkImageData * imagedata = vtkImageData::New();
>   imagedata->DeepCopy((vtkDataObject *)Stencil->GetOutput());
>  vtkImageActor * test = vtkImageActor::New();
>   test->SetInput(imagedata);
>  vtkRenderer * ren = vtkRenderer::New();
>  vtkRenderWindow * renwin = vtkRenderWindow::New();
>   renwin->AddRenderer(ren);
>   ren->AddActor(test);
>   renwin->SetPosition(300,300);
>   renwin->SetSize(300,300);
>   renwin->Render();
>  vtkRenderWindowInteractor * iren = vtkRenderWindowInteractor::New();
>   iren->SetRenderWindow(renwin);
>   iren->Initialize();
>   iren->Start();
>         }
>     }
>   vtkITWCallback():SplineWidget(0),Actor(0),Stencil(0),Extract(0),
>                    PathPoly(0),SplinePoly(0){}
>   vtkSplineWidget *SplineWidget;
>   vtkImageActor   *Actor;
>   vtkImageStencil *Stencil;
>   vtkExtractVOI   *Extract;
>   vtkPolyData     *PathPoly;
>   vtkPolyData     *SplinePoly;
> };
> class vtkSW2Callback : public vtkCommand
> {
> public:
>   static vtkSW2Callback *New()
>     { return new vtkSW2Callback; }
>   virtual void Execute(vtkObject *caller, unsigned long, void*)
>     {
>       vtkSplineWidget *splineWidget =
>         reinterpret_cast<vtkSplineWidget*>(caller);
>       if(!splineWidget) { return; }
>       int npts = splineWidget->GetNumberOfHandles();
>       int closed = splineWidget->IsClosed();
>       Points->Reset();
>       for (int i = 0; i < npts; ++i)
>         {
>         Points->InsertNextPoint(splineWidget->GetHandlePosition(i));
>         }
>       if (closed)
>         {
>         if (TracerWidget->GetAutoClose())
>           {
>           Points->InsertNextPoint(splineWidget->GetHandlePosition(0));
>           }
>          splineWidget->GetPolyData(SplinePoly);
>          Stencil->Update();
>          //Actor->SetInput(Stencil->GetOutput());
>         }
>       TracerWidget->InitializeHandles(Points);
>     }
>   vtkSW2Callback():Points(0),TracerWidget(0),Actor(0),Stencil 
> (0),SplinePoly(0){}
>   vtkPoints            *Points;
>   vtkImageTracerWidget *TracerWidget;
>   vtkImageActor        *Actor;
>   vtkImageStencil      *Stencil;
>   vtkPolyData          *SplinePoly;
> };
> int TestImageTracerWidget()
> {
>   vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
>   vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters 
> (10,10);
>   vtkDICOMImageReader *reader = vtkDICOMImageReader::New();
>     reader->SetDataByteOrderToLittleEndian();
>     reader->SetDirectoryName("E:\\Data1\\chest");
>     reader->SetDataSpacing(3.2, 3.2, 1.5);
>     reader->SetDataOrigin(0.0, 0.0, 0.0);
>   vtkImageCast *readerImageCast1 = vtkImageCast::New();
>  readerImageCast1->SetInputConnection(reader->GetOutputPort());
>  readerImageCast1->SetOutputScalarTypeToUnsignedChar();
>  readerImageCast1->ClampOverflowOn();
>   vtkRenderer* ren1 = vtkRenderer::New();
>   vtkRenderer* ren2 = vtkRenderer::New();
>   vtkRenderWindow* renWin = vtkRenderWindow::New();
>     renWin->AddRenderer(ren1);
>     renWin->AddRenderer(ren2);
>   vtkInteractorStyleImage* interactorStyle =  
> vtkInteractorStyleImage::New();
>   vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
>     iren->SetInteractorStyle(interactorStyle);
>     iren->SetRenderWindow(renWin);
>   double range[2];
>   readerImageCast1->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(readerImageCast1->GetOutputPort());
>     shifter->ReleaseDataFlagOff();
>     shifter->Update();
>   vtkImageActor* imageActor1 = vtkImageActor::New();
>     imageActor1->SetInput(shifter->GetOutput());
>     imageActor1->VisibilityOn();
>     imageActor1->SetDisplayExtent(250, 250, 0, 511, 0, 21);
>     imageActor1->InterpolateOff();
>   vtkExtractVOI* extract = vtkExtractVOI::New();
>     extract->SetVOI(imageActor1->GetDisplayExtent());
>     extract->SetSampleRate(1, 1, 1);
>     extract->SetInputConnection(shifter->GetOutputPort());
>     extract->ReleaseDataFlagOff();
>   vtkImageActor* imageActor2 = vtkImageActor::New();
>     imageActor2->SetInput(extract->GetOutput());
>     imageActor2->VisibilityOn();
>     imageActor2->SetDisplayExtent(extract->GetVOI());
>     imageActor2->InterpolateOff();
>   vtkImageTracerWidget* imageTracerWidget =  
> vtkImageTracerWidget::New();
>     imageTracerWidget->SetDefaultRenderer(ren1);
>     imageTracerWidget->SetCaptureRadius(1.5);
>     imageTracerWidget->GetGlyphSource()->SetColor(1, 1, 1);
>     imageTracerWidget->GetGlyphSource()->SetScale(3.0);
>     imageTracerWidget->GetGlyphSource()->SetRotationAngle(45.0);
>     imageTracerWidget->GetGlyphSource()->Modified();
>     imageTracerWidget->ProjectToPlaneOn();
>     imageTracerWidget->SetProjectionNormalToXAxes();
>     imageTracerWidget->SetProjectionPosition(imageActor1->GetBounds 
> ()[0]);
>     imageTracerWidget->SetViewProp(imageActor1);
>     imageTracerWidget->SetInput(shifter->GetOutput());
>     imageTracerWidget->SetInteractor(iren);
>     imageTracerWidget->PlaceWidget();
>     imageTracerWidget->SnapToImageOn();
>     imageTracerWidget->AutoCloseOn();
>   vtkSplineWidget* splineWidget = vtkSplineWidget::New();
>     splineWidget->SetCurrentRenderer(ren2);
>     splineWidget->SetDefaultRenderer(ren2);
>     splineWidget->SetInput(extract->GetOutput());
>     splineWidget->SetInteractor(iren);
>     splineWidget->PlaceWidget(imageActor2->GetBounds());
>     splineWidget->ProjectToPlaneOn();
>     splineWidget->SetProjectionNormalToXAxes();
>     splineWidget->SetProjectionPosition(imageActor2->GetBounds()[0]);
>     splineWidget->ClosedOn();
>   vtkPolyData* pathPoly = vtkPolyData::New();
>   vtkPoints* points = vtkPoints::New();
>   vtkPolyData* splinePoly = vtkPolyData::New();
>   extrude = vtkLinearExtrusionFilter::New();
>     extrude->SetInput(splinePoly);
>     extrude->SetScaleFactor(1);
>     extrude->SetExtrusionTypeToNormalExtrusion();
>     extrude->SetVector(1, 0, 0);
>   dataToStencil = vtkPolyDataToImageStencil::New();
>     dataToStencil->SetInputConnection(extrude->GetOutputPort());
>   stencil = vtkImageStencil::New();
>     stencil->SetInputConnection(extract->GetOutputPort());
>     stencil->SetStencil(dataToStencil->GetOutput());
>     stencil->SetBackgroundValue(-1000);
>   readerImageCast = vtkImageCast::New();
>  readerImageCast->SetInput((vtkDataObject *)stencil->GetOutput());
>  readerImageCast->SetOutputScalarTypeToUnsignedChar();
>  readerImageCast->ClampOverflowOn();
>   bmp = vtkBMPWriter::New();
>  bmp->SetInput(readerImageCast->GetOutput());
>  bmp->SetFileName("E:/Data/bmptest.bmp");
>   vtkITWCallback* itwCallback = vtkITWCallback::New();
>     itwCallback->SplineWidget = splineWidget;
>     itwCallback->Actor = imageActor2;
>     itwCallback->Stencil = stencil;
>     itwCallback->Extract = extract;
>     itwCallback->PathPoly = pathPoly;
>     itwCallback->SplinePoly = splinePoly;
>   imageTracerWidget->AddObserver 
> (vtkCommand::EndInteractionEvent,itwCallback);
>   vtkSW2Callback* swCallback = vtkSW2Callback::New();
>     swCallback->Points = points;
>     swCallback->TracerWidget = imageTracerWidget;
>     swCallback->Actor = imageActor2;
>     swCallback->Stencil = stencil;
>     swCallback->SplinePoly = splinePoly;
>   splineWidget->AddObserver 
> (vtkCommand::EndInteractionEvent,swCallback);
>   ren1->SetBackground(0.4, 0.4, 0.5);
>   ren1->SetViewport(0, 0, 0.5, 1);
>   ren1->AddViewProp(imageActor1);
>   ren2->SetBackground(0.5, 0.4, 0.4);
>   ren2->SetViewport(0.5, 0, 1, 1);
>   ren2->AddViewProp(imageActor2);
>   ren1->ResetCamera();
>   ren2->ResetCamera();
>   renWin->SetSize(480, 240);
>   imageTracerWidget->On();
>   splineWidget->On();
>   vtkCamera* cam = ren1->GetActiveCamera();
>   cam->SetViewUp(0, 1, 0);
>   cam->Azimuth(270);
>   cam->Roll(270);
>   cam->Dolly(1.7);
>   ren1->ResetCameraClippingRange();
>   cam = ren2->GetActiveCamera();
>   cam->SetViewUp(0, 1, 0);
>   cam->Azimuth(270);
>   cam->Roll(270);
>   cam->Dolly(1.7);
>   ren2->ResetCameraClippingRange();
>   SaveCommand * save = SaveCommand::New();
>   iren->AddObserver(vtkCommand::KeyReleaseEvent,save);
>   iren->Initialize();
>   renWin->Render();
>   iren->Start();
>   return 0;
> }
> void main()
> {
>  int i = TestImageTracerWidget();
> }
>
>     thank you with great regards ! and waiting for your answer !
>     Mark
>     PS . maybe there is something wrong , I can not receive your  
> answer mail , sorry !
>
>
>
> >
> > Message: 19
> > Date: Thu, 30 Aug 2007 10:47:33 -0400
> > From: Flo <snrf at no-log.org>
> > Subject: Re: [vtkusers] vtkImageTracerWidget & vtkBMPWriter
> > To: JohnMark <zhaojunxp at hotmail.com>
> > Cc: "vtkusers at vtk.org" <vtkusers at vtk.org>
> > Message-ID: <526C1A4F-7EA1-4C19-9D51-3413CD1109F3 at no-log.org>
> > Content-Type: text/plain; charset=UTF-8; delsp=yes; format=flowed
> >
> > Mark:
> >
> > From my experience, there is a pointer reference issue.
> > I overcame it by using a temp vtkImageData to which I ->DeepCopy
> > (TracerWidget->GetOutput()).
> > After that, you just work with the vtkImageData and you'll be fine.
> >
> > HTH
> > Flo.
> >
> > On 29-Aug-07, at 8:56 PM, JohnMark wrote:
> >
> > > Hello , vtkusers ,
> > > I want to extract any regions from a 2D CT slice , so I use the
> > > vtkImageTracerWidget . surely , it is perfect , I can clip any
> > > regions . but there is the problem coming : I want to save the
> > > result with vtkBMPWriter :
> > >
> > > vtkImageCast * readerImageCast = vtkImageCast::New();
> > > // its data from the output of vtkImageStencil
> > > readerImageCast->SetInput((vtkDataObject *)stencil->GetOutput());
> > > readerImageCast->SetOutputScalarTypeToUnsignedChar();
> > > vtkBMPWriter * bmp = vtkBMPWriter::New();
> > > bmp->SetInput(readerImageCast1->GetOutput());
> > > bmp->SetFileName("C:/test.bmp");
> > > bmp->Write(); // problem !!!! it does not work any more !
> > > bmp->Update();
> > >
> > > however , it does not work , what I got is just a black line
> > > ( nothing ) . and then I change my method , I add a  
> KeyReleaseEvent
> > > and a AddObserver of vtkRenderWindowInteractor , the render window
> > > would capture the message when the key was released and I get the
> > > region , then I will save the result , but I still get nothing .
> > >
> > > so what is wrong with my thinking ? and I know the
> > > vtkImageTracerWidget class wrap the vtkImageViewer , vtkActor and
> > > so on , I can save the result rendering in the vtkImageViewer ,  
> but
> > > I can do nothing with the result of vtkImageTracerWidget . we can
> > > not save the result but just to see it , isn't it ? if it is not ,
> > > is there any method or any class to save it ?
> > >
> > > thank you with great regards ! and waiting for your answer !
> > >
> > > Mark
> > >
> > > PS . this is a problem I have been thinking for almost a
> > > month , I still get nothing now .
> > >
> > >
> > > 使用新一代 Windows Live Messenger  
> 轻松交流和共享! 立
> > > 即体验!
> > > _______________________________________________
> > > This is the private VTK discussion list.
> > > Please keep messages on-topic. Check the FAQ at: http:// 
> www.vtk.org/
> > > Wiki/VTK_FAQ
> > > Follow this link to subscribe/unsubscribe:
> > > http://www.vtk.org/mailman/listinfo/vtkusers
> >
>
> > Message: 22
> > Date: Thu, 30 Aug 2007 11:04:31 -0400
> > From: Flo <snrf at no-log.org>
> > Subject: Re: [vtkusers] vtkImageTracerWidget & vtkBMPWriter
> > To: JohnMark <zhaojunxp at hotmail.com>
> > Cc: "vtkusers at vtk.org" <vtkusers at vtk.org>
> > Message-ID: <DB3B1816-BE07-4CB3-90F3-7C8B10737ADB at no-log.org>
> > Content-Type: text/plain; charset=UTF-8; delsp=yes; format=flowed
> >
> > Mark:
> >
> > From my experience, there is a pointer reference issue.
> > I overcame it by using a temp vtkImageData to which I ->DeepCopy
> > (TracerWidget->GetOutput()).
> > After that, you just work with the vtkImageData and you'll be fine.
> >
> > HTH
> > Flo.
> >
> > On 29-Aug-07, at 8:56 PM, JohnMark wrote:
> >
> > > Hello , vtkusers ,
> > > I want to extract any regions from a 2D CT slice , so I use the
> > > vtkImageTracerWidget . surely , it is perfect , I can clip any
> > > regions . but there is the problem coming : I want to save the
> > > result with vtkBMPWriter :
> > >
> > > vtkImageCast * readerImageCast = vtkImageCast::New();
> > > // its data from the output of vtkImageStencil
> > > readerImageCast->SetInput((vtkDataObject *)stencil->GetOutput());
> > > readerImageCast->SetOutputScalarTypeToUnsignedChar();
> > > vtkBMPWriter * bmp = vtkBMPWriter::New();
> > > bmp->SetInput(readerImageCast1->GetOutput());
> > > bmp->SetFileName("C:/test.bmp");
> > > bmp->Write(); // problem !!!! it does not work any more !
> > > bmp->Update();
> > >
> > > however , it does not work , what I got is just a black line
> > > ( nothing ) . and then I change my method , I add a  
> KeyReleaseEvent
> > > and a AddObserver of vtkRenderWindowInteractor , the render window
> > > would capture the message when the key was released and I get the
> > > region , then I will save the result , but I still get nothing .
> > >
> > > so what is wrong with my thinking ? and I know the
> > > vtkImageTracerWidget class wrap the vtkImageViewer , vtkActor and
> > > so on , I can save the result rendering in the vtkImageViewer ,  
> but
> > > I can do nothing with the result of vtkImageTracerWidget . we can
> > > not save the result but just to see it , isn't it ? if it is not ,
> > > is there any method or any class to save it ?
> > >
> > > thank you with great regards ! and waiting for your answer !
> > >
> > > Mark
> > >
> > > PS . this is a problem I have been thinking for almost a
> > > month , I still get nothing now .
> > >
> > >
> > > 使用新一代 Windows Live Messenger  
> 轻松交流和共享! 立
> > > 即体验!
> > > _______________________________________________
> > > This is the private VTK discussion list.
> > > Please keep messages on-topic. Check the FAQ at: http:// 
> www.vtk.org/
> > > Wiki/VTK_FAQ
> > > Follow this link to subscribe/unsubscribe:
> > > http://www.vtk.org/mailman/listinfo/vtkusers
> >
> >
> >
>
>
>
> 不登录就能管理多个邮件帐户,试试 Windows Live  
> Mail。 立即尝试!




More information about the vtkusers mailing list