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 process shape 2d filtering
// used to manage exceptions possibly thrown by algorithms 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 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>
using namespace boost::filesystem;
using namespace boost::program_options;
using namespace ipsdk;
using namespace ipsdk::image;
using namespace ipsdk::image::file;
using namespace ipsdk::imaproc;
using namespace ipsdk::sample;
bool
readCmdArguments(int argc, char* argv[],
boost::filesystem::path& inputGreyImgPath,
boost::filesystem::path& inputBinLabImgPath,
std::string& inputFilterFormula,
boost::filesystem::path& outputImgPath);
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;
}
// read program options from command line, and, if appropriate,
// initialize input and output images files and used filtering formula
boost::filesystem::path inputGreyImgPath, inputBinLabImgPath, outputImgPath;
std::string inputFilterFormula;
if(!readCmdArguments(argc, argv, inputGreyImgPath, inputBinLabImgPath, inputFilterFormula, outputImgPath))
return -1;
// display a log message in "INFO" level, to notify the user of the current
// step
% inputGreyImgPath.string());
// opening grey level input image
ImagePtr pInGreyImg = loadTiffImageFile(inputGreyImgPath);
// display a log message in "INFO" level, to notify the user of the current
// step
% inputBinLabImgPath.string());
// opening binary/label input image
ImagePtr pInBinLabImg = loadTiffImageFile(inputBinLabImgPath);
// shape 2d filtering operation
ImagePtr pOutImg;
try {
pOutImg = shapeFiltering2dImg(pInBinLabImg, pInGreyImg, inputFilterFormula);
} catch(const BaseException& e) {
% e.getMsg());
// clear IPSDK environment features; should be called before exiting
// program
// quit the application with an exit code indicating an error
return -1;
}
try {
// save the resulting image in specified path
ipsdk::image::file::saveTiffImageFile(outputImgPath, pOutImg);
% outputImgPath % e.getMsg());
// clear IPSDK environment features; should be called before exiting
// program
// quit the application with an exit code indicating an error
return -1;
}
// clearing IPSDK environment features; should be called before exiting
// program
return 0;
}
bool
readCmdArguments(int argc, char* argv[],
boost::filesystem::path& inputGreyImgPath,
boost::filesystem::path& inputBinLabImgPath,
std::string& inputFilterFormula,
boost::filesystem::path& outputImgPath)
{
// list of allowed program options
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("inputGreyImgPath", value<path>(),
"input grey image file path (optional; only TIFF format is accepted)")
("inputBinLabImgPath", value<path>(),
"input binary/label image file path (optional; only TIFF format is accepted)")
("inputFilterFormula", value<std::string>(),
"input formula string for filtering operation (optional)")
("outputImgPath", value<path>(),
"output result image path (optional)")
;
// 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 grey image file path with a default file path
inputGreyImgPath = getIPSDKDirectory(eInternalDirectory::eID_Images) / "blobs_483x348_UInt8.tif";
// if the user specified his own input image file path as argument...
if(vm.count("inputGreyImgPath"))
// ... replace the default one by his own
inputGreyImgPath = vm["inputGreyImgPath"].as<path>();
// initialize input image file path with a default file path
const boost::filesystem::path binLabRelToRootDir =
path("data") / "Sample" / "images" / "blobs_483x348_Binary.tif";
// initialize input binary image file path with a default file path
inputBinLabImgPath = getProjectRootDirectory() / binLabRelToRootDir;
if(!boost::filesystem::exists(inputBinLabImgPath))
inputBinLabImgPath = getIPSDKRootDirectory() / binLabRelToRootDir;
// if the user specified his own input image file path as argument...
if(vm.count("inputBinLabImgPath"))
// ... replace the default one by his own
inputBinLabImgPath = vm["inputBinLabImgPath"].as<path>();
// initialize input filtering formula
inputFilterFormula = "Area2dMsr > 800";
// if the user specified his own formula as argument...
if(vm.count("inputFilterFormula"))
// ... replace the default one by his own
inputFilterFormula = vm["inputFilterFormula"].as<std::string>();
// if the user specified his own input image file path as argument...
if(vm.count("outputImgFilePath"))
// ... replace the default one by his own
outputImgPath = vm["outputImgFilePath"].as<path>();
else {
// ... otherwise, output image 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
outputImgPath = outputDir / "shapeFiltering2dImg.tif";
}
return true;
}