grmSpringSegmenter.txx 1.76 KB
Newer Older
Pierre Lassalle's avatar
Pierre Lassalle committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*=========================================================================

  Program: Generic Region Merging Library
  Language: C++
  author: Lassalle Pierre
  contact: lassallepierre34@gmail.com



  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
Pierre Lassalle's avatar
Pierre Lassalle committed
18 19
#ifndef GRM_SPRING_SEGMENTER_TXX
#define GRM_SPRING_SEGMENTER_TXX
Pierre Lassalle's avatar
Pierre Lassalle committed
20 21
#include <otbImageFileReader.h>
#include <itkImageRegionIterator.h>
Pierre Lassalle's avatar
Pierre Lassalle committed
22
#include "grmSpringSegmenter.h"
Pierre Lassalle's avatar
Pierre Lassalle committed
23 24 25 26 27 28

namespace grm
{

	template<class TImage>
	void
29
	SpringSegmenter<TImage>::UpdateSpecificAttributes(NodePointerType n, PixelType p)
Pierre Lassalle's avatar
Pierre Lassalle committed
30
	{
31
			n->m_Means.reserve(p.GetSize());
Pierre Lassalle's avatar
Pierre Lassalle committed
32

33
			for(std::size_t b = 0; b < p.GetSize(); ++b)
Pierre Lassalle's avatar
Pierre Lassalle committed
34
			{
35
				n->m_Means.push_back(p[b]);
Pierre Lassalle's avatar
Pierre Lassalle committed
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
			}	
	}

	template<class TImage>
	float
	SpringSegmenter<TImage>::ComputeMergingCost(NodePointerType n1, NodePointerType n2)
	{
		float eucDist = 0.0;

		for(unsigned int b = 0; b < this->m_NumberOfComponentsPerPixel; b++)
		{
			eucDist += (n1->m_Means[b] - n2->m_Means[b])*(n1->m_Means[b] - n2->m_Means[b]);
		}

		return (static_cast<float>(std::sqrt(eucDist)));
	}

	template<class TImage>
	void
	SpringSegmenter<TImage>::UpdateSpecificAttributes(NodePointerType n1, NodePointerType n2)
	{
		const float a1 = static_cast<float>(n1->m_Area);
		const float a2 = static_cast<float>(n2->m_Area);
		const float a_sum = a1 + a2;

		for(unsigned int b = 0; b < this->m_NumberOfComponentsPerPixel; ++b)
		{
			n1->m_Means[b] = (a1 * n1->m_Means[b] + a2 * n2->m_Means[b]) / a_sum;
		}
	}
} // end of namespace grm
#endif