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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkShapeUniqueLabelMapFilter_h
#define itkShapeUniqueLabelMapFilter_h
#include "itkInPlaceLabelMapFilter.h"
#include "itkShapeLabelObjectAccessors.h"
#include "itkProgressReporter.h"
#include <queue>
#include "itkMath.h"
namespace itk
{
/** \class ShapeUniqueLabelMapFilter
* \brief Remove some pixels in the label object according to the value of their shape attribute to ensure that a pixel is not in to objects
*
* \author Gaetan Lehmann. Biologie du Developpement et de la Reproduction, INRA de Jouy-en-Josas, France.
*
* This implementation was taken from the Insight Journal paper:
* https://hdl.handle.net/1926/584 or
* http://www.insight-journal.org/browse/publication/176
*
* \sa ShapeLabelObject, BinaryShapeOpeningImageFilter, LabelStatisticsOpeningImageFilter
* \ingroup ImageEnhancement MathematicalMorphologyImageFilters
* \ingroup ITKLabelMap
*/
template< typename TImage >
class ITK_TEMPLATE_EXPORT ShapeUniqueLabelMapFilter:
public InPlaceLabelMapFilter< TImage >
{
public:
/** Standard class typedefs. */
typedef ShapeUniqueLabelMapFilter Self;
typedef InPlaceLabelMapFilter< TImage > Superclass;
typedef SmartPointer< Self > Pointer;
typedef SmartPointer< const Self > ConstPointer;
/** Some convenient typedefs. */
typedef TImage ImageType;
typedef typename ImageType::Pointer ImagePointer;
typedef typename ImageType::ConstPointer ImageConstPointer;
typedef typename ImageType::PixelType PixelType;
typedef typename ImageType::IndexType IndexType;
typedef typename ImageType::LabelObjectType LabelObjectType;
typedef typename LabelObjectType::LineType LineType;
typedef typename LabelObjectType::AttributeType AttributeType;
/** ImageDimension constants */
itkStaticConstMacro(ImageDimension, unsigned int,
TImage::ImageDimension);
/** Standard New method. */
itkNewMacro(Self);
/** Runtime information support. */
itkTypeMacro(ShapeUniqueLabelMapFilter,
InPlaceLabelMapFilter);
#ifdef ITK_USE_CONCEPT_CHECKING
// Begin concept checking
/* itkConceptMacro(InputEqualityComparableCheck,
(Concept::EqualityComparable<InputImagePixelType>));
itkConceptMacro(IntConvertibleToInputCheck,
(Concept::Convertible<int, InputImagePixelType>));
itkConceptMacro(InputOStreamWritableCheck,
(Concept::OStreamWritable<InputImagePixelType>));*/
// End concept checking
#endif
/**
* Set/Get the ordering of the objects. By default, the objects with
* a the largest attribute value are kept. If set to true, the filter
* to keeps the object with the smallest attribute instead.
*/
itkGetConstMacro(ReverseOrdering, bool);
itkSetMacro(ReverseOrdering, bool);
itkBooleanMacro(ReverseOrdering);
/**
* Set/Get the attribute to use to select the object to remove. The default
* is "Size".
*/
itkGetConstMacro(Attribute, AttributeType);
itkSetMacro(Attribute, AttributeType);
void SetAttribute(const std::string & s)
{
this->SetAttribute( LabelObjectType::GetAttributeFromName(s) );
}
protected:
ShapeUniqueLabelMapFilter();
~ShapeUniqueLabelMapFilter() ITK_OVERRIDE {}
virtual void GenerateData() ITK_OVERRIDE;
template< typename TAttributeAccessor >
void TemplatedGenerateData(const TAttributeAccessor & accessor)
{
// Allocate the output
this->AllocateOutputs();
// the priority queue to store all the lines of all the objects sorted
typedef typename std::priority_queue< LineOfLabelObject, std::vector< LineOfLabelObject >,
LineOfLabelObjectComparator > PriorityQueueType;
PriorityQueueType priorityQueue;
ProgressReporter progress(this, 0, 1);
// TODO: really report the progress
for ( typename ImageType::Iterator it( this->GetLabelMap() );
! it.IsAtEnd();
++it )
{
LabelObjectType *labelObject = it.GetLabelObject();
// may reduce the number of lines to proceed
labelObject->Optimize();
typename LabelObjectType::ConstLineIterator lit( labelObject );
while( ! lit.IsAtEnd() )
{
priorityQueue.push( LineOfLabelObject(lit.GetLine(), labelObject) );
++lit;
}
// clear the lines to read them later
labelObject->Clear();
// go to the next label
// progress.CompletedPixel();
}
if ( priorityQueue.empty() )
{
// nothing to do
return;
}
typedef typename std::deque< LineOfLabelObject > LinesType;
LinesType lines;
lines.push_back( priorityQueue.top() );
LineOfLabelObject prev = lines.back();
IndexType prevIdx = prev.line.GetIndex();
priorityQueue.pop();
while ( !priorityQueue.empty() )
{
LineOfLabelObject l = priorityQueue.top();
IndexType idx = l.line.GetIndex();
priorityQueue.pop();
bool newMainLine = false;
// don't check dim 0!
for ( unsigned int i = 1; i < ImageDimension; i++ )
{
if ( idx[i] != prevIdx[i] )
{
newMainLine = true;
}
}
if ( newMainLine )
{
// just push the line
lines.push_back(l);
}
else
{
OffsetValueType prevLength = prev.line.GetLength();
OffsetValueType length = l.line.GetLength();
if ( prevIdx[0] + prevLength >= idx[0] )
{
// the lines are overlapping. We need to choose which line to keep.
// the label, the only "attribute" to be guaranteed to be unique, is
// used to choose
// which line to keep. This is necessary to avoid the case where a
// part of a label is over
// a second label, and below in another part of the image.
bool keepCurrent;
typename TAttributeAccessor::AttributeValueType prevAttr = accessor(prev.labelObject);
typename TAttributeAccessor::AttributeValueType attr = accessor(l.labelObject);
// this may be changed to a single boolean expression, but may become
// quite difficult to read
if ( Math::ExactlyEquals(attr, prevAttr) )
{
if ( l.labelObject->GetLabel() > prev.labelObject->GetLabel() )
{
keepCurrent = !m_ReverseOrdering;
}
else
{
keepCurrent = m_ReverseOrdering;
}
}
else
{
if ( attr > prevAttr )
{
keepCurrent = !m_ReverseOrdering;
}
else
{
keepCurrent = m_ReverseOrdering;
}
}
if ( keepCurrent )
{
// keep the current one. We must truncate the previous one to remove
// the
// overlap, and take care of the end of the previous line if it
// extends
// after the current one.
if ( prevIdx[0] + prevLength > idx[0] + length )
{
// the previous line is longer than the current one. Lets take its
// tail and
// add it to the priority queue
IndexType newIdx = idx;
newIdx[0] = idx[0] + length;
OffsetValueType newLength = prevIdx[0] + prevLength - newIdx[0];
priorityQueue.push( LineOfLabelObject(LineType(newIdx, newLength), prev.labelObject) );
}
// truncate the previous line to let some place for the current one
prevLength = idx[0] - prevIdx[0];
if ( prevLength != 0 )
{
lines.back(). line.SetLength(idx[0] - prevIdx[0]);
}
else
{
// length is 0 - no need to keep that line
lines.pop_back();
}
// and push the current one
lines.push_back(l);
}
else
{
// keep the previous one. If the previous line fully overlap the
// current one,
// the current one is fully discarded.
if ( prevIdx[0] + prevLength > idx[0] + length )
{
// discarding the current line - just do nothing
}
else
{
IndexType newIdx = idx;
newIdx[0] = prevIdx[0] + prevLength;
OffsetValueType newLength = idx[0] + length - newIdx[0];
l.line.SetIndex(newIdx);
l.line.SetLength(newLength);
lines.push_back(l);
}
}
}
else
{
// no overlap - things are just fine already
lines.push_back(l);
}
}
// store the current line as the previous one, and go to the next one.
prev = lines.back();
prevIdx = prev.line.GetIndex();
}
// put the lines in their object
for ( size_t i = 0; i < lines.size(); ++i )
{
LineOfLabelObject & l = lines[i];
l.labelObject->AddLine(l.line);
}
// remove objects without lines
typename ImageType::Iterator it( this->GetLabelMap() );
while ( ! it.IsAtEnd() )
{
typename LabelObjectType::LabelType label = it.GetLabel();
LabelObjectType *labelObject = it.GetLabelObject();
if ( labelObject->Empty() )
{
// must increment the iterator before removing the object to avoid
// invalidating the iterator
++it;
this->GetLabelMap()->RemoveLabel(label);
}
else
{
++it;
}
}
}
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
AttributeType m_Attribute;
private:
ITK_DISALLOW_COPY_AND_ASSIGN(ShapeUniqueLabelMapFilter);
bool m_ReverseOrdering;
struct LineOfLabelObject {
typedef typename LabelObjectType::LineType LineType;
LineOfLabelObject(const LineType _line, LabelObjectType *_lo)
{
this->line = _line;
this->labelObject = _lo;
}
LineType line;
LabelObjectType *labelObject;
};
class LineOfLabelObjectComparator
{
public:
bool operator()(const LineOfLabelObject & lla, const LineOfLabelObject & llb)
{
for ( int i = ImageDimension - 1; i >= 0; i-- )
{
if ( lla.line.GetIndex()[i] > llb.line.GetIndex()[i] )
{
return true;
}
else if ( lla.line.GetIndex()[i] < llb.line.GetIndex()[i] )
{
return false;
}
}
return false;
}
};
}; // end of class
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
#include "itkShapeUniqueLabelMapFilter.hxx"
#endif
#endif
|