USTK MHD parser example.
This example contains the declaration of a class used to read and write data in a mhd file like:
#include <visp3/core/vpConfig.h>
#include <iostream>
#include <visp3/core/vpDebug.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/io/vpParseArgv.h>
#include <visp3/ustk_core/usImageIo.h>
#include <visp3/ustk_core/usMetaHeaderParser.h>
#include <string>
#define GETOPTARGS "cdo:h"
void usage(const char *name, const char *badparam, const std::string &opath, const std::string &user);
bool getOptions(int argc, const char **argv, std::string &opath, const std::string &user);
void usage(const char *name, const char *badparam, const std::string &opath, const std::string &user)
{
fprintf(stdout, "\n\
Write and read data in a mhd file.\n\
\n\
SYNOPSIS\n\
%s [-o <output image path>] [-h]\n",
name);
fprintf(stdout, "\n\
OPTIONS: Default\n\
-o <output data path> %s\n\
Set data output path.\n\
From this directory, creates the \"%s\"\n\
subdirectory depending on the username, where \n\
rf2D.mhd file is written.\n\
\n\
-h\n\
Print the help.\n\n",
opath.c_str(), user.c_str());
if (badparam) {
fprintf(stderr, "ERROR: \n");
fprintf(stderr, "\nBad parameter [%s]\n", badparam);
}
}
bool getOptions(int argc, const char **argv, std::string &opath, const std::string &user)
{
const char *optarg_;
int c;
while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
switch (c) {
case 'o':
opath = optarg_;
break;
case 'h':
usage(argv[0], NULL, opath, user);
return false;
break;
case 'c':
case 'd':
break;
default:
usage(argv[0], optarg_, opath, user);
return false;
break;
}
}
if ((c == 1) || (c == -1)) {
usage(argv[0], NULL, opath, user);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
return false;
}
return true;
}
int main(int argc, const char **argv)
{
try {
std::string opt_opath;
std::string opath;
std::string filename;
std::string username;
usMetaHeaderParser testReferenceSettings;
std::cout << "-------------------------------------------------------" << std::endl;
std::cout << " testUsImageRf2DMhdParser.cpp" << std::endl << std::endl;
std::cout << " writing and reading ultrasound data using a the US mhd parser" << std::endl;
std::cout << "-------------------------------------------------------" << std::endl;
std::cout << std::endl;
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
opt_opath = "/tmp";
#elif defined(_WIN32)
opt_opath = "C:\\temp";
#endif
vpIoTools::getUserName(username);
if (getOptions(argc, argv, opt_opath, username) == false) {
exit(-1);
}
if (!opt_opath.empty())
opath = opt_opath;
std::string dirname = vpIoTools::createFilePath(opath, username);
if (vpIoTools::checkDirectory(dirname) == false) {
try {
vpIoTools::makeDirectory(dirname);
} catch (...) {
usage(argv[0], NULL, opath, username);
std::cerr << std::endl << "ERROR:" << std::endl;
std::cerr << " Cannot create " << dirname << std::endl;
std::cerr << " Check your -o " << opath << " option " << std::endl;
exit(-1);
}
}
filename = dirname + vpIoTools::path("/") + "rf2d.mhd";
rf2DReference.setScanLineNumber(233);
rf2DReference.setScanLinePitch(0.0145);
rf2DReference.setTransducerRadius(0.554);
rf2DReference.setTransducerConvexity(true);
rf2DReference.setAxialResolution(0.0058);
rf2DReference.setTransmitFrequency(300000);
rf2DReference.setSamplingFrequency(2000000);
rf2DReference.setDepth(rf2DReference.getScanLinePitch() * rf2DReference.getHeight());
std::cout << "Written in " << filename << std::endl;
std::cout << rf2DReference;
filename = dirname + vpIoTools::path("/") + "rf2d.mhd";
std::cout << "Read from " << filename << std::endl;
std::cout << rf2D;
if (rf2D == rf2DReference) {
std::cout << "Test passed !" << std::endl;
return 0;
}
std::cout << "Test failed !" << std::endl;
return 1;
} catch (const vpException &e) {
std::cout << "Catch an exception: " << e.getMessage() << std::endl;
return 1;
}
}
static void read(usImageRF2D< short int > &imageRf2D, const std::string &headerFileName)
static void write(const usImageRF2D< short > &rfImage, const std::string &headerFileName, const std::string &imageExtension2D)
2D Radio Frequence (RF) ultrasound image.