[vtkusers] [KWWidgets] problem using VTKKWLoadSaveButton

Ashish Singh mrasingh at gmail.com
Tue Dec 12 13:50:38 EST 2006


Hi Yumin,

Thanks for replying. I checked my code again. I don't think there is any
problem with the VTK part in there. I can successfully read a dicom image
and display it. Currently the file to be read is hard coded in the VTK code
and I wanted to add this KWWidgets based filedialog to allow a user to
choose the file to be displayed.
I also tried using the code that you sent me, to display the image in this
example, but even that doesn't work.
I am attaching my complete code along with a single dicom file for
testing(for that matter any jpeg file with appropriate code modifications
would work for testing). It would be a great help if you or anyone in the
list can help me figure out what's going wrong. I have just modified the
MedicalImageViewer example and am using the same cmakelists.txt file also.

Thanks,
Ashish
-----------------------------------------------
file 1- vtkKWMyWindow.h---below
--------------------------------------------------
#ifndef __vtkKWMyWindow_h
#define __vtkKWMyWindow_h

#include "vtkKWWindow.h"

class vtkKWRenderWidget;
class vtkImageViewer2;
class vtkKWScale;
class vtkKWWindowLevelPresetSelector;
class vtkKWSimpleAnimationWidget;
class vtkKWLoadSaveButton;
class vtkDICOMImageReader;
class vtkImageData;

class vtkKWMyWindow : public vtkKWWindow
{
public:
  static vtkKWMyWindow* New();
  vtkTypeRevisionMacro(vtkKWMyWindow,vtkKWWindow);
  virtual void mycallback();

protected:
  vtkKWMyWindow();
  ~vtkKWMyWindow();

  // Description:
  // Create the widget.
  virtual void CreateWidget();

  vtkImageViewer2                *ImageViewer;
  vtkKWRenderWidget              *RenderWidget;
  vtkKWLoadSaveButton             *myLoadSaveButton;
  vtkDICOMImageReader             *mydicom;
  vtkImageData                     *img_data;

 // virtual void UpdateSliceRanges();

private:
  vtkKWMyWindow(const vtkKWMyWindow&);   // Not implemented.
  void operator=(const vtkKWMyWindow&);  // Not implemented.
};

#endif
------------------------------------------------------
file 2- vtkKWMyWindow.cxx---below
-----------------------------------------------------
#include "vtkKWMyWindow.h"

#include "vtkCornerAnnotation.h"
#include "vtkImageData.h"
#include "vtkImageViewer2.h"
#include "vtkKWApplication.h"
#include "vtkKWFrame.h"
#include "vtkKWFrameWithLabel.h"
#include "vtkKWMenu.h"
#include " vtkKWMenuButton.h"
#include "vtkKWMenuButtonWithSpinButtons.h"
#include "vtkKWMenuButtonWithSpinButtonsWithLabel.h"
#include "vtkKWNotebook.h"
#include "vtkKWRenderWidget.h "
#include "vtkKWScale.h"
#include "vtkKWSimpleAnimationWidget.h"
#include "vtkKWWindow.h"
#include "vtkKWWindowLevelPresetSelector.h"
#include "vtkObjectFactory.h "
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkXMLImageDataReader.h"

#include "vtkKWLoadSaveButton.h"
#include "vtkKWLoadSaveDialog.h "
#include "vtkDICOMImageReader.h"

#include "vtkKWWidgetsPaths.h"
#include "vtkToolkits.h"

#include <vtksys/SystemTools.hxx>

vtkStandardNewMacro( vtkKWMyWindow );
vtkCxxRevisionMacro(vtkKWMyWindow, "$Revision: 1.2 $");


vtkKWMyWindow::vtkKWMyWindow()
{
  this->RenderWidget = NULL;
  this->ImageViewer = NULL;
  this->mydicom = NULL;
  this->myLoadSaveButton = NULL;
  this->img_data = NULL;
}

vtkKWMyWindow::~vtkKWMyWindow()
{
  if (this->ImageViewer)
    {
    this->ImageViewer->Delete();
    }
  if (this->RenderWidget)
    {
    this->RenderWidget->Delete();
    }
  if(this->mydicom)
      this->mydicom->Delete();
  if(this->myLoadSaveButton)
      this->myLoadSaveButton->Delete();
  if(this->img_data)
      this->img_data->Delete();

}

