UsTK : Ultrasound ToolKit  version 2.0.1 under development (2025-02-01)
us2DViewer.cpp
1 #include <visp3/ustk_core/usConfig.h>
2 
3 #ifdef USTK_HAVE_VTK_QT
4 
5 #include <visp3/ustk_core/us.h>
6 #include <visp3/ustk_core/usImageIo.h>
7 #include <visp3/ustk_core/usImagePostScan3D.h>
8 #include <visp3/ustk_gui/us2DSceneWidget.h>
9 #include <visp3/ustk_gui/usVTKConverter.h>
10 #include <vtkMetaImageReader.h>
11 
12 int main(int argc, char **argv)
13 {
14  std::string mhd_filename;
15 
16  for (int i = 0; i < argc; i++) {
17  if (std::string(argv[i]) == "--input")
18  mhd_filename = std::string(argv[i + 1]);
19  else if (std::string(argv[i]) == "--help") {
20  std::cout << "\nUsage: " << argv[0] << " [--input <preScan3D.xml>] [--help]\n" << std::endl;
21  return 0;
22  }
23  }
24 
25  // Get the ustk-dataset package path or USTK_DATASET_PATH environment variable value
26  if (mhd_filename.empty()) {
27  std::string env_ipath = us::getDataSetPath();
28  if (!env_ipath.empty())
29  mhd_filename = env_ipath + "/post-scan/3D_mhd/volume.mhd";
30  else {
31  std::cout << "You should set USTK_DATASET_PATH environment var to access to ustk dataset" << std::endl;
32  return 0;
33  }
34  }
35  // QT application
36  QApplication app(argc, argv);
37 
38  // read us image
40  usImageIo::read(postScan3D, mhd_filename);
41  // conversion to vtk format
42  vtkSmartPointer<vtkImageData> vtkImage = vtkSmartPointer<vtkImageData>::New();
43  usVTKConverter::convert(postScan3D, vtkImage);
44 
45  int dims[3];
46  vtkImage->GetDimensions(dims);
47  double spacing[3];
48  vtkImage->GetSpacing(spacing);
49 
50  // set reslice matrix
51  vpHomogeneousMatrix mat;
52  mat.eye();
53  mat.buildFrom(0, 0, 0.05, 0, 0, 0);
54 
55  vtkMatrix4x4 *vtkMat = vtkMatrix4x4::New();
56  usVTKConverter::convert(mat, vtkMat);
57 
58  // setup view widget
59  us2DSceneWidget *scene = new us2DSceneWidget();
60  scene->setImageData(vtkImage);
61  scene->setResliceMatrix(vtkMat);
62  scene->init();
63  scene->show();
64 
65  // extract 2D slice
67  scene->getCurrentSlice(image2D); // We get only the pixel values and pixel size. Transducer settings are not set when
68  // we extract a post-scan 2D slice.
69 
70  // We write it
71  usImageIo::write(image2D, "slice.xml");
72 
73  // wait until user closes the window
74  return app.exec();
75 }
76 #else
77 #include <iostream>
78 
79 int main() { std::cout << "Install vtk with qt4 or qt5 support to run this tutorial." << std::endl; }
80 
81 #endif
Class used to render a 2D slice of a vtkImageData in a vtk scene in a QWidget (based on QVTKWidget)
void getCurrentSlice(usImagePostScan2D< unsigned char > &image2D)
void setImageData(vtkImageData *imageData)
void setResliceMatrix(vtkMatrix4x4 *matrix)
static void read(usImageRF2D< short int > &imageRf2D, const std::string &headerFileName)
Definition: usImageIo.cpp:153
static void write(const usImageRF2D< short > &rfImage, const std::string &headerFileName, const std::string &imageExtension2D)
Definition: usImageIo.cpp:104
static void convert(const usImagePostScan3D< unsigned char > &postScanImage, vtkSmartPointer< vtkImageData > &vtkPostScanImage, vtkSmartPointer< vtkImageImport > importer=NULL)
VISP_EXPORT std::string getDataSetPath()
Definition: us.cpp:54