lsrmSegmenter.h 4.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#ifndef __LSRM_SEGMENTER_H
#define __LSRM_SEGMENTER_H
#include "macro-generator.h"
#include "lsrmGraph.h"
#include "lsrmGraphOperations.h"
#include "lsrmGraphToOtbImage.h"

namespace lsrm
{
	template<class TImage, class TNode, class TParam>
	class Segmenter
	{
	public:

		/* Some convenient typedefs */
		
		typedef Segmenter<TImage, TNode, TParam> Self;
		typedef TImage ImageType;
		typedef TNode NodeType;
		typedef TParam ParamType;
		typedef lsrm::Graph<NodeType> GraphType;
		typedef typename GraphType::EdgeType EdgeType;
		typedef GraphOperations<Self> GraphOperatorType;
		typedef typename GraphOperatorType::NodePointerType NodePointerType;
		typedef GraphToOtbImage<GraphType> IOType;
		typedef typename IOType::LabelImageType LabelImageType;

		/* Default constructor and destructor */
		
		Segmenter(){
			this->m_DoBFSegmentation = true;
			this->m_NumberOfIterations = 75;
		};
		~Segmenter(){};

		/*
		 * This method performs the region merging segmentation.
		 */
		virtual void Update() = 0;

		/* methods to overload */

		/*
		 * Given 2 smart adjacent node pointers (boost::shared_ptr), this
		 * method has to compute the merging cost which is coded as a float.
		 *
		 * @params
		 * NodePointerType n1 : Smart pointer to node 1
		 * NodePointerType n2 : Smart pointer to node 2
		 *
		 * @return the merging cost.
		 */
		virtual float ComputeMergingCost(NodePointerType n1, NodePointerType n2) = 0;

		/*
		 * Given 2 smart adjacent node pointers (boost::shared_ptr), this
		 * method merges th node n2 into the node n1 by updating the customized
		 * attributes of the node n1.
		 *
		 * @params
		 * NodePointerType n1 : Smart pointer to node 1
		 * NodePointerType n2 : Smart pointer to node 2
		 *
		 */
		virtual void UpdateSpecificAttributes(NodePointerType n1, NodePointerType n2) = 0;

		/*
		 * Given the input image, this method initializes the
		 * internal and specific attributes of the graph.
		 *
		 * @params
		 * const std::string& inputFileName : input image path
		 */
		virtual void InitFromImage() = 0;

		/* Return the label image */
		inline typename LabelImageType::Pointer GetLabeledClusteredOutput()
			{
				IOType io;
				auto labelImg = io.GetLabelImage(this->m_Graph, this->m_ImageWidth, this->m_ImageHeight);
				return labelImg;
			}
		
84 85 86
		/*
		 * Returns the memory (in bytes) occupied by one node of the graph
		 */
87
		virtual long long unsigned int GetNodeMemory(NodePointerType &node) = 0;
88 89 90 91 92 93 94 95 96 97 98

        /*
         * Returns the memory (in bytes) occupied by the entire graph
         */
        long long unsigned int GetGraphMemory()
        {
          long long unsigned int memory = 0;
          long long unsigned int numberOfMoves = 0;
          for(auto& node : m_Graph.m_Nodes)
            {
            numberOfMoves += node->m_Contour.size();
99
            memory += this->GetNodeMemory(node);
100 101 102 103 104 105
            }
          memory += std::ceil(numberOfMoves / 4);

          return memory;
        }

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
		/* Set methods */
		SetMacro(bool, DoBFSegmentation);
		SetMacro(unsigned int, NumberOfIterations);
		SetMacro(float, Threshold);
		SetMacro(ParamType, Param);
		SetMacro(unsigned int, ImageWidth);
		SetMacro(unsigned int, ImageHeight);
		SetMacro(unsigned int, NumberOfComponentsPerPixel);
		inline void SetInput(TImage * in){ m_InputImage = in;}
		inline bool GetComplete(){ return this->m_Complete;}

		/* Get methods */
		GetMacro(float, Threshold);
		GetMacro(unsigned int, ImageWidth);
		GetMacro(unsigned int, ImageHeight);
		GetMacro(unsigned int, NumberOfComponentsPerPixel);
		GetMacro(unsigned int, NumberOfIterations);
		
		/* Graph */
		GraphType m_Graph;
		
		
	protected:

		/* Boolean indicating if the segmentation procedure is achieved */
		bool m_Complete;
		
		/* Activate the Best Fitting heuristic */
		bool m_DoBFSegmentation;
		
		/* Number of iterations for the Local Mutual Best Fitting segmentation */
		unsigned int m_NumberOfIterations;

		/* Limit threshold for the region merging criterion  */
		float m_Threshold;

		/* Specific parameters required for the region merging criterion */
		ParamType m_Param;

		/* Image information (has to be initialized in the method InitFromImage) */
		unsigned int m_ImageWidth; // Number of columns
		unsigned int m_ImageHeight; // NUmber of rows
		unsigned int m_NumberOfComponentsPerPixel; // Number of spectral bands

		/* Pointer to the input image to segment */
		TImage * m_InputImage;
	};
} // end of namespace lsrm

#endif