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 classify the input image using k-means algorithm
#include <IPSDKIPL/IPSDKIPLClassification/Processor/KMeansImg/KMeansImg.h>
// used to unnoise the input image using the median filter
// used to standardize the temporal sequence image
// used to extract Law's texture energy map
// used to concatenate image of intensities and image of local textures into a temporal sequence of images (format accepted by k-means algorithm)
// used to convert input image buffer type into real32, to make sequence image homogeneous
// used to manage exceptions possibly thrown by algoritms functions
#include <IPSDKBaseProcessing/Logger/IPSDKBaseProcessingException.h>
// for BaseImageGeometry
#include <IPSDKImage/Geometry/BaseImageGeometry.h>
// for function "geometry2d"
// for ImageGeometryPtr typedef
// for MemoryImage class
#include <IPSDKImage/Image/Memory/MemoryImage.h>
// used to catch exceptions potentially thrown by functions loadRawImageFile and saveRawImageFile
#include <IPSDKImageFile/Logger/IPSDKImageFileException.h>
// used to read/write an image from/to a RAW 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>
// --- 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 ipsdk;
using namespace ipsdk::image;
using namespace ipsdk::imaproc;
using namespace ipsdk::sample;
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
break;
case ipsdk::core::eLibInitStatus::eLIS_Failed:
// IPSDK library initialization; notify the user and exit
return -1;
break;
default:
break;
}
// boost objects, used to store input and output images files paths
boost::filesystem::path inImgFilePath =
getIPSDKDirectory(eInternalDirectory::eID_Images) / "sampleKMeansInputImage.tif";
"Unequalized_Hawkes_Bay_NZ.tif";
boost::filesystem::path outImgFilePath =
getIPSDKDefaultDirectory(eDefaultExternalDirectory::eDED_Tmp) / "outClassImg.tif";
// display a log message in "INFO" level, to notify the user of the current
// step
% inImgFilePath.string());
// declare the variable that will contain the input image, loaded from
// TIFF file
try {
// read input image from specified path
pInImg =
// loadTiffImageFile function threw an exception; display error log
// message
% inImgFilePath.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
// declare the variable that will contain the output image, resulting from
// boundary extraction
ImagePtr pUnnoisedImg;
try {
// compute boundary image
pUnnoisedImg = ipsdk::imaproc::filter::median2dImg(pInImg, 1, 1);
// median2dImg function threw an exception; display error log
// message
% e.getMsg());
// clear IPSDK environment features; should be called before exiting
// program
// quit the application with an exit code indicating an error
return -1;
}
ImagePtr pLawTexImg;
// initialize Law Texture filter input parameters
// initialize Law Texture filter input pre-process parameters (use default parameters for this one)
// initialize Law Texture filter input post-process parameters (use default parameters for this one)
// initialize Law Texture filter input kernel types
// here, only R5R5 Law's texture energy map interests us, so we reset all flags but R5R5 one (remember, all flags are set to True by default)
pKernelTypes->setValue<attr::LawTextureKernel2dTypes::FlagL5E5_E5L5>(false);
pKernelTypes->setValue<attr::LawTextureKernel2dTypes::FlagL5S5_S5L5>(false);
pKernelTypes->setValue<attr::LawTextureKernel2dTypes::FlagL5R5_R5L5>(false);
pKernelTypes->setValue<attr::LawTextureKernel2dTypes::FlagE5E5>(false);
pKernelTypes->setValue<attr::LawTextureKernel2dTypes::FlagE5S5_S5E5>(false);
pKernelTypes->setValue<attr::LawTextureKernel2dTypes::FlagE5R5_R5E5>(false);
pKernelTypes->setValue<attr::LawTextureKernel2dTypes::FlagS5S5>(false);
pKernelTypes->setValue<attr::LawTextureKernel2dTypes::FlagS5R5_R5S5>(false);
try {
// apply Law's texture filter
pLawTexImg = ipsdk::imaproc::stats::lawTexture2dImg(pInImg, pKernelTypes, pPreProcParams, pPostProcParams);
}
% e.getMsg());
// clear IPSDK environment features; should be called before exiting
// program
// quit the application with an exit code indicating an error
return -1;
}
ImagePtr pSeqImg;
try {
// apply Law's texture filter
pSeqImg = ipsdk::imaproc::util::appendSeqImg(pLawTexImg, util::convertImg(pUnnoisedImg, eImageBufferType::eIBT_Real32));
}
% 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 {
// standardize the sequence image, to give same weight to intensity information and local texture information
}
% e.getMsg());
// clear IPSDK environment features; should be called before exiting
// program
// quit the application with an exit code indicating an error
return -1;
}
boost::shared_ptr<MemoryImage> pOutImg = boost::make_shared<MemoryImage>();
pOutImg->init(pInImg->getGeometry());
try {
// launch classification using k-means algorithm
const ipUInt32 K = 4;
ipsdk::imaproc::classif::kMeansImg(pSeqImg, K, 20, 1000, 0.001, pOutImg);
}
% e.getMsg());
// clear IPSDK environment features; should be called before exiting
// program
// quit the application with an exit code indicating an error
return -1;
}
% outImgFilePath.string());
try {
// save the resulting image in specified path
ipsdk::image::file::saveTiffImageFile(outImgFilePath, pOutImg);
% outImgFilePath % 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 that the job
// is successfully done
// clearing IPSDK environment features; should be called before exiting
// program
return 0;
}
# [DocSampleImports]
import os
import sys
import PyIPSDK
import PyIPSDK.IPSDKIPLClassification as classif # for classif.kMeansImg
import PyIPSDK.IPSDKIPLFiltering as filter # for filter.median2dImg
import PyIPSDK.IPSDKIPLIntensityTransform as itrans # for itrans.standardizeImg
import PyIPSDK.IPSDKIPLStats as stats # for stats.lawTexture2dImg
import PyIPSDK.IPSDKIPLUtility as util # for util.convertImg and util.appendSeqImg
# [DocSampleImports]
def main(argv):
# [DocSampleDefineArguments]
# define input image path
imagesSamplePath = PyIPSDK.getIPSDKDirectory(PyIPSDK.eInternalDirectory.eID_Images)
inputImgPath = os.path.join(imagesSamplePath, "sampleKMeansInputImage.tif")
tmpPath = PyIPSDK.getIPSDKDefaultDirectory(PyIPSDK.eDefaultExternalDirectory.eDED_Tmp)
outputClassImgPath = os.path.join(tmpPath, "outClassImg.tif")
# [DocSampleDefineArguments]
# [DocSampleLoadImage]
# opening of 2D input image
inImg = PyIPSDK.loadTiffImageFile(inputImgPath, PyIPSDK.eTiffDirectoryMode.eTDM_Volume)
# [DocSampleLoadImage]
# [DocUnnoiseImg]
unnoisedImg = filter.median2dImg(inImg, 1, 1)
# [DocUnnoiseImg]
# [DocLawTex]
preProcParams = PyIPSDK.createMeanLawTexPreProcParams(7)
postProcParams = PyIPSDK.createMeanAbsLawTexPostProcParams(7)
# here, only R5R5 Law's texture energy map interests us, so we reset all flags but R5R5 one (remember, all flags are set to True by default)
kernelTypes = PyIPSDK.createLawTextureKernel2dTypes()
kernelTypes.flagL5E5_E5L5 = False
kernelTypes.flagL5S5_S5L5 = False
kernelTypes.flagL5R5_R5L5 = False
kernelTypes.flagE5E5 = False
kernelTypes.flagE5S5_S5E5 = False
kernelTypes.flagE5R5_R5E5 = False
kernelTypes.flagS5S5 = False
kernelTypes.flagS5R5_R5S5 = False
lawTexImg = stats.lawTexture2dImg(inImg, kernelTypes, preProcParams, postProcParams)
# [DocLawTex]
# [DocAppendSeq]
seqImg = util.appendSeqImg(lawTexImg, util.convertImg(unnoisedImg, PyIPSDK.eImageBufferType.eIBT_Real32))
# [DocAppendSeq]
# [DocStandardize]
seqImg = itrans.standardizeImg(seqImg)
# [DocStandardize]
# [DocKMeans]
outClassImg = PyIPSDK.createImage(inImg.getGeometry())
# K: number of expected classes
K = 4
classif.kMeansImg(seqImg, K, 20, 1000, 0.001, outClassImg)
# [DocKMeans]
# [DocSampleSaveImage]
# save generated image
print("Writing result in file : " + outputClassImgPath)
PyIPSDK.saveTiffImageFile(outputClassImgPath, outClassImg)
# [DocSampleSaveImage]
if __name__ == "__main__":
main(sys.argv[1:])