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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkCoreAtomImageToUnaryCorrespondenceMatrixProcess.txx,v $
Language: C++
Date: $Date: 2004-12-21 22:47:26 $
Version: $Revision: 1.5 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.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 notices for more information.
=========================================================================*/
#ifndef __itkCoreAtomImageToUnaryCorrespondenceMatrixProcess_txx
#define __itkCoreAtomImageToUnaryCorrespondenceMatrixProcess_txx
#include "itkCoreAtomImageToUnaryCorrespondenceMatrixProcess.h"
namespace itk
{
/**
* Constructor.
*/
template< typename TSourceImage >
CoreAtomImageToUnaryCorrespondenceMatrixProcess< TSourceImage >
::CoreAtomImageToUnaryCorrespondenceMatrixProcess()
{
itkDebugMacro(<< "itkCoreAtomImageToUnaryCorrespondenceMatrixProcess::itkCoreAtomImageToUnaryCorrespondenceMatrixProcess() called");
// Setting the output.
CorrespondenceMatrixPointer output;
output = static_cast<typename CoreAtomImageToUnaryCorrespondenceMatrixProcess::CorrespondenceMatrixType*>(this->MakeOutput(0).GetPointer());
this->ProcessObject::SetNumberOfRequiredOutputs(1);
this->ProcessObject::SetNthOutput(0, output.GetPointer());
// Initialize unary metric object.
//m_Metric = UnaryMetricType::New();
// Initialize flag to off by default.
m_OutputPNG = false;
}
/**
* Returns a pointer to the output cast as a Data Object.
*/
template< typename TSourceImage >
typename CoreAtomImageToUnaryCorrespondenceMatrixProcess< TSourceImage >::DataObjectPointer
CoreAtomImageToUnaryCorrespondenceMatrixProcess< TSourceImage >
::MakeOutput(unsigned int)
{
return static_cast<DataObject*>(CorrespondenceMatrixType::New().GetPointer());
}
/**
* GetOutput.
*/
template< typename TSourceImage >
typename CoreAtomImageToUnaryCorrespondenceMatrixProcess< TSourceImage >::CorrespondenceMatrixType*
CoreAtomImageToUnaryCorrespondenceMatrixProcess< TSourceImage >
::GetOutput()
{
if (this->GetNumberOfOutputs() < 1)
{
return 0;
}
return static_cast< CorrespondenceMatrixType * >
(this->ProcessObject::GetOutput(0));
}
/**
*
*/
template< typename TSourceImage >
void
CoreAtomImageToUnaryCorrespondenceMatrixProcess< TSourceImage >
::SetInput1(const TSourceImage * image1 )
{
itkDebugMacro(<< "CoreAtomImageToUnaryCorrespondenceMatrixProcess: Setting first core atom image input")
// Process object is not const-correct so the const casting is required.
SetNthInput(1, const_cast<TSourceImage *>( image1 ) );
}
/**
*
*/
template< typename TSourceImage >
void
CoreAtomImageToUnaryCorrespondenceMatrixProcess< TSourceImage >
::SetInput2(const TSourceImage * image2 )
{
itkDebugMacro(<< "CoreAtomImageToUnaryCorrespondenceMatrixProcess: Setting second core atom image input")
// Process object is not const-correct so the const casting is required.
SetNthInput(0, const_cast<TSourceImage *>( image2 ) );
}
/**
* Get first core atom image input.
*/
template< typename TSourceImage >
TSourceImage *
CoreAtomImageToUnaryCorrespondenceMatrixProcess< TSourceImage >
::GetInput1()
{
// Process object is not const-correct so the const casting is required.
return const_cast<TSourceImage *>(this->GetNthInput(0));
}
/**
* Get second core atom image input.
*/
template< typename TSourceImage >
TSourceImage *
CoreAtomImageToUnaryCorrespondenceMatrixProcess< TSourceImage >
::GetInput2()
{
// Process object is not const-correct so the const casting is required.
return const_cast<TSourceImage *>(this->GetNthInput(1));
}
/**
* Run the unary metric on the two images and create the unary correspondence matrix.
*/
template< typename TSourceImage >
void
CoreAtomImageToUnaryCorrespondenceMatrixProcess< TSourceImage >
::GenerateData()
{
itkDebugMacro(<< "itkCoreAtomImageToUnaryCorrespondenceMatrixProcess::GenerateData() called");
m_Metric = UnaryMetricType::New();
// Pointers to the input core atom images, output matrix object.
m_CoreAtomImageA = dynamic_cast<CoreAtomImageType*>(ProcessObject::GetInput(0));
m_CoreAtomImageB = dynamic_cast<CoreAtomImageType*>(ProcessObject::GetInput(1));
m_CorrespondenceMatrix = dynamic_cast<CorrespondenceMatrixType*>(ProcessObject::GetOutput(0));
m_Rows = m_CoreAtomImageA->GetMedialNodeCount();
m_Columns = m_CoreAtomImageB->GetMedialNodeCount();
if(m_CorrespondenceMatrix->set_size(m_Rows,m_Columns))
{
itkDebugMacro(<< "m_CorrespondenceMatrix resized successfully");
}
else
{
itkDebugMacro(<< "m_CorrespondenceMatrix resize failed");
}
// Here we iterate through all of the core atoms and get their metric values.
// Create iterators that will walk the blox core atom images.
typedef itk::ImageRegionIterator<CoreAtomImageType> BloxIterator;
BloxIterator bloxItA = BloxIterator(m_CoreAtomImageA,
m_CoreAtomImageA->GetRequestedRegion() );
BloxIterator bloxItB = BloxIterator(m_CoreAtomImageB,
m_CoreAtomImageB->GetRequestedRegion() );
int counterA = 0;
int counterB = 0;
double MetricValue;
// Initialize necessary components to write-out a PNG image of correspondance matrix.
// The image only gets created is the flag is set.
typedef unsigned char CorrespondencePixelType;
typedef Image<CorrespondencePixelType, 2> CorrespondenceImageType;
CorrespondenceImageType::IndexType pixelIndex;
CorrespondenceImageType::SizeType size;
size[0] = m_Rows;
size[1] = m_Columns;
CorrespondenceImageType::RegionType region;
CorrespondenceImageType::IndexType index;
index.Fill(0);
region.SetIndex( index );
region.SetSize(size);
CorrespondenceImageType::Pointer CorrespondenceImage;
CorrespondenceImage = CorrespondenceImageType::New();
CorrespondenceImage->SetRegions( region );
CorrespondenceImage->Allocate();
CorrespondenceImage->FillBuffer(0);
typedef ImageFileWriter<CorrespondenceImageType> FileWriterType;
FileWriterType::Pointer imageWriter;
imageWriter = FileWriterType::New();
PNGImageIO::Pointer io;
io = PNGImageIO::New();
// Iterate through nodes in m_CoreAtomImageA (rows).
for ( bloxItA.GoToBegin(); !bloxItA.IsAtEnd(); ++bloxItA)
{
MedialNodeType* pPixelA = &bloxItA.Value();
if( pPixelA->empty() )
continue;
// Iterate through nodes in m_CoreAtomImageB (columns)
for ( bloxItB.GoToBegin(); !bloxItB.IsAtEnd(); ++bloxItB) //iterate through nodes in m_MedialWindowA
{
MedialNodeType* pPixelB = &bloxItB.Value();
if( pPixelB->empty() )
continue;
m_Metric->SetMedialNodes(pPixelA, pPixelB);
m_Metric->Initialize();
MetricValue = m_Metric->GetResult();
//MetricValue = 1;
// Use m_CorrespondenceMatrix->put(row, column, value) to set matrix.
m_CorrespondenceMatrix->put(counterA, counterB, MetricValue);
pixelIndex[0] = counterA;
pixelIndex[1] = counterB;
if(m_OutputPNG)
{
CorrespondenceImage->SetPixel(pixelIndex,
static_cast<CorrespondencePixelType>(100*m_CorrespondenceMatrix->get(counterA,counterB)) );
}
counterB++;
}
counterB = 0;
counterA++;
}
// Write voting PNG image.
if(m_OutputPNG)
{
std::cerr << "WROTE CORRESPONDANCE IMAGE TO CorrespondenceImage1.png" << std::endl;
imageWriter->SetInput(CorrespondenceImage);
imageWriter->SetFileName("CorrespondenceImage1.png");
imageWriter->SetImageIO(io);
imageWriter->Write();
}
itkDebugMacro(<< "Finished CoreAtomImageToUnaryCorrespondenceMatrixProcess\n");
}
/**
* Print Self
*/
template< typename TSourceImage >
void
CoreAtomImageToUnaryCorrespondenceMatrixProcess< TSourceImage >
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
}
} // end namespace
#endif
|