void vtkKWMyWindow::CreateWidget()
{
  // Check if already created

  if (this->IsCreated())
    {
    vtkErrorMacro("class already created");
    return;
    }

  // Call the superclass to create the whole widget

  this->Superclass::CreateWidget();

  vtkKWApplication *app = this->GetApplication();

  // Add a render widget, attach it to the view frame, and pack

  if (!this->RenderWidget)
    {
    this->RenderWidget = vtkKWRenderWidget::New();
    }
  this->RenderWidget->SetParent(this->GetViewFrame());
  this->RenderWidget->Create();
  this->RenderWidget->ResetCamera();
  this->RenderWidget->CornerAnnotationVisibilityOn();

  app->Script("pack %s -expand y -fill both -anchor c -expand y",
              this->RenderWidget->GetWidgetName());

  //create loadsavebutton
  if(!this->myLoadSaveButton)
  {
      this->myLoadSaveButton = vtkKWLoadSaveButton::New();
  }
  this->myLoadSaveButton->SetParent(this->GetMainPanelFrame());
  this->myLoadSaveButton->Create();
  this->myLoadSaveButton->SetText("Click to Pick a File");
  this->myLoadSaveButton->GetLoadSaveDialog()->SaveDialogOff();
  this->myLoadSaveButton->GetLoadSaveDialog()->SetFileTypes("{ {Dicom
Document} {.dcm} }");

  this->myLoadSaveButton->SetCommand(this, "mycallback");

  app->Script("pack %s -side top -anchor nw -expand n -padx 2 -pady
2",this->myLoadSaveButton->GetWidgetName());
//  this->RenderWidget->ResetCamera();

}

void vtkKWMyWindow::mycallback()
{

this->mydicom->SetFileName(this->myLoadSaveButton->GetLoadSaveDialog()->GetFileName());
//    this->mydicom->Update();

    /*if (!this->ImageViewer)
    {
        this->ImageViewer = vtkImageViewer2::New();
    }*/
//
this->ImageViewer->SetRenderWindow(this->RenderWidget->GetRenderWindow());
//    this->ImageViewer->SetInput(this->mydicom->GetOutput());
//
this->ImageViewer->SetupInteractor(this->RenderWidget->GetRenderWindow()->GetInteractor());
//    this->ImageViewer->Render();

    this->img_data = this->mydicom->GetOutput();
    double *range = this->img_data->GetScalarRange();
    this->ImageViewer->SetColorWindow(range[1] - range[0]);
    this->ImageViewer->SetColorLevel(0.5 * (range[1] + range[0]));
    this->ImageViewer->SetInput(img_data);
    this->ImageViewer->Render();

}
----------------------------------------------------------------------------
file 3-KWMedicalImageViewerExample.cxx---below
---------------------------------------------------------------------------
#include "vtkKWApplication.h"
#include "vtkKWMyWindow.h"

#include <vtksys/SystemTools.hxx>
#include <vtksys/CommandLineArguments.hxx>

extern "C" int Kwmedicalimageviewerexamplelib_Init(Tcl_Interp *interp);

int my_main(int argc, char *argv[])
{
  // Initialize Tcl

  Tcl_Interp *interp = vtkKWApplication::InitializeTcl(argc, argv, &cerr);
  if (!interp)
    {
    cerr << "Error: InitializeTcl failed" << endl ;
    return 1;
    }

  // Initialize our Tcl library (i.e. our classes wrapped in Tcl).
  // This *is* required for the C++ methods to be used as callbacks.
  // See comment at the top of this file.

  Kwmedicalimageviewerexamplelib_Init(interp);

  // Process some command-line arguments
  // The --test option here is used to run this example as a non-interactive

  // test for software quality purposes. You can ignore it.

  int option_test = 0;
  vtksys::CommandLineArguments args;
  args.Initialize(argc, argv);
  args.AddArgument(
    "--test", vtksys::CommandLineArguments::NO_ARGUMENT, &option_test, "");
  args.Parse();

  // Create the application
  // If --test was provided, ignore all registry settings, and exit silently
  // Restore the settings that have been saved to the registry, like
  // the geometry of the user interface so far.

  vtkKWApplication *app = vtkKWApplication::New();
  app->SetName("KWMedicalImageViewerExample");
  if (option_test)
    {
    app->SetRegistryLevel(0);
    app->PromptBeforeExitOff();
    }
  app->RestoreApplicationSettingsFromRegistry();

  // Set a help link. Can be a remote link (URL), or a local file

  app->SetHelpDialogStartingPage(" http://www.kwwidgets.org");

  // Add our window
  // Set 'SupportHelp' to automatically add a menu entry for the help link

  vtkKWMyWindow *win = vtkKWMyWindow::New();
  win->SupportHelpOn();
  app->AddWindow(win);
  win->Create();
  //win->SecondaryPanelVisibilityOff();
    std::cout<<"hi"<<"\n";

  // Start the application
  // If --test was provided, do not enter the event loop and run this
example
  // as a non-interactive test for software quality purposes.

  int ret = 0;
  win->Display();
  if (!option_test)
    {
    app->Start(argc, argv);
    ret = app->GetExitStatus();
    }
  win->Close();

  // Deallocate and exit

  win->Delete();
  app->Delete();

  return ret;
}

