IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
// main.cpp:
// ---------
//
//
// --- IPSDK includes
// ------------------
// used to initialize IPSDK environment
#include <IPSDKCore/Config/LibraryInitializer.h>
// used to manage exceptions possibly thrown by algoritms functions
#include <IPSDKBaseProcessing/Logger/IPSDKBaseProcessingException.h>
// used to catch exceptions potentially thrown by functions loadTiffImageFile and saveTiffImageFile
#include <IPSDKImageFile/Logger/IPSDKImageFileException.h>
// used to read/write an image from/to a TIFF file:
// used to retrieve usual folders (IPSDK temporary folder, root development folder, etc.)
// used to compute the separated bilateral 3d smoothing
// used to compute the Otsu threshold
// used to compte the 3d binary watershed separation
// used to compute the connected component analysis
// used to compute the 3d label contour extraction
// used to compute measures on shapes
// used for saveCsvMeasureFile
// used for create3dInstance
#include <IPSDKBaseShapeAnalysis/Measure/Info/MeasureInfoSet.h>
// used for createMeasureInfo
// used for the getMeasure() method
#include <IPSDKBaseShapeAnalysis/Measure/MeasureSet.h>
// used to specify the maxFeretDiameter measure parameters
#include <IPSDKIPL/IPSDKIPLShapeAnalysis/Measure/Geometry/FormFactor/MaxFeretDiameter/MaxFeretDiameterMsrParams.h>
// used to retrieve the results of the Area3d measure
#include <IPSDKIPL/IPSDKIPLShapeAnalysis/Measure/Geometry/Basic/Area3d/Area3dMsr.h>
// used to display log messages
// --- third-party boost includes
// ------------------------------
// boost/filesystem/*: contains functions and classes providing facilities to
// manipulate files and directories, and associated paths
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>
// boost/program_options/*: contains functions and classes used to manage and
// interpret arguments of command line
#include <boost/program_options/cmdline.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
// --- third-party log4cplus include
// ---------------------------------
// used to add console as output support of logs
#include <log4cplus/consoleappender.h>
// --- STL include
// ---------------
// for std::cout
#include <iostream>
// To deactivate GPU support
using namespace boost::filesystem;
using namespace boost::program_options;
using namespace ipsdk;
using namespace ipsdk::image;
using namespace ipsdk::imaproc;
using namespace ipsdk::sample;
bool
readCmdArguments(int argc, char* argv[],
boost::filesystem::path& inImgFilePath,
boost::filesystem::path& outputReportPath,
ipUInt32& inHalfKnlSize,
ipReal64& inSpaceSigma,
ipReal32& inDilateFactor);
int
main(int argc, char* argv[])
{
// add console appender for application logs
log4cplus::SharedAppenderPtr pConsole(new log4cplus::ConsoleAppender);
log4cplus::Logger::getRoot().addAppender(pConsole);
log4cplus::Logger::getRoot().setLogLevel(log4cplus::INFO_LOG_LEVEL);
// initialize IPSDK environment (first call to be done before calling any
// function or using any entity of IPSDK environment)
switch(initRes.getResult().value()) {
case ipsdk::core::eLibInitStatus::eLIS_Warn:
// IPSDK library is initialized but there were warnings;
// notify the user by displaying a message
% initRes.getMsg());
break;
case ipsdk::core::eLibInitStatus::eLIS_Failed:
// IPSDK library initialization; notify the user and exit
return -1;
break;
default:
break;
}
// Deactivate GPU support
// boost objects, used to store input and output files pathes
boost::filesystem::path inputImgPath, outputReportPath;
ipUInt32 inHalfKnlSize;
ipReal32 inDilateFactor;
ipReal64 inSpaceSigma;
// read program options from command line, and, if appropriate,
// initialize input and output images files paths
if(!readCmdArguments(argc, argv, inputImgPath, outputReportPath, inHalfKnlSize, inSpaceSigma, inDilateFactor)) {
// clear IPSDK environment features; should be called before exiting
// program
return -1;
}
// display a log message in "INFO" level, to notify the user of the current step
% inputImgPath.string());
// declare the variable that will contain the input image, loaded from TIFF file
try {
// read input image from specified path
pInGreyImg = ipsdk::image::file::loadTiffImageFile(inputImgPath, ipsdk::image::file::eTiffDirectoryMode::eTDM_Volume);
// loadTiffImageFile function threw an exception; display error log
// message
% inputImgPath.string() % e.getMsg());
// clear IPSDK environment features; should be called before exiting
// program
// quit the application with an exit code indicating an error
return -1;
}
// display a log message in "INFO" level, to notify the user of the current step
// filtering of input image to smooth defaults
// Define the path to save the output images
const path binImgPath =
getIPSDKDefaultDirectory(eDefaultExternalDirectory::eDED_Tmp) / "Sample";
// display a log message in "INFO" level, to notify the user of the current step
// auto threshold on filtered image
ipsdk::image::file::saveTiffImageFile(binImgPath/"binImg.tif", otsuResult._pOutImg);
// display a log message in "INFO" level, to notify the user of the current step
// automatic separation of binary shapes
ipsdk::imaproc::attr::eWatershedSeparationMode::eWSM_Split);
ipsdk::image::file::saveTiffImageFile(binImgPath/"binSepImg.tif", pBinSepImg);
// display a log message in "INFO" level, to notify the user of the current step
// connected components analysis of separated binary image
ipsdk::image::file::saveTiffImageFile(binImgPath/"labelImg.tif", pLabelImg);
// display a log message in "INFO" level, to notify the user of the current step
// definition of measure set on shapes
createMeasureInfo(pMeasureInfoSet3d, "Area3dMsr");
createMeasureInfo(pMeasureInfoSet3d, "EquivalentRayMsr");
createMeasureInfo(pMeasureInfoSet3d, "MeanMsr");
// display a log message in "INFO" level, to notify the user of the current step
// extract contours from connected component (label) image
// shape analysis computation
ipsdk::shape::analysis::MeasureSetPtr pMeasureSet = ipsdk::imaproc::shape::analysis::shapeAnalysis3d(pInGreyImg, pShape3dColl, pMeasureInfoSet3d);
// display a log message in "INFO" level, to notify the user of the current step
// save report in csv format
ipsdk::shape::analysis::saveCsvMeasureFile(outputReportPath, *pMeasureSet);
// retrieve area measure results
const ipsdk::shape::analysis::MeasureConstPtr& pArea3dOutMsr = pMeasureSet->getMeasure("Area3dMsr");
static_cast<const ipsdk::shape::analysis::ValueMeasureResult<ipReal64>&>(pArea3dOutMsr->getMeasureResult());
// extract associated results collection
const std::vector<ipReal64> areaValues = outAreaMsrResults.getColl();
// access to the first label area
const ipReal64 firstLabelArea = areaValues[1];
// display a log message in "INFO" level, to notify the user of the current step
return 0;
}
bool
readCmdArguments(int argc, char* argv[],
boost::filesystem::path& inputImgPath,
boost::filesystem::path& outputReportPath,
ipUInt32& inHalfKnlSize,
ipReal64& inSpaceSigma,
ipReal32& inDilateFactor)
{
// list of allowed program options
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("inputImg3dFilePath", value<path>(),
"3d input image file path (optional; only TIFF format is accepted)")
("outputReportPath", value<path>(),
"output file path (optional; CSV resulting file path)")
("inHalfKnlSize", value<ipUInt32>(),
"half kernel size (optional; default value = 3)")
("inSpaceSigma", value<ipReal64>(),
"spatial Gaussien standard deviation (optional; default value = 8)")
("inDilateFactor", value<ipReal32>(),
"dilation factor (optional; CSV resulting file 5)")
;
// vm stores options found in command line
boost::program_options::variables_map vm;
try {
boost::program_options::store(parse_command_line(argc, argv, desc), vm);
} catch(const std::exception& e) {
% e.what());
return false;
}
boost::program_options::notify(vm);
// if the user typed "<app_name>.exe --help"...
if (vm.count("help")) {
// ... the application displays help message
std::cout << desc << "\n";
return false;
}
// initialize input image file path with a default file path
inputImgPath = getIPSDKDirectory(eInternalDirectory::eID_Images) / "blobs3d_483x348x31_UInt8.tif";
// if the user specified his own input image file path as argument...
if(vm.count("inputImg3dFilePath"))
// ... replace the default one by his own
inputImgPath = vm["inputImg3dFilePath"].as<path>();
// if the user specified his own output csv file path as argument...
if(vm.count("outputReportPath"))
// ... replace the default one by his own
outputReportPath = vm["outputReportPath"].as<path>();
else {
// ... otherwise, output csv file will be saved in temporary user directory
const path outputDir =
getIPSDKDefaultDirectory(eDefaultExternalDirectory::eDED_Tmp) /
"Sample";
// if output directory does not exist, we try to create it
if(!boost::filesystem::exists(outputDir)) {
try {
boost::filesystem::create_directories(outputDir);
} catch(const std::exception& e) {
% outputDir % e.what());
}
}
// initialize output image file path with a default file path
outputReportPath = outputDir / "quantification3d.csv";
}
// if the user specified his own half kernel size as argument...
inHalfKnlSize = 3;
if(vm.count("inHalfKnlSize"))
// ... replace the default one by his own
inHalfKnlSize = vm["inHalfKnlSize"].as<ipUInt32>();
// if the user specified his own spatial sigma as argument...
inSpaceSigma = 8;
if(vm.count("inSpaceSigma"))
// ... replace the default one by his own
inSpaceSigma = vm["inSpaceSigma"].as<ipReal64>();
// if the user specified his own dilation factor as argument...
inDilateFactor = 5;
if(vm.count("inDilateFactor"))
// ... replace the default one by his own
inDilateFactor = vm["inDilateFactor"].as<ipReal32>();
// check that input image file path exists. If not...
if(!boost::filesystem::exists(inputImgPath)) {
// ... notify the user, and abort
% inputImgPath);
return false;
}
return true;
}