IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
KMeansImgLvl1.h
1 // KMeansImgLvl1.h:
3 // ----------------
4 //
14 
15 #ifndef __IPSDKIPLCLASSIFICATION_KMEANSIMGLVL1_H__
16 #define __IPSDKIPLCLASSIFICATION_KMEANSIMGLVL1_H__
17 
19 #include <IPSDKIPL/IPSDKIPLClassification/Processor/KMeansAssignImg/KMeansAssignImgLvl1.h>
20 #include <IPSDKIPL/IPSDKIPLClassification/Processor/KMeansPPClusterInit/KMeansPPClusterInitLvl1.h>
21 #include <IPSDKIPL/IPSDKIPLClassification/Processor/KMeansComputeCompactness/KMeansComputeCompactnessLvl1.h>
25 #include <IPSDKIPL/IPSDKIPLUtility/Processor/CopyImg/CopyImgLvl1.h>
26 
27 // Attributes headers
28 #include <IPSDKIPL/IPSDKIPLAttributes/Attribute/DataItem/InOutOptClustersPptiesColl.h>
29 #include <IPSDKIPL/IPSDKIPLAttributes/Attribute/DataItem/OutKMeansAssignmentInfo.h>
30 #include <IPSDKIPL/IPSDKIPLAttributes/DataItem/ClusterEltInfo.h>
31 #include <IPSDKIPL/IPSDKIPLAttributes/DataItem/Doubles.h>
32 #include <IPSDKIPL/IPSDKIPLAttributes/DataItem/UIntegers64.h>
33 #include <IPSDKIPL/IPSDKIPLAttributes/Attribute/Image/Input/InHomogeneousImg.h>
34 #include <IPSDKIPL/IPSDKIPLAttributes/Attribute/Image/Output/OutClassImg.h>
35 #include <IPSDKIPL/IPSDKIPLAttributes/Attribute/Image/Output/Optional/OutOptPosterizedImg.h>
36 #include <IPSDKIPL/IPSDKIPLAttributes/Attribute/Image/Output/Working/Optional/OutOptWkClassImg.h>
37 #include <IPSDKIPL/IPSDKIPLAttributes/Attribute/Value/InOptClustersCentersShiftTolerance.h>
38 #include <IPSDKIPL/IPSDKIPLAttributes/Attribute/Value/InNbClusters.h>
39 #include <IPSDKIPL/IPSDKIPLAttributes/Attribute/Value/InOptNbAttempts.h>
40 #include <IPSDKIPL/IPSDKIPLAttributes/Attribute/Value/InOptNbMaxIter.h>
41 #include <IPSDKIPL/IPSDKIPLAttributes/Attribute/Value/OutCompactness.h>
42 #include <IPSDKIPL/IPSDKIPLAttributes/Attribute/Image/Input/Optional/InOptSingleGreyMaskImg.h>
43 
44 namespace ipsdk {
45 namespace imaproc {
46 namespace classif {
47 
48 class KMeansCentersRandomInitLvl1;
49 class LocateMostEccentricClusterEltLvl1;
50 class KMeansPosterizeImgLvl1;
51 
54 
55 // declare processor class
56 IPSDK_DECLARE_LVL1_PROCESSOR(REP_ASYNC1, IPSDKIPLClassification, KMeansImg,
57  ipsdk::imaproc::matchSize(eMatchImageSizeType::eMIST_3d, _pInHomogeneousImg, _pOutClassImg) &&
58  ipsdk::processor::ifIsSet(_pOutOptPosterizedImg,
59  ipsdk::imaproc::matchSizeAndType(_pInHomogeneousImg, _pOutOptPosterizedImg)) &&
60  ipsdk::processor::ifIsSet(_pOutOptWkClassImg,
61  ipsdk::imaproc::matchSizeAndType(_pOutClassImg, _pOutOptWkClassImg)) &&
62  ipsdk::imaproc::customImageProperty(_pOutClassImg, allowedClassImgBuffersFromNbClusters(_pInNbClusters)) &&
63  ipsdk::processor::If(ipsdk::processor::isSet(_pInOptSingleGreyMaskImg),
64  ipsdk::imaproc::matchSize(eMatchImageSizeType::eMIST_3d, _pInHomogeneousImg, _pInOptSingleGreyMaskImg),
66 
67  (((ipsdk)(imaproc)(attr))(InHomogeneousImg))
68  (((ipsdk)(imaproc)(attr))(InOptSingleGreyMaskImg))
69  (((ipsdk)(imaproc)(attr))(InOptNbAttempts))
70  (((ipsdk)(imaproc)(attr))(InOptNbMaxIter))
71  (((ipsdk)(imaproc)(attr))(InNbClusters))
72  (((ipsdk)(imaproc)(attr))(InOptClustersCentersShiftTolerance))
73  (((ipsdk)(imaproc)(attr))(OutClustersCenters)(allocate))
74  (((ipsdk)(imaproc)(attr))(OutCompactness))
75  (((ipsdk)(imaproc)(attr))(OutOptWkClassImg)(customOutput)(outputKMeans(_pOutClassImg, _pInHomogeneousImg, _pInNbClusters)))
76  (((ipsdk)(imaproc)(attr))(OutOptPosterizedImg))
77  (((ipsdk)(imaproc)(attr))(OutClassImg)(customOutput)(outputKMeans(_pInHomogeneousImg, _pInNbClusters))))
78 
79 // methods
80 public:
81 
82 protected:
83 
84  void finalizeClustersCenters();
85  void computeNewCenters();
86  bool checkCentersShift();
87 
88  processor::RetrievalResult assignClasses(
89  const core::BasePriority& priority,
90  core::ActionProviderPtr& pProvider);
91 
92  processor::RetrievalResult updateCenters(
93  const core::BasePriority& priority,
94  core::ActionProviderPtr& pProvider);
95 
96  processor::RetrievalResult checkNoEmptyCluster(
97  const core::BasePriority& priority,
98  core::ActionProviderPtr& pProvider);
99 
100  processor::RetrievalResult initRandomCenters(
101  const core::BasePriority& priority,
102  core::ActionProviderPtr& pProvider);
103 
104  processor::RetrievalResult locateMostEccentricPtInBiggestCluster(
105  const core::BasePriority& priority,
106  core::ActionProviderPtr& pProvider);
107 
108  processor::RetrievalResult replaceEmptyCluster(
109  ipUInt64 emptyClusterIx,
110  const attr::ClusterEltInfo& mostEccentricClusterEltInfo,
111  const core::BasePriority& priority,
112  core::ActionProviderPtr& pProvider);
113 
114  processor::RetrievalResult checkCentersShift(
115  const core::BasePriority& priority,
116  core::ActionProviderPtr& pProvider);
117 
118  processor::RetrievalResult processOnCurAttemptComplete(
119  const core::BasePriority& priority,
120  core::ActionProviderPtr& pProvider);
121 
122  processor::RetrievalResult setCurClassImgAsBest(
123  const core::BasePriority& priority,
124  core::ActionProviderPtr& pProvider);
125 
126  processor::RetrievalResult copyBestClassImgToOutClassImg(
127  const core::BasePriority& priority,
128  core::ActionProviderPtr& pProvider);
129 
130  processor::RetrievalResult posterizeImg(
131  const core::BasePriority& priority,
132  core::ActionProviderPtr& pProvider);
133 
134  processor::RetrievalResult computeCompactnessForCurrentAttempt(
135  const core::BasePriority& priority,
136  core::ActionProviderPtr& pProvider);
137 
138  processor::RetrievalResult compactnessForCurrentAttemptComputed(
139  const core::BasePriority& priority,
140  core::ActionProviderPtr& pProvider,
141  ipReal64 compactness);
142 
143  processor::RetrievalResult provideProcessor(
144  const boost::shared_ptr<core::BaseAction>& pProcessor,
145  const core::BasePriority& priority,
146  core::ActionProviderPtr& pProvider);
147 
148  ipUInt64 findBiggestCluster();
149 
150 // attributes
151 protected:
152  image::ImageConstPtr _pReinterpretedInImg;
153  image::ImagePtr _pReinterpretedOutPosterizedImg;
154 
155  ipUInt32 _iterIx;
156  ipUInt32 _attemptIx;
157 
158  boost::shared_ptr<KMeansAssignImgLvl1> _pKMeansAssignProcessor;
159  boost::shared_ptr<KMeansPPClusterInitLvl1> _pKMeansInitCentersProcessor;
160  boost::shared_ptr<LocateMostEccentricClusterEltLvl1> _pLocateEmptyClusterSubstituteProcessor;
161  boost::shared_ptr<KMeansComputeCompactnessLvl1> _pKMeansComputeCompactnessProcessor;
162  boost::shared_ptr<util::CopyImgLvl1> _pSetCurClassImgAsBestProcessor;
163  boost::shared_ptr<util::CopyImgLvl1> _pCopyBestClassImgToOutClassImgProcessor;
164  boost::shared_ptr<KMeansPosterizeImgLvl1> _pPosterizeProcessor;
165 
166  attr::KMeansAssignmentInfoPtr _pKMeansAssignmentInfo;
167  ipUInt64 _lastEmptyClusterIx;
168  std::vector<ipReal64> _oldFlattenedClustersCenters;
169  std::vector<ipReal64> _flattenedClustersCenters;
170  std::vector<ipReal64> _flattenedClustersStdDeviations;
171 
172  std::vector<ipReal64> _bestFlattenedClustersCenters;
173 
174  ipReal64 _bestCompactness;
175 
176  // declaration of dispatch results collection
178 };
179 
182 
183 } // end of namespace classif
184 } // end of namespace imaproc
185 } // end of namespace ipsdk
186 
187 #endif // __IPSDKIPLCLASSIFICATION_KMEANSIMGLVL1_H__
ProcessingResult< eRetrievalResultType > RetrievalResult
#define IPSDK_DECLARE_LVL1_PROCESSOR(ProcType, libraryName, algoName, RuleString, attributeSeq)
uint64_t ipUInt64
IPSDKBASEPROCESSING_API RulePtr none()
Utility functions for K-Means attribute rules management.
boost::shared_ptr< BaseImage > ImagePtr
double ipReal64
boost::enable_if_c< AttributeType::g_bOptional, RulePtr >::type ifIsSet(const boost::shared_ptr< AttributeType > &pAttribute, const RulePtr &pRuleTrue)
Definition of import/export macro for library.
processor::OutputInitializerPtr customOutput(const boost::weak_ptr< OutputAttributeType > &pOutputAttribute, const CustomImageInfoPtr &pCustomImageInfo)
Utility functions for k-means attribute output initialization management.
boost::shared_ptr< const BaseImage > ImageConstPtr
#define IPSDK_DECLARE_STATIC_RESULT_COLL(AttributeType, CollType)
boost::enable_if_c< AttributeType::g_bOptional, RulePtr >::type isSet(const boost::shared_ptr< AttributeType > &pAttribute)
boost::shared_ptr< BaseActionProvider > ActionProviderPtr
IPSDKIPLCLASSIFICATION_API CustomImageInfoPtr outputKMeans(const ImageAttributeConstPtr &pInputAttribute, const boost::shared_ptr< const attr::InNbClusters > &pNbClustersAttribute)
IPSDKIPLCLASSIFICATION_API CustomCheckedImagePropertyPtr allowedClassImgBuffersFromNbClusters(const boost::shared_ptr< const attr::InNbClusters > &pInNbClustersAttribute)
[Output] information resulting from assignment step in k-means algorithm
Definition: OutKMeansAssignmentInfo.h:45
boost::shared_ptr< ipsdk::imaproc::attr::KMeansAssignmentInfo > KMeansAssignmentInfoPtr
shared pointer to ipsdk::imaproc::attr::KMeansAssignmentInfo
Definition: KMeansAssignmentInfo.h:154
IPSDKBASEPROCESSING_API RulePtr If(const RulePtr &pPredicate, const RulePtr &pRuleTrue)
uint32_t ipUInt32