UsTK : Ultrasound ToolKit  version 2.0.1 under development (2025-02-01)
tutorial-volReading.cpp
1 /****************************************************************************
2  *
3  * This file is part of the UsNeedleDetection software.
4  * Copyright (C) 2013 - 2016 by Inria. All rights reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License ("GPL") as
8  * published by the Free Software Foundation, either version 3 of the
9  * License, or (at your option) any later version.
10  * See the file COPYING at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * This software was developed at:
14  * INRIA Rennes - Bretagne Atlantique
15  * Campus Universitaire de Beaulieu
16  * 35042 Rennes Cedex
17  * France
18  * http://www.irisa.fr/lagadic
19  *
20  * If you have questions regarding the use of this file, please contact the
21  * authors at Alexandre.Krupa@inria.fr
22  *
23  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
24  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25  *
26  * Authors:
27  * Pierre Chatelain
28  * Alexandre Krupa
29  *
30  *****************************************************************************/
31 
32 /* -*-c++-*-
33 #----------------------------------------------------------------------------
34 #
35 # Example file for needle detection.
36 #
37 # Pierre Chatelain
38 # July 10, 2015
39 #
40 #----------------------------------------------------------------------------
41 */
42 
43 // visp
44 #include <visp3/core/vpIoTools.h>
45 #include <visp3/core/vpMatrix.h>
46 #include <visp3/gui/vpDisplayD3D.h>
47 #include <visp3/gui/vpDisplayGDI.h>
48 #include <visp3/gui/vpDisplayGTK.h>
49 #include <visp3/gui/vpDisplayOpenCV.h>
50 #include <visp3/gui/vpDisplayX.h>
51 #include <visp3/gui/vpPlot.h>
52 #include <visp3/io/vpImageIo.h>
53 #include <visp3/io/vpParseArgv.h>
54 
55 // ustk
56 #include <visp3/ustk_core/usSequenceReader3D.h>
57 #include <visp3/ustk_needle_detection/usNeedleTrackerSIR2D.h>
58 
59 using namespace std;
60 
61 /* -------------------------------------------------------------------------- */
62 /* COMMAND LINE OPTIONS */
63 /* -------------------------------------------------------------------------- */
64 
65 // List of allowed command line options
66 #define GETOPTARGS "cdo:h"
67 
68 void usage(const char *name, const char *badparam, const std::string &opath, const std::string &user);
69 bool getOptions(int argc, const char **argv, std::string &opath, const std::string &user);
70 
81 void usage(const char *name, const char *badparam, const std::string &opath, const std::string &user)
82 {
83  fprintf(stdout, "\n\
84  Write and read ultrasound sequences in 2d image files, and the associated xml settings file.\n\
85  \n\
86  SYNOPSIS\n\
87  %s [-o <output image path>] [-h]\n",
88  name);
89 
90  fprintf(stdout, "\n\
91  OPTIONS: Default\n\
92  -o <output data path> %s\n\
93  Set data output path.\n\
94  From this directory, creates the \"%s\"\n\
95  subdirectory depending on the username, where \n\
96  sequenceRF2D.xml file is written.\n\
97  \n\
98  -h\n\
99  Print the help.\n\n",
100  opath.c_str(), user.c_str());
101 
102  if (badparam) {
103  fprintf(stderr, "ERROR: \n");
104  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
105  }
106 }
107 
117 bool getOptions(int argc, const char **argv, std::string &opath, const std::string &user)
118 {
119  const char *optarg_;
120  int c;
121  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
122 
123  switch (c) {
124  case 'o':
125  opath = optarg_;
126  break;
127  case 'h':
128  usage(argv[0], NULL, opath, user);
129  return false;
130  break;
131 
132  case 'c':
133  case 'd':
134  break;
135 
136  default:
137  usage(argv[0], optarg_, opath, user);
138  return false;
139  break;
140  }
141  }
142 
143  if ((c == 1) || (c == -1)) {
144  // standalone param or error
145  usage(argv[0], NULL, opath, user);
146  std::cerr << "ERROR: " << std::endl;
147  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
148  return false;
149  }
150 
151  return true;
152 }
153 
154 int main(int argc, const char *argv[])
155 {
156 
157  std::string vol_filename;
158 
159  for (int i = 0; i < argc; i++) {
160  if (std::string(argv[i]) == "--input")
161  vol_filename = std::string(argv[i + 1]);
162  else if (std::string(argv[i]) == "--help") {
163  std::cout << "\nUsage: " << argv[0] << " [--input <file.vol>] [--help]\n" << std::endl;
164  return 0;
165  }
166  }
167 
168  // Get the ustk-dataset package path or USTK_DATASET_PATH environment variable value
169  if (vol_filename.empty()) {
170  std::string env_ipath = us::getDataSetPath();
171  if (!env_ipath.empty())
172  vol_filename = env_ipath + "/vol/01.vol";
173  else {
174  std::cout << "You should set USTK_DATASET_PATH environment var to access to ustk dataset" << std::endl;
175  return 0;
176  }
177  }
178 
180 
181  // usImageIo::read(image, vol_filename);
183  reader.setSequenceFileName(vol_filename);
184 
185  vpImage<unsigned char> firstFrame;
186  char buffer[300];
187 
188  std::string base = "vol%d-frame%d.png";
189 
190  // reading 10th volume with getVolume method, and writing the 1st frame to check it
191  reader.getVolume(image, 10);
192  firstFrame.resize(image.getBModeSampleNumber(), image.getScanLineNumber());
193  for (unsigned int i = 0; i < image.getBModeSampleNumber(); i++) {
194  for (unsigned int j = 0; j < image.getScanLineNumber(); j++) {
195  firstFrame(i, j, image(j, i, 0));
196  }
197  }
198  snprintf(buffer, 300, base.c_str(), 10, 0);
199  vpImageIo::write(firstFrame, buffer);
200 
201  // Reading all the sequence with the reader
202  /*
203  while(!reader.end()) {
204  reader.acquire(image);
205  firstFrame.resize(image.getBModeSampleNumber(), image.getScanLineNumber());
206  for (unsigned int k = 0; k < image.getDimZ(); k++) {
207  for (unsigned int i = 0; i < image.getBModeSampleNumber(); i++) {
208  for (unsigned int j = 0; j < image.getScanLineNumber(); j++) {
209  firstFrame(i, j, image(j, i, k));
210  }
211  }
212  sprintf(buffer, base.c_str(), reader.getImageNumber(), k);
213  vpImageIo::write(firstFrame, buffer);
214  }
215  }*/
216  return 0;
217 }
unsigned int getBModeSampleNumber() const
Reading of sequences of ultrasound images.
void setSequenceFileName(const std::string &sequenceFileName)
void getVolume(usImagePreScan3D< unsigned char > &preScanImage, int volumeNumberInSequence)
unsigned int getScanLineNumber() const
VISP_EXPORT std::string getDataSetPath()
Definition: us.cpp:54