UsTK : Ultrasound ToolKit  version 2.0.1 under development (2025-02-01)
usFullConverter.cpp
1 #include <visp3/ustk_core/usConfig.h>
2 
3 #ifdef USTK_HAVE_VTK_QT
4 
5 #include <visp3/ustk_core/usImageIo.h>
6 #include <visp3/ustk_core/usPreScanToPostScan3DConverter.h>
7 #include <visp3/ustk_gui/usVTKConverter.h>
8 #include <vtkImageImport.h>
9 #include <vtkMetaImageWriter.h>
10 
11 int main(int argc, char **argv)
12 {
13  std::string mhd_filename;
14 
15  for (int i = 0; i < argc; i++) {
16  if (std::string(argv[i]) == "--input")
17  mhd_filename = std::string(argv[i + 1]);
18  else if (std::string(argv[i]) == "--help") {
19  std::cout << "\nUsage: " << argv[0] << " [--input <preScan3D.xml>] [--help]\n" << std::endl;
20  return 0;
21  }
22  }
23 
24  // Get the ustk-dataset package path or USTK_DATASET_PATH environment variable value
25  if (mhd_filename.empty()) {
26  std::string env_ipath = us::getDataSetPath();
27  if (!env_ipath.empty())
28  mhd_filename = env_ipath + "/pre-scan/3D_xml/sequencepreScan3D.xml";
29  else {
30  std::cout << "You should set USTK_DATASET_PATH environment var to access to ustk dataset" << std::endl;
31  return 0;
32  }
33  }
34 
35  // read 2 us images to simulate double buffer system for real grabbing
36 
37  // 1st us image
40  usImageIo::read(preScanImage, mhd_filename);
41  // 2nd us image
42  usImagePreScan3D<unsigned char> preScanImage2;
43  usImagePostScan3D<unsigned char> postScanImage2;
44  usImageIo::read(preScanImage2, mhd_filename);
45 
46  // scan-conversion
48  double startTime = vpTime::measureTimeMs();
49  std::cout << "init converter..." << std::endl;
50  converter.init(preScanImage);
51  double endInitTime = vpTime::measureTimeMs();
52  std::cout << "init time (sec) = " << (endInitTime - startTime) / 1000.0 << std::endl;
53 
54  // scan-convert to init post-scan images (and not have a NULL pointer given to vtk importer)
55  converter.convert(postScanImage, preScanImage);
56  converter.convert(postScanImage2, preScanImage2);
57 
58  // check addresses of each image
59  std::cout << "image 1 @ : " << (void *)postScanImage.getConstData() << std::endl;
60  std::cout << "image 2 @ : " << (void *)postScanImage2.getConstData() << std::endl;
61 
62  // pre-compute the importer to save time during the data import in vtk
63  // this importer will switch between the 2 previsous usImages at each iteration
64  vtkSmartPointer<vtkImageData> vtkImage = vtkSmartPointer<vtkImageData>::New();
65  vtkImageImport *importer = vtkImageImport::New();
66  importer->SetDataScalarTypeToUnsignedChar();
67  importer->SetImportVoidPointer((void *)postScanImage.getConstData());
68  importer->SetWholeExtent(0, postScanImage.getWidth() - 1, 0, postScanImage.getHeight() - 1, 0,
69  postScanImage.getNumberOfFrames() - 1);
70  importer->SetDataExtentToWholeExtent();
71  importer->SetNumberOfScalarComponents(1);
72 
73  // trying successive conversions to simulate the double buffer system that should be used to grab and display
74  // sucessive volumes
75  for (int i = 0; i < 20; i++) {
76  double t1 = vpTime::measureTimeMs();
77 
78  // Scan-conversion
79  if (i % 2 == 0)
80  converter.convert(postScanImage, preScanImage);
81  else
82  converter.convert(postScanImage2, preScanImage2);
83 
84  double endConvertTime = vpTime::measureTimeMs();
85  std::cout << "scan convert time (ms) = " << (endConvertTime - t1) << std::endl;
86 
87  // vtk conversion
88  if (i % 2 == 0)
89  usVTKConverter::convert(postScanImage, vtkImage, importer);
90  else
91  usVTKConverter::convert(postScanImage2, vtkImage, importer);
92 
93  double t2 = vpTime::measureTimeMs();
94 
95  std::cout << "vtk convert time (ms) = " << t2 - endConvertTime << std::endl;
96  std::cout << "full convert time (ms) = " << t2 - t1 << std::endl;
97  std::cout << "vtk image @ : " << vtkImage->GetScalarPointer() << std::endl;
98  }
99  return 0;
100 }
101 #else
102 #include <iostream>
103 
104 int main() { std::cout << "Install vtk with qt4 or qt5 support to run this tutorial." << std::endl; }
105 
106 #endif
unsigned int getNumberOfFrames() const
Definition: usImage3D.h:137
Type * getConstData() const
Definition: usImage3D.h:104
unsigned int getHeight() const
Definition: usImage3D.h:131
unsigned int getWidth() const
Definition: usImage3D.h:125
static void read(usImageRF2D< short int > &imageRf2D, const std::string &headerFileName)
Definition: usImageIo.cpp:153
void init(const usImagePreScan3D< unsigned char > &preScanImage, double down=1)
void convert(usImagePostScan3D< unsigned char > &postScanImage, const usImagePreScan3D< unsigned char > &preScanImage)
static void convert(const usImagePostScan3D< unsigned char > &postScanImage, vtkSmartPointer< vtkImageData > &vtkPostScanImage, vtkSmartPointer< vtkImageImport > importer=NULL)
VISP_EXPORT std::string getDataSetPath()
Definition: us.cpp:54