17 #ifndef __itkActiveShapeModelCalculator_txx
18 #define __itkActiveShapeModelCalculator_txx
43 template<
class TImage>
57 binaryFilter->SetOutsideValue( 0.0 );
58 binaryFilter->SetInsideValue( 1.0 );
59 binaryFilter->SetLowerThreshold( m_LowerThreshold );
60 binaryFilter->SetUpperThreshold( 255.0 );
62 gradientFilter->SetInput( m_Image );
63 binaryFilter->SetInput( gradientFilter->GetOutput() );
64 distanceFilter->InputIsBinaryOn();
65 distanceFilter->SetInput( binaryFilter->GetOutput() );
66 distanceFilter->Update();
69 typedef typename Region2DType::SizeType Size2DType;
70 typedef typename Region2DType::IndexType Index2DType;
76 typename Image3DType::ConstPointer inputImage;
79 inputImage = distanceFilter->GetOutput();
80 typename Image3DType::RegionType requestedRegion = inputImage->GetRequestedRegion();
82 unsigned int projectionDirection = 2;
84 unsigned int direction[2];
86 for (i = 0, j = 0; i < 3; ++i )
88 if (i != projectionDirection)
95 index[ direction[0] ] = requestedRegion.GetIndex()[ direction[0] ];
96 index[ 1- direction[0] ] = requestedRegion.GetIndex()[ direction[1] ];
97 size[ direction[0] ] = requestedRegion.GetSize()[ direction[0] ];
98 size[ 1- direction[0] ] = requestedRegion.GetSize()[ direction[1] ];
99 unsigned int slices = requestedRegion.GetSize()[ 2 ];
104 m_NumberOfTrainingImages = slices;
106 region.SetSize( size );
107 region.SetIndex( index );
108 outputImage->SetRegions( region );
109 outputImage->Allocate();
112 inputIt.SetFirstDirection( direction[1] );
113 inputIt.SetSecondDirection( direction[0] );
115 outputIt.GoToBegin();
117 while ( ! outputIt.IsAtEnd() )
119 while ( ! outputIt.IsAtEndOfLine() )
121 outputIt.Set( NumericTraits<unsigned char>::NonpositiveMin() );
128 outputIt.GoToBegin();
130 while( !inputIt.IsAtEnd() )
132 while ( !inputIt.IsAtEndOfSlice() )
134 while ( !inputIt.IsAtEndOfLine() )
136 float valueOutput = outputIt.Get();
137 float valueInput = inputIt.Get();
138 float sum = valueOutput + valueInput;
139 outputIt.Set( static_cast<PixelType> (sum) );
146 outputIt.GoToBegin();
149 outputIt.GoToBegin();
151 while ( ! outputIt.IsAtEnd() )
153 while ( ! outputIt.IsAtEndOfLine() )
155 float valueOutput = outputIt.Get();
156 float mean = valueOutput /
static_cast<float> (slices);
157 outputIt.Set( static_cast<PixelType>(mean) );
164 binaryFilter1->SetOutsideValue( 0 );
165 binaryFilter1->SetInsideValue( 1 );
166 binaryFilter1->SetLowerThreshold( 0.0 );
167 binaryFilter1->SetUpperThreshold( m_UpperThreshold1 );
171 typename PointsContainer::Pointer points = PointsContainer::New();
172 typename PointDataContainer::Pointer pointData = PointDataContainer::New();
174 binaryFilter1->SetInput( outputImage );
175 thinFilter->SetInput( binaryFilter1->GetOutput() );
176 pruneFilter->SetIteration( m_PruneIteration );
178 pruneFilter->SetInput( thinFilter->GetOutput() );
179 pruneFilter->Update();
182 pruneImage = pruneFilter->GetOutput();
183 IteratorType ot( pruneImage, pruneImage->GetRequestedRegion() );
191 while( !ot.IsAtEnd() )
202 for (
unsigned int identifier = 0; identifier<2; identifier++)
204 p[identifier] = position[identifier];
208 points->InsertElement( pointId, p );
209 pointData->InsertElement( pointId, value1 );
222 otNeighbor.SetLocation( position );
223 unsigned int count = 1;
228 if ( otNeighbor.GetPixel(offset1) )
230 position = otNeighbor.
GetIndex(offset1);
231 otNeighbor.SetPixel ( offset1, value0 );
232 for (
unsigned int identifier = 0; identifier<2; identifier++)
234 p[identifier] = position[identifier];
236 points->InsertElement( pointId, p );
237 pointData->InsertElement( pointId, value1 );
240 otNeighbor += offset1;
244 if ( otNeighbor.GetPixel(offset2) )
246 position = otNeighbor.
GetIndex(offset2);
247 otNeighbor.SetPixel ( offset2, value0 );
248 for (
unsigned int identifier = 0; identifier<2; identifier++)
250 p[identifier] = position[identifier];
252 points->InsertElement( pointId, p );
253 pointData->InsertElement( pointId, value1 );
256 otNeighbor += offset2;
260 if ( otNeighbor.GetPixel(offset3) )
262 position = otNeighbor.
GetIndex(offset3);
263 otNeighbor.SetPixel ( offset3, value0 );
264 for (
unsigned int identifier = 0; identifier<2; identifier++)
266 p[identifier] = position[identifier];
268 points->InsertElement( pointId, p );
269 pointData->InsertElement( pointId, value1 );
272 otNeighbor += offset3;
276 if ( otNeighbor.GetPixel(offset4) )
278 position = otNeighbor.
GetIndex(offset4);
279 otNeighbor.SetPixel ( offset4, value0 );
280 for (
unsigned int identifier = 0; identifier<2; identifier++)
282 p[identifier] = position[identifier];
284 points->InsertElement( pointId, p );
285 pointData->InsertElement( pointId, value1 );
288 otNeighbor += offset4;
292 if ( otNeighbor.GetPixel(offset5) )
294 position = otNeighbor.
GetIndex(offset5);
295 otNeighbor.SetPixel ( offset5, value0 );
296 for (
unsigned int identifier = 0; identifier<2; identifier++)
298 p[identifier] = position[identifier];
300 points->InsertElement( pointId, p );
301 pointData->InsertElement( pointId,value1 );
304 otNeighbor += offset5;
308 if ( otNeighbor.GetPixel(offset6) )
310 position = otNeighbor.
GetIndex(offset6);
311 otNeighbor.SetPixel ( offset6,value0 );
312 for (
unsigned int identifier = 0; identifier<2; identifier++)
314 p[identifier] = position[identifier];
316 points->InsertElement( pointId, p );
317 pointData->InsertElement( pointId, value1 );
320 otNeighbor += offset6;
324 if ( otNeighbor.GetPixel(offset7) )
326 position = otNeighbor.
GetIndex(offset7);
327 otNeighbor.SetPixel ( offset7, value0 );
328 for (
unsigned int identifier = 0; identifier<2; identifier++)
330 p[identifier] = position[identifier];
332 points->InsertElement( pointId, p );
333 pointData->InsertElement( pointId, value1 );
336 otNeighbor += offset7;
340 if ( otNeighbor.GetPixel(offset8) )
342 position = otNeighbor.
GetIndex(offset8);
343 otNeighbor.SetPixel ( offset8, value0 );
344 for (
unsigned int identifier = 0; identifier<2; identifier++)
346 p[identifier] = position[identifier];
348 points->InsertElement( pointId, p );
349 pointData->InsertElement( pointId, value1 );
352 otNeighbor += offset8;
363 pointSet->SetPoints( points );
364 pointSet->SetPointData( pointData );
371 position[ 1 ] = (pointSet->GetNumberOfPoints()) - 1;
374 double squareNorm1, squareNorm2;
375 unsigned int pointId2 = 0;
377 m_Queue.push_front(position);
378 while (! m_Queue.empty())
380 current = m_Queue.front();
382 double m_distance = 0;
383 pointSet->GetPoint( current[ 0 ], & p1 );
384 pointSet->GetPoint( current[ 1 ], & p2 );
385 for (
unsigned int identifier = 0; identifier<2; identifier++)
387 v1[ identifier ]= p2[ identifier ] - p1[ identifier ];
391 for( pointId = (current[ 0 ] + 1); pointId < (current[ 1 ] - 1); pointId++)
393 pointSet->GetPoint( pointId, & p2 );
394 for (
unsigned int identifier = 0; identifier < 2; identifier++)
396 v2[ identifier ]= p2[ identifier ] - p1[ identifier ];
399 for (
unsigned int identifier = 0; identifier < 2; identifier++)
403 v3[ 2 ] = (v1[0] * v2[ 1 ]) - (v1[ 1 ] * v2[ 0 ]);
405 double m_temp = squareNorm2 / squareNorm1;
406 if ( m_temp > m_distance)
412 if (m_distance > m_Tolerance)
414 position[ 0 ] = current[ 0 ];
415 position[ 1 ] = pointId2;
416 m_Queue.push_front( position );
417 position[ 0 ] = pointId2;
418 position[ 1 ] = current[ 1 ];
419 m_Queue.push_front( position );
423 position[ 0 ] =
static_cast<unsigned long int> (p1[ 0 ]);
424 position[ 1 ] =
static_cast<unsigned long int> (p1[ 1 ]);
425 m_Queue2.push_back( position );
429 unsigned int numberOfLandmarks =
static_cast<unsigned int> (m_Queue2.size());
434 while (! m_Queue2.empty())
436 current = m_Queue2.front();
437 m_Queue2.pop_front();
438 mv [ 0 ] = current [ 0 ];
439 mv [ 1 ] = current [ 1 ];
440 sampleLandmarks->PushBack( mv );
445 typename VectorType::iterator p6;
447 coordLandmarks.fill(0);
448 m_Means.set_size( 2*numberOfLandmarks );
451 covarianceMatrix.fill(0);
453 identityMatrix.set_identity();
456 binaryFilter2->SetOutsideValue( 0 );
457 binaryFilter2->SetInsideValue( 255 );
458 binaryFilter2->SetLowerThreshold( 0.0 );
459 binaryFilter2->SetUpperThreshold( m_UpperThreshold2 );
460 binaryFilter2->SetInput( distanceFilter->GetOutput() );
462 inputImage2 = binaryFilter2->GetOutput();
463 binaryFilter2->Update();
464 ConstIteratorType constIterator( inputImage2, inputImage2->GetLargestPossibleRegion());
468 int d, dx, dy, sx, sy;
474 for (i = 0; i < slices; i++)
476 for ( j = 0; j < numberOfLandmarks; j++ )
478 mv = sampleLandmarks->GetMeasurementVector(j);
479 position3D [ 0 ] = mv [ 0 ];
480 position3D [ 1 ] = mv [ 1 ];
481 position3D [ 2 ] = i;
482 constIterator.SetIndex(position3D);
483 if (constIterator.Get())
485 v.push_back (mv [ 0 ]);
486 v.push_back (mv [ 1 ]);
490 for (
unsigned int identifier = 0; identifier<2; identifier++)
492 posRight[ identifier ] = mv [ identifier ];
493 posLeft[ identifier ] = mv [ identifier ];
497 dxyRef1 = sampleLandmarks->GetMeasurementVector(1) -
498 sampleLandmarks->GetMeasurementVector(0);
499 dxyRef2 = sampleLandmarks->GetMeasurementVector(numberOfLandmarks - 1) -
500 sampleLandmarks->GetMeasurementVector(0);
502 if (j == numberOfLandmarks - 1)
504 dxyRef1 = sampleLandmarks->GetMeasurementVector(1) -
505 sampleLandmarks->GetMeasurementVector(j);
506 dxyRef2 = sampleLandmarks->GetMeasurementVector(j-1) -
507 sampleLandmarks->GetMeasurementVector(j);
509 if ((j < numberOfLandmarks - 1) && (j > 1))
511 dxyRef1 = sampleLandmarks->GetMeasurementVector(j + 1) -
512 sampleLandmarks->GetMeasurementVector(j);
513 dxyRef2 = sampleLandmarks->GetMeasurementVector(j-1) -
514 sampleLandmarks->GetMeasurementVector(j);
520 dx = dxyRef2[ 1 ] - dxyRef1[ 1 ];
521 dy = dxyRef1[ 0 ] - dxyRef2[ 0 ];
523 ax = vcl_abs(dx) * 2;
524 ay = vcl_abs(dy) * 2;
549 posRight[ 1 ] = posRight[ 1 ] + sy;
550 posLeft[ 1 ] = posLeft[ 1 ] - sy;
553 posRight[ 0 ] = posRight[ 0 ] + sx;
554 posLeft[ 0 ] = posLeft[ 0 ] - sx;
556 position3D [ 0 ] = posRight [ 0 ];
557 position3D [ 1 ] = posRight[ 1 ];
558 position3D [ 2 ] = i;
559 constIterator.SetIndex(position3D);
560 if (constIterator.Get())
563 v.push_back (posRight[ 0 ]);
564 v.push_back (posRight[ 1 ]);
568 position3D [ 0 ] = posLeft [ 0 ];
569 position3D [ 1 ] = posLeft[ 1 ];
570 position3D [ 2 ] = i;
571 constIterator.SetIndex(position3D);
572 if (constIterator.Get())
575 v.push_back (posLeft[ 0 ]);
576 v.push_back (posLeft[ 1 ]);
588 posRight[ 0 ] = posRight[ 0 ] + sx;
589 posLeft[ 0 ] = posLeft[ 0 ] - sx;
592 posRight[ 1 ] = posRight[ 1 ] + sy;
593 posLeft[ 1 ] = posLeft[ 1 ] - sy;
595 position3D [ 0 ] = posRight [ 0 ];
596 position3D [ 1 ] = posRight[ 1 ];
597 position3D [ 2 ] = i;
598 constIterator.SetIndex(position3D);
599 if (constIterator.Get())
602 v.push_back (posRight[ 0 ]);
603 v.push_back (posRight[ 1 ]);
607 position3D [ 0 ] = posLeft [ 0 ];
608 position3D [ 1 ] = posLeft[ 1 ];
609 position3D [ 2 ] = i;
610 constIterator.SetIndex(position3D);
611 if (constIterator.Get())
614 v.push_back (posLeft[ 0 ]);
615 v.push_back (posLeft[ 1 ]);
622 unsigned int row = 0;
623 for (p6 = v.begin(); p6 != v.end(); p6++)
625 coordLandmarks[row][i] = (*p6);
628 v.erase(v.begin(), v.end());
631 for( i = 0; i < slices; i++)
633 for( j = 0; j < 2*numberOfLandmarks; j++)
635 m_Means[j] += coordLandmarks[j][i];
641 for(i = 0; i < 2*numberOfLandmarks; i++)
643 for(j = 0; j < 2*numberOfLandmarks; j++)
645 for(
unsigned int k = 0; k < slices; k++)
647 covarianceMatrix[i][j] += (coordLandmarks[i][k] - m_Means[i]) * (coordLandmarks[j][k] - m_Means[j]);
651 if( ( slices - 1 ) != 0 )
653 covarianceMatrix /= ( slices - 1 );
657 covarianceMatrix.fill(0);
660 vnl_generalized_eigensystem eigenVectors_eigenValues(covarianceMatrix, identityMatrix);
664 eigenValuesFull.flip();
665 double maxVariance = 0.98 * eigenValuesFull.sum();
668 while (temp < maxVariance)
670 temp += eigenValuesFull[count];
673 m_EigenVectors.set_size(eigenVectorsFull.rows(),count);
674 m_EigenVectors = eigenVectorsFull.extract (eigenVectorsFull.rows(),count, 0, 0);
675 m_EigenValues.set_size(count);
676 m_EigenValues = eigenValuesFull.extract(count,0);
688 template<
class TImage>
703 template<
class TImage>
712 return m_EigenValues;
718 template<
class TImage>
727 return m_EigenVectors;