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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
|
/*=========================================================================
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.kitware.com/VolViewCopyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/** Generic interface for protocol communication between an ITK filter
and the VolView Plugin Interface */
#ifndef _itkVVSplineBoundedConnectedThreshold_txx
#define _itkVVSplineBoundedConnectedThreshold_txx
#include "vvITKSplineBoundedConnectedThreshold.h"
#include "itkMinimumMaximumImageCalculator.h"
namespace VolView
{
namespace PlugIn
{
/*
* Constructor
*/
template <class TInputPixelType >
SplineBoundedConnectedThreshold<TInputPixelType>
::SplineBoundedConnectedThreshold()
{
const unsigned int numberOfLandmarks = 9; // nodes in the surface
m_ImportFilter = ImportFilterType::New();
m_MedianFilter = MedianFilterType::New();
m_MedianFilter->SetInput( m_ImportFilter->GetOutput() );
itk::Size< 3 > radius;
radius.Fill( 2 ); // process 5x5x5 neighborhoods.
m_MedianFilter->SetRadius( radius );
m_ConnectedThresholdFilter = ConnectedThresholdFilterType::New();
m_ConnectedThresholdFilter->SetInput( m_MedianFilter->GetOutput() );
m_Spline = SplineType::New();
m_SourceLandmarks = PointSetType::New();
m_TargetLandmarks = PointSetType::New();
m_DoSegmentation = false;
m_ProduceDoubleOutput = false;
m_EngraveSurface = false;
this->SetNumberOfPointsAlongColumns( 21 );
this->SetNumberOfPointsAlongRows( 21 );
LandmarkContainerPointer sourceLandmarks = m_SourceLandmarks->GetPoints();
LandmarkContainerPointer targetLandmarks = m_TargetLandmarks->GetPoints();
targetLandmarks->Reserve( numberOfLandmarks );
sourceLandmarks->Reserve( numberOfLandmarks );
}
/*
* Destructor
*/
template <class TInputPixelType >
SplineBoundedConnectedThreshold<TInputPixelType>
::~SplineBoundedConnectedThreshold()
{
}
/*
* Set the number of points along the colums
*/
template <class TInputPixelType >
void
SplineBoundedConnectedThreshold<TInputPixelType>
::SetNumberOfPointsAlongColumns( unsigned int num )
{
m_SidePointsCol = num;
}
/*
* Set the number of points along the rows
*/
template <class TInputPixelType >
void
SplineBoundedConnectedThreshold<TInputPixelType>
::SetNumberOfPointsAlongRows( unsigned int num )
{
m_SidePointsRow = num;
}
/*
* Set the stiffness value that allows to make the surface move from
* interpolation (passing through the landmarks) to approximation (not
* touching the landmarks).
*/
template <class TInputPixelType >
void
SplineBoundedConnectedThreshold<TInputPixelType>
::SetStiffness( double stiffness )
{
m_Spline->SetStiffness( stiffness );
}
/*
* Set whether the filter should produce a double output or not.
*/
template <class TInputPixelType >
void
SplineBoundedConnectedThreshold<TInputPixelType>
::SetProduceDoubleOutput( bool doubleOutput )
{
m_ProduceDoubleOutput = doubleOutput;
}
/*
* Set whether the filter should just engrave the surface on the image pixels.
*/
template <class TInputPixelType >
void
SplineBoundedConnectedThreshold<TInputPixelType>
::SetEngraveSurface( bool engraveSurface )
{
m_EngraveSurface = engraveSurface;
}
/*
* Set whether the filter should do segmentation with confidence connected or not
*/
template <class TInputPixelType >
void
SplineBoundedConnectedThreshold<TInputPixelType>
::SetDoSegmentation( bool doSegmentation )
{
m_DoSegmentation = doSegmentation;
}
/*
* Get the pointer to the ConnectedThreshold filter
*/
template <class TInputPixelType >
typename SplineBoundedConnectedThreshold<TInputPixelType>::ConnectedThresholdFilterType *
SplineBoundedConnectedThreshold<TInputPixelType>
::GetFilter()
{
return m_ConnectedThresholdFilter;
}
/*
* Performs the actual filtering on the data
*/
template <class TInputPixelType >
void
SplineBoundedConnectedThreshold<TInputPixelType>
::ProcessData( const vtkVVProcessDataStruct * pds )
{
this->SetUpdateMessage("Computing Surface Spline...");
vtkVVPluginInfo * info = this->GetPluginInfo();
const unsigned int numberOfMarkersGroups = info->NumberOfMarkersGroups;
if( numberOfMarkersGroups < 3 )
{
info->SetProperty( info, VVP_ERROR, "This plugin requires you to provide at 2 groups of markers in addition to the Default group" );
return;
}
// Names of the three expected groups
std::string surfaceName = "Surface";
std::string seedsName = "Seeds";
unsigned int seedsGroupId =0;
unsigned int surfaceGroupId =0;
for(unsigned int gid=0; gid < numberOfMarkersGroups; ++gid)
{
if( seedsName == info->MarkersGroupName[gid] )
{
seedsGroupId = gid;
continue;
}
if( surfaceName == info->MarkersGroupName[gid] )
{
surfaceGroupId = gid;
continue;
}
}
if( seedsGroupId == 0 )
{
info->SetProperty( info, VVP_ERROR, "The groups of markers labeled 'Seeds' is missing" );
return;
}
if( surfaceGroupId == 0 )
{
info->SetProperty( info, VVP_ERROR, "The groups of markers labeled 'Surface' is missing" );
return;
}
SizeType size;
IndexType start;
double origin[3];
double spacing[3];
size[0] = info->InputVolumeDimensions[0];
size[1] = info->InputVolumeDimensions[1];
size[2] = info->InputVolumeDimensions[2];
for(unsigned int i=0; i<3; i++)
{
origin[i] = info->InputVolumeOrigin[i];
spacing[i] = info->InputVolumeSpacing[i];
start[i] = 0;
}
RegionType region;
region.SetIndex( start );
region.SetSize( size );
m_ImportFilter->SetSpacing( spacing );
m_ImportFilter->SetOrigin( origin );
m_ImportFilter->SetRegion( region );
const unsigned int totalNumberOfPixels = region.GetNumberOfPixels();
const bool importFilterWillDeleteTheInputBuffer = false;
const unsigned int numberOfPixelsPerSlice = size[0] * size[1];
InputPixelType * dataBlockStart =
static_cast< InputPixelType * >( pds->inData )
+ numberOfPixelsPerSlice * pds->StartSlice;
m_ImportFilter->SetImportPointer( dataBlockStart,
totalNumberOfPixels,
importFilterWillDeleteTheInputBuffer );
// Execute the filters and progressively remove temporary memory
this->SetCurrentFilterProgressWeight( 0.1 );
this->SetUpdateMessage("Preprocessing: Spline Surface...");
const unsigned int numberOfMarkers = info->NumberOfMarkers;
const MarkersGroupIdType * markersGroupId = info->MarkersGroupId;
// Recover the Seeds.
// For this we use the group Id of the markers group "Seeds".
unsigned int numberOfSeeds = 0;
IndexType seed;
for( unsigned int m=0; m<numberOfMarkers; ++m)
{
if( markersGroupId[m] == seedsGroupId )
{
VolView::PlugIn::FilterModuleBase::Convert3DMarkerToIndex( info, m, seed );
m_ConnectedThresholdFilter->AddSeed( seed );
numberOfSeeds++;
}
}
if( !numberOfSeeds )
{
info->SetProperty( info, VVP_ERROR, "The groups of markers labeled 'Seeds' does not have any seeds" );
return;
}
LandmarkContainerPointer targetLandmarks = m_TargetLandmarks->GetPoints();
LandmarkIterator targetLandmarkItr = targetLandmarks->Begin();
LandmarkType landmark;
unsigned int node = 0;
unsigned int landmarkId = 0;
const MarkersCoordinatesType * markersCoordinates = info->Markers;
// Load first the landmarks of the "Surface" group.
unsigned int numberOfSurfacePoints = 0;
for( unsigned int k=0; k<numberOfMarkers; k++ )
{
if( markersGroupId[k] == surfaceGroupId )
{
landmark[0] = *markersCoordinates++;
landmark[1] = *markersCoordinates++;
landmark[2] = *markersCoordinates++;
m_GridNodes[node++] = landmark;
targetLandmarks->InsertElement( landmarkId++, landmark );
numberOfSurfacePoints++;
}
else
{
markersCoordinates += 3;
}
}
if( !numberOfSurfacePoints )
{
info->SetProperty( info, VVP_ERROR, "The groups of markers labeled 'Surface' should have 9 markers" );
return;
}
// Set the Source landmaks. These are defined roughly in the
// plane of the target landmark, using three of the corners
// in the 3x3 grid. This nodes are defined by node[0], node[2] and node[6]
LandmarkContainerPointer sourceLandmarks = m_SourceLandmarks->GetPoints();
LandmarkIterator sourceLandmarkItr = sourceLandmarks->Begin();
VectorType Vx = m_GridNodes[2] - m_GridNodes[0];
VectorType Vy = m_GridNodes[6] - m_GridNodes[0];
sourceLandmarkItr.Value() = m_GridNodes[0];
++sourceLandmarkItr;
sourceLandmarkItr.Value() = m_GridNodes[0] + ( Vx / 2.0 );
++sourceLandmarkItr;
sourceLandmarkItr.Value() = m_GridNodes[2];
++sourceLandmarkItr;
sourceLandmarkItr.Value() = m_GridNodes[0] + ( Vy / 2.0 );
++sourceLandmarkItr;
sourceLandmarkItr.Value() = m_GridNodes[0] + ( ( Vy / 2.0 ) + (Vx / 2.0) );
++sourceLandmarkItr;
sourceLandmarkItr.Value() = m_GridNodes[2] + ( Vy / 2.0 );
++sourceLandmarkItr;
sourceLandmarkItr.Value() = m_GridNodes[6];
++sourceLandmarkItr;
sourceLandmarkItr.Value() = m_GridNodes[6] + ( Vx / 2.0 );
++sourceLandmarkItr;
sourceLandmarkItr.Value() = m_GridNodes[6] + Vx;
// This should actually be a covariant vector.
m_CannonicalNormal = CrossProduct( Vx, Vy );
m_CannonicalOrigin = m_GridNodes[0];
if( m_EngraveSurface )
{
this->SetUpdateMessage("Engraving surface...");
this->EngraveSurfaceIntoImage( pds );
}
else
{
this->SetUpdateMessage("Generating surface...");
this->SetCurrentFilterProgressWeight( 0.9 );
this->SetNumberOfPointsAlongColumns( 21 );
this->SetNumberOfPointsAlongRows( 21 );
this->GenerateSurfacePoints( pds );
this->PostProcessData( pds );
}
} // end of ProcessData
/*
* Performs the actual filtering on the data
*/
template <class TInputPixelType >
void
SplineBoundedConnectedThreshold<TInputPixelType>
::GenerateSurfacePoints( const vtkVVProcessDataStruct * pds )
{
m_Spline->SetTargetLandmarks( m_TargetLandmarks );
m_Spline->SetSourceLandmarks( m_SourceLandmarks );
m_Spline->ComputeWMatrix();
VectorType Vx = m_GridNodes[2] - m_GridNodes[0];
VectorType Vy = m_GridNodes[6] - m_GridNodes[0];
// Create the set of source points. These are the cannonical
// positions of the points forming the surface for visualization
m_SourcePoints.clear();
for(unsigned int row=0; row < m_SidePointsRow; row++ )
{
double factorX = static_cast<float>( row ) /
static_cast<float>( m_SidePointsRow-1 );
PointType pointBase = m_GridNodes[0] + ( Vx * factorX );
for(unsigned int col=0; col< m_SidePointsCol; col++ )
{
double factorY = static_cast<float>( col ) /
static_cast<float>( m_SidePointsCol-1 );
PointType point = pointBase + ( Vy * factorY );
m_SourcePoints.push_back( point );
}
}
// Map the source points throught the transform in order
// to obtain the actual points in the surface.
m_TargetPoints.clear();
PointIterator sourcePointItr = m_SourcePoints.begin();
PointType destination;
while( sourcePointItr != m_SourcePoints.end() )
{
destination = m_Spline->TransformPoint( *sourcePointItr );
m_TargetPoints.push_back( destination );
++sourcePointItr;
}
}
/*
* Performs post-processing of data.
* This involves an intensity window operation and
* data copying into the volview provided buffer.
*/
template <class TInputPixelType >
void
SplineBoundedConnectedThreshold<TInputPixelType>
::PostProcessData( const vtkVVProcessDataStruct * pds )
{
// A change in ProcessData signature could prevent this const_cast...
vtkVVProcessDataStruct * opds = const_cast<vtkVVProcessDataStruct *>( pds );
vtkVVPluginInfo * info = this->GetPluginInfo();
// now put the results into the data structure
const unsigned int numberOfPoints = m_SidePointsCol * m_SidePointsRow;
opds->NumberOfMeshPoints = numberOfPoints;
float * points = new float[ numberOfPoints * 3 ];
opds->MeshPoints = points;
float * outputPointsItr = points;
ConstPointIterator pointItr = m_TargetPoints.begin();
ConstPointIterator pointsEnd = m_TargetPoints.end();
while( pointItr != pointsEnd )
{
*outputPointsItr++ = (*pointItr)[0];
*outputPointsItr++ = (*pointItr)[1];
*outputPointsItr++ = (*pointItr)[2];
++pointItr;
}
opds->NumberOfMeshCells = ( m_SidePointsRow - 1 ) * ( m_SidePointsCol - 1 );
// Cell connectivity entries follow the format of vtkCellArray:
// n1, id1, id2,... idn1, n2, id1, id2,.. idn2....
unsigned int numEntries = opds->NumberOfMeshCells * 5; // each cell id + 4 points ids.
int * cellsTopology = new int [ numEntries ];
opds->MeshCells = cellsTopology;
int * cellsTopItr = cellsTopology;
for(unsigned int row=0; row < m_SidePointsRow - 1 ; row++ )
{
for(unsigned int col=0; col < m_SidePointsCol - 1; col++ )
{
const unsigned int cornerPoint = row * m_SidePointsCol + col;
const unsigned int pointId1 = cornerPoint;
const unsigned int pointId2 = cornerPoint + 1;
const unsigned int pointId3 = cornerPoint + 1 + m_SidePointsCol;
const unsigned int pointId4 = cornerPoint + m_SidePointsCol;
*cellsTopItr++ = 4;
*cellsTopItr++ = pointId1;
*cellsTopItr++ = pointId2;
*cellsTopItr++ = pointId3;
*cellsTopItr++ = pointId4;
}
}
// return the polygonal data
info->AssignPolygonalData(info, opds);
// clean up
delete [] cellsTopology;
delete [] points;
} // end of PostProcessData
/** Set the pixels over the surface to the minimum value of the image.
* This is called here 'engraving' the surface into the image. */
template <class TInputPixelType >
void
SplineBoundedConnectedThreshold<TInputPixelType>
::EngraveSurfaceIntoImage( const vtkVVProcessDataStruct * pds )
{
this->SetNumberOfPointsAlongColumns( 200 );
this->SetNumberOfPointsAlongRows( 200 );
this->GenerateSurfacePoints( pds );
// Copy the data (with casting) to the output buffer provided by the PlugIn API
typedef InputImageType EngravedImageType;
typedef typename EngravedImageType::PixelType MaskedPixelType;
typename EngravedImageType::Pointer engravedImage = EngravedImageType::New();
m_MedianFilter->Update();
typename InputImageType::ConstPointer inputImage = m_MedianFilter->GetOutput();
engravedImage->SetRegions( inputImage->GetBufferedRegion() );
engravedImage->SetSpacing( inputImage->GetSpacing() );
engravedImage->SetOrigin( inputImage->GetOrigin() );
engravedImage->Allocate();
typedef itk::ImageRegionIterator< EngravedImageType > EngravedIteratorType;
EngravedIteratorType et( engravedImage, engravedImage->GetBufferedRegion() );
et.GoToBegin();
typedef itk::ImageRegionConstIterator< InputImageType > InputIteratorType;
typename InputImageType::RegionType region = inputImage->GetBufferedRegion();
InputIteratorType it( inputImage, region );
it.GoToBegin();
while( !et.IsAtEnd() )
{
et.Set( it.Get() );
++et;
++it;
}
typedef itk::MinimumMaximumImageCalculator< InputImageType > CalculatorType;
typename CalculatorType::Pointer minimumCalculator = CalculatorType::New();
minimumCalculator->SetImage( inputImage );
minimumCalculator->ComputeMinimum();
TInputPixelType engravingValue = minimumCalculator->GetMinimum();
// Visit all the surface points and engrave them in the image
PointIterator targetPointItr = m_TargetPoints.begin();
PointIterator targetPointEnd = m_TargetPoints.end();
while( targetPointItr != targetPointEnd )
{
IndexType index;
engravedImage->TransformPhysicalPointToIndex( *targetPointItr, index );
if( region.IsInside( index ) )
{
engravedImage->SetPixel( index, engravingValue );
}
++targetPointItr;
}
if( m_DoSegmentation )
{
m_ConnectedThresholdFilter->SetInput( engravedImage );
m_ConnectedThresholdFilter->Update();
typename OutputImageType::ConstPointer outputImage =
m_ConnectedThresholdFilter->GetOutput();
typedef itk::ImageRegionConstIterator< OutputImageType > OutputIteratorType;
OutputIteratorType ot( outputImage, outputImage->GetBufferedRegion() );
ot.GoToBegin();
if( m_ProduceDoubleOutput )
{
it.GoToBegin(); // restart the loop.
// When producing composite output, the output image must have the same
// type as the input image. Therefore, we use InputPixelType instead of
// OutputPixelType.
InputPixelType * outData = (InputPixelType *)(pds->outData);
vtkVVPluginInfo * info = this->GetPluginInfo();
InputPixelType maxFromInput =
static_cast< InputPixelType >(
atof( this->GetInputVolumeScalarMaximum( info ) ) );
InputPixelType minFromInput =
static_cast< InputPixelType >(
atof( this->GetInputVolumeScalarMinimum( info ) ) );
const OutputPixelType outsideSegmentedRegion =
itk::NumericTraits< OutputPixelType >::Zero;
typename InputImageType::ConstPointer inputImage = m_ImportFilter->GetOutput();
typename InputImageType::RegionType region = inputImage->GetBufferedRegion();
InputIteratorType it( inputImage, region );
it.GoToBegin();
while( !ot.IsAtEnd() )
{
*outData = it.Get(); // copy input pixel
++outData;
if( ot.Get() != outsideSegmentedRegion ) // copy segmented pixel
{
*outData = maxFromInput;
}
else
{
*outData = minFromInput;
}
++outData;
++ot;
++it;
}
}
else
{
OutputPixelType * outData = (OutputPixelType *)(pds->outData);
while( !ot.IsAtEnd() )
{
*outData = ot.Get(); // copy output pixel
++outData;
++ot;
}
}
}
else
{
// Now copy this into the output to return to VolView
MaskedPixelType * outData = (MaskedPixelType *)(pds->outData);
et.GoToBegin();
while( !et.IsAtEnd() )
{
*outData = et.Get(); // copy output pixel
++outData;
++et;
}
}
}
} // end of namespace PlugIn
} // end of namespace Volview
#endif
|