#if defined(_WIN32) && !defined(__CYGWIN__)
#include <windows.h>
int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR lpCmdLine, int)
{
  int argc;
  char **argv;
  vtksys::SystemTools::ConvertWindowsCommandLineToUnixArguments(
    lpCmdLine, &argc, &argv);
  int ret = my_main(argc, argv);
  for (int i = 0; i < argc; i++) { delete [] argv[i]; }
  delete [] argv;
  return ret;
}
#else
int main(int argc, char *argv[])
{
  return my_main(argc, argv);
}
#endif
--------------------------

On 12/11/06, Yumin Yuan <yumin.yuan at kitware.com> wrote:
>
> Hi Ashish,
>
> Since I do not have the DICOM files you are using, here are a couple
> thoughts:
>
> 1. Tell us on which line of code it is crashing.
>
> 2. Make sure the vtkDICOMImageReader can read your files.
> If it is crashing on "img2->Update()", or
> "this->ImageViewer->SetInput(img_data);"
> or  "this->ImageViewer->Render();"
>
> your problem is more appropriate on the VTK users mailing list:
>
> http://www.vtk.org/mailman/listinfo/vtkusers
>
> 3. Here are the documentation for vtkImageViewer2 (for displaying 2D
> image) and vtkDICOMImageReader
> http://www.vtk.org/doc/nightly/html/classvtkDICOMImageReader.html
> http://www.vtk.org/doc/nightly/html/classvtkImageViewer2.html
>
> 4. Use the following code to display the image:
>
>   vtkImageData *img_data = img2->GetOutput();
>    double *range = img_data->GetScalarRange();
>    this->ImageViewer->SetColorWindow(range[1] - range[0]);
>    this->ImageViewer->SetColorLevel(0.5 * (range[1] + range[0]));
>    this->ImageViewer->SetInput(img_data);
> this->ImageViewer->Render();
>
> 5. If you could be more specific on your problem, such as which line
> of code is crashing, it will save us a great deal of time to find a
> solution.
>
> Hope this helps,
>
> -Yumin
> -----------------------------
> Yumin Yuan
> R&D Engineer
> Kitware Inc.
>
>
> On 12/11/06, Ashish Singh <mrasingh at gmail.com> wrote:
> > Hi,
> >
> > Can someone please tell me how to resolve this problem? I am stuck here
> and
> > can't figure out what's going wrong.
> >
> > Thanks,
> > Ashish
> >
> >
> > On 12/8/06, Ashish Singh <mrasingh at gmail.com> wrote:
> > > Thanks Yumin.
> > > I tried creating a callback function. On running the code, I get the
> > loadsave button but after selecting a file, the program crashes. I can't
> > figure out what's wrong. Here is my code. Can you please tell me where I
> am
> > going wrong and how to get it to work?
> > >
> > > ---part of my code----
> > > .
> > > .
> > > .
> > >
> > >  if(!this->myLoadSaveButton)
> > >   {
> > >       this->myLoadSaveButton = vtkKWLoadSaveButton::New();
> > >   }
> > >
> > this->myLoadSaveButton->SetParent(this->GetMainPanelFrame());
> > >   this->myLoadSaveButton->Create();
> > >   this->myLoadSaveButton->SetText("Click to Pick a
> > File");
> > >
> > this->myLoadSaveButton->GetLoadSaveDialog()->SaveDialogOff();
> > >
> > this->myLoadSaveButton->GetLoadSaveDialog()->SetFileTypes("{
> > {Dicom Document} {.dcm} }");
> > >   this->myLoadSaveButton->SetCommand(this,"mycallback");
> > >
> > >   app->Script("pack %s -side top -anchor nw -expand n -padx 2 -pady
> > 2",this->myLoadSaveButton->GetWidgetName());
> > > .
> > > .
> > > void vtkKWMyWindow::mycallback()
> > > {
> > >
> >
> this->mydicom->SetFileName(this->myLoadSaveButton->GetLoadSaveDialog()->GetFileName());
> > >     this->mydicom->Update();
> > >     if (!this->ImageViewer)
> > >     {
> > >         this->ImageViewer = vtkImageViewer2::New();
> > >     }
> > >
> >
> this->ImageViewer->SetRenderWindow(this->RenderWidget->GetRenderWindow());
> > >
> > this->ImageViewer->SetRenderer(this->RenderWidget->GetRenderer());
> > >
> > this->ImageViewer->SetInput(this->mydicom->GetOutput());
> > >
> >
> this->ImageViewer->SetupInteractor(this->RenderWidget->GetRenderWindow()->GetInteractor());
>
> > >     this->ImageViewer->Render();
> > >
> > > }
> > >
> > > Thanks,
> > > Ashish
> > > ---------
> > >
> > >
> > >
> > > On 12/6/06, Yumin Yuan < yumin.yuan at kitware.com > wrote:
> > > > Hi Ashish,
> > > >
> > > > The problem is the
> > "load_button1->GetLoadSaveDialog()->GetFileName()"
> > > > returns NULL in your code.
> > > >
> > > > Two options here:
> > > >
> > > > 1. Use vtkKWLoadSaveDialog directly, instead of the
> > > > vtkKWLoadSaveButton, and call Invoke() on the dialog before
> > > > GetFileName().
> > > > Also, check dialog status "GetStatus() == vtkKWDialog::StatusOK"
> > > >
> > > >
> > > > 2. If you want to use vtkKWLoadSaveButton, try to add a callback to
> the
> > button:
> > > >
> > > > for example:
> > > >
> > > >    this->load_button1->SetCommand( this, "SelectFileCallback");
> > > >
> > > > before packing statement.
> > > >
> > > > >   app->Script("pack %s -side top -anchor nw -expand n -padx 2
> -pady
> > > > > 2",load_button1->GetWidgetName());
> > > >
> > > > Then, put your file loading code in the callback function
> > > >
> > > > void yourclass::SelectFileCallback()
> > > > {
> > > >
> > > > //Check dialog status "GetStatus() == vtkKWDialog::StatusOK"
> > > >
> > > > >    vtkDICOMImageReader
> > *img2=vtkDICOMImageReader::New();
> > > > >
> > > > >
> > img2->SetFileName(load_button1->GetLoadSaveDialog()->GetFileName());
> > > > >    img2->Update();
> > > > >
> > > > >   vtkImageViewer2 * myviewer=vtkImageViewer2::New();
> > > > >   myviewer->SetInput(img2->GetOutput());
> > > > >   myviewer->SetColorWindow(2000);
> > > > >   myviewer->SetColorLevel(1500);
> > > > >   myviewer->Render();
> > > >
> > > > }
> > > >
> > > >
> > > > -Yumin
> > > >
> > > > On 12/6/06, Ashish Singh <mrasingh at gmail.com > wrote:
> > > > > Hi,
> > > > >
> > > > > I have a vtk application to display a dicom image.I am trying to
> > create a
> > > > > button on main panel which when clicked opens up the directory
> tree
> > and we
> > > > > can choose the dicom image files to be displayed in this
> application.
> > I am
> > > > > using vtkkwloadsavebutton for this. When I run the application, I
> get
> > an
> > > > > error in the vtkoutput window saying that-'filename was not
> specified
> > or
> > > > > directory doesn't contain dicom images'. I also get the button in
> the
> > main
> > > > > panel which when clicked lets me choose the file that I want to
> load,
> > but
> > > > > nothing happens after I choose the file.
> > > > >
> > > > > I am pasting a part of my code(this is not the complete code) to
> do
> > this,
> > > > > below. Can anyone please tell me how to fix this so that I can
> choose
> > the
> > > > > file using the dialog and get it displayed?
> > > > >
> > > > > Thanks,
> > > > > Ashish
> > > > >
> > > > > --------
> > > > >
> > > > >  vtkKWLoadSaveButton *load_button1 = vtkKWLoadSaveButton::New();
> > > > >   load_button1->SetParent(win->GetMainPanelFrame());
> > > > >   load_button1->Create();
> > > > >   load_button1->SetText("Click to Pick a File");
> > > > >   load_button1->GetLoadSaveDialog()->SaveDialogOff();
> > //
> > > > > load mode
> > > > >   load_button1->GetLoadSaveDialog()->SetFileTypes("{
> > {Dicom
> > > > > Document} {.dcm} }");
> > > > >   app->Script("pack %s -side top -anchor nw -expand n -padx 2
> -pady
> > > > > 2",load_button1->GetWidgetName());
> > > > >
> > > > >    vtkDICOMImageReader
> > *img2=vtkDICOMImageReader::New();
> > > > >
> > > > >
> > img2->SetFileName(load_button1->GetLoadSaveDialog()->GetFileName());
> > > > >    img2->Update();
> > > > >
> > > > >   vtkImageViewer2 * myviewer=vtkImageViewer2::New();
> > > > >   myviewer->SetInput(img2->GetOutput());
> > > > >   myviewer->SetColorWindow(2000);
> > > > >   myviewer->SetColorLevel(1500);
> > > > >   myviewer->Render();
> > > > > -------
> > > > >
> > > > > _______________________________________________
> > > > > KWWidgets mailing list
> > > > > KWWidgets at kwwidgets.org
> > > > >
> > http://public.kitware.com/cgi-bin/mailman/listinfo/kwwidgets
> > > > >
> > > > >
> > > >
> > >
> > >
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20061212/6591bd28/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: IM_00001.dcm
Type: application/octet-stream
Size: 34778 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20061212/6591bd28/attachment.obj>


More information about the vtkusers mailing list