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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageAnisotropicDiffusion2D.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/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 notice for more information.
=========================================================================*/
#include "vtkImageAnisotropicDiffusion2D.h"
#include "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include <math.h>
vtkStandardNewMacro(vtkImageAnisotropicDiffusion2D);
//----------------------------------------------------------------------------
// Construct an instance of vtkImageAnisotropicDiffusion2D fitler.
vtkImageAnisotropicDiffusion2D::vtkImageAnisotropicDiffusion2D()
{
this->HandleBoundaries = 1;
this->NumberOfIterations = 0;
this->SetNumberOfIterations(4);
this->DiffusionThreshold = 5.0;
this->DiffusionFactor = 1;
this->Faces = 0;
this->FacesOn();
this->Edges = 0;
this->EdgesOn();
this->Corners = 0;
this->CornersOn();
this->GradientMagnitudeThreshold = 1;
this->GradientMagnitudeThresholdOff();
}
//----------------------------------------------------------------------------
void
vtkImageAnisotropicDiffusion2D::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "NumberOfIterations: " << this->NumberOfIterations << "\n";
os << indent << "DiffusionThreshold: " << this->DiffusionThreshold << "\n";
os << indent << "DiffusionFactor: " << this->DiffusionFactor << "\n";
os << indent << "Faces: " << this->Faces << "\n";
if (this->Edges)
{
os << indent << "Edges: On\n";
}
else
{
os << indent << "Edges: Off\n";
}
if (this->Corners)
{
os << indent << "Corners: On\n";
}
else
{
os << indent << "Corners: Off\n";
}
if (this->GradientMagnitudeThreshold)
{
os << indent << "GradientMagnitudeThreshold: On\n";
}
else
{
os << indent << "GradientMagnitudeThreshold: Off\n";
}
}
//----------------------------------------------------------------------------
// This method sets the number of inputs which also affects the
// input neighborhood needed to compute one output pixel.
void vtkImageAnisotropicDiffusion2D::SetNumberOfIterations(int num)
{
int temp;
vtkDebugMacro(<< "SetNumberOfIterations: " << num);
if (this->NumberOfIterations == num)
{
return;
}
this->Modified();
temp = num*2 + 1;
this->KernelSize[0] = temp;
this->KernelSize[1] = temp;
this->KernelMiddle[0] = num;
this->KernelMiddle[1] = num;
this->NumberOfIterations = num;
}
//----------------------------------------------------------------------------
// This method contains a switch statement that calls the correct
// templated function for the input data type. The input and output datas
// must have the same data type.
void vtkImageAnisotropicDiffusion2D::ThreadedRequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *vtkNotUsed(outputVector),
vtkImageData ***inData,
vtkImageData **outData,
int outExt[6], int id)
{
int inExt[6], wholeExt[6];
double *ar;
int idx;
vtkImageData *temp;
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), wholeExt);
this->InternalRequestUpdateExtent(inExt,outExt,wholeExt);
// this filter expects that input is the same type as output.
if (inData[0][0]->GetScalarType() != outData[0]->GetScalarType())
{
vtkErrorMacro("Execute: input ScalarType, "
<< inData[0][0]->GetScalarType()
<< ", must match out ScalarType "
<< outData[0]->GetScalarType());
return;
}
ar = inData[0][0]->GetSpacing();
// make the temporary regions to iterate over.
vtkImageData *in = vtkImageData::New();
in->SetExtent(inExt);
in->AllocateScalars(VTK_DOUBLE,
inData[0][0]->GetNumberOfScalarComponents());
in->CopyAndCastFrom(inData[0][0],inExt);
vtkImageData *out = vtkImageData::New();
out->SetExtent(inExt);
out->AllocateScalars(VTK_DOUBLE,
inData[0][0]->GetNumberOfScalarComponents());
// Loop performing the diffusion
// Note: region extent could get smaller as the diffusion progresses
// (but never get smaller than output region).
for (idx = this->NumberOfIterations - 1;
!this->AbortExecute && idx >= 0; --idx)
{
if (!id)
{
this->UpdateProgress(static_cast<double>(this->NumberOfIterations - idx)
/this->NumberOfIterations);
}
this->Iterate(in, out, ar[0], ar[1], outExt, idx);
temp = in;
in = out;
out = temp;
}
// copy results into output.
outData[0]->CopyAndCastFrom(in,outExt);
in->Delete();
out->Delete();
}
//----------------------------------------------------------------------------
// This method performs one pass of the diffusion filter.
// The inData and outData are assumed to have data type double,
// and have the same extent.
void vtkImageAnisotropicDiffusion2D::Iterate(vtkImageData *inData,
vtkImageData *outData,
double ar0, double ar1,
int *coreExtent, int count)
{
int idx0, idx1, idx2;
vtkIdType inInc0, inInc1, inInc2;
vtkIdType outInc0, outInc1, outInc2;
int inMin0, inMax0, inMin1, inMax1, inMin2, inMax2;
int min0, max0, min1, max1, min2, max2;
double *inPtr0, *inPtr1, *inPtr2;
double *outPtr0, *outPtr1, *outPtr2;
double th0, th1, th01;
double df0, df1, df01;
double temp, sum;
int idxC, maxC;
maxC = inData->GetNumberOfScalarComponents();
inData->GetExtent(inMin0, inMax0, inMin1, inMax1, inMin2, inMax2);
inData->GetIncrements(inInc0, inInc1, inInc2);
outData->GetIncrements(outInc0, outInc1, outInc2);
// Avoid warnings.
th0 = th1 = th01 = df0 = df1 = df01 = 0.0;
// Compute direction specific diffusion thresholds and factors.
sum = 0.0;
if (this->Edges)
{
th0 = ar0 * this->DiffusionThreshold;
df0 = 1.0 / ar0;
th1 = ar1 * this->DiffusionThreshold;
df1 = 1.0 / ar1;
// two edges per direction.
sum += 2.0 * (df0 + df1);
}
if (this->Corners)
{
temp = sqrt(ar0*ar0 + ar1*ar1);
th01 = temp * this->DiffusionThreshold;
df01 = 1 / temp;
// four corners per plane
sum += 4 * (df01);
}
if (sum > 0.0)
{
temp = this->DiffusionFactor / sum;
df0 *= temp;
df1 *= temp;
df01 *= temp;
}
else
{
vtkWarningMacro(<< "Iterate: NO NEIGHBORS");
return;
}
// Compute the shrinking extent to loop over.
min0 = coreExtent[0] - count;
max0 = coreExtent[1] + count;
min1 = coreExtent[2] - count;
max1 = coreExtent[3] + count;
// intersection
min0 = (min0 > inMin0) ? min0 : inMin0;
max0 = (max0 < inMax0) ? max0 : inMax0;
min1 = (min1 > inMin1) ? min1 : inMin1;
max1 = (max1 < inMax1) ? max1 : inMax1;
vtkDebugMacro(<< "Iteration count: " << count << " ("
<< min0 << ", " << max0 << ", " << min1 << ", " << max1 << ")");
// I apologize for explicitly diffusing each neighbor, but it is the easiest
// way to deal with the boundary conditions. Besides it is fast.
// (Are you sure every one is correct?!!!)
min2 = inMin2;
max2 = inMax2;
for (idxC = 0; idxC < maxC; idxC++)
{
inPtr2 = static_cast<double *>(inData->GetScalarPointer(min0, min1, min2));
outPtr2 =
static_cast<double *>(outData->GetScalarPointer(min0, min1, min2));
inPtr2 += idxC;
outPtr2 += idxC;
for (idx2 = min2; idx2 <= max2; ++idx2, inPtr2+=inInc2, outPtr2+=outInc2)
{
inPtr1 = inPtr2;
outPtr1 = outPtr2;
for (idx1 = min1; idx1 <= max1; ++idx1, inPtr1+=inInc1, outPtr1+=outInc1)
{
inPtr0 = inPtr1;
outPtr0 = outPtr1;
for (idx0 = min0; idx0 <= max0; ++idx0, inPtr0+=inInc0, outPtr0+=outInc0)
{
// Copy center
*outPtr0 = *inPtr0;
// Special case for gradient magnitude threhsold
if (this->GradientMagnitudeThreshold)
{
double d0, d1;
// compute the gradient magnitude (central differences).
d0 = (idx0 != inMax0) ? inPtr0[inInc0] : *inPtr0;
d0 -= (idx0 != inMin0) ? inPtr0[-inInc0] : *inPtr0;
d0 /= ar0;
d1 = (idx1 != inMax1) ? inPtr0[inInc1] : *inPtr0;
d1 -= (idx1 != inMin1) ? inPtr0[-inInc1] : *inPtr0;
d1 /= ar1;
// If magnitude is big, don't diffuse.
d0 = sqrt(d0*d0 + d1*d1);
if (d0 > this->DiffusionThreshold)
{
// hack to not diffuse
th0 = th1 = th01 = 0.0;
}
else
{
// hack to diffuse
th0 = th1 = th01 = VTK_DOUBLE_MAX;
}
}
// Start diffusing
if (this->Edges)
{
// left
if (idx0 != inMin0)
{
temp = inPtr0[-inInc0] - *inPtr0;
if (fabs(temp) < th0)
{
*outPtr0 += temp * df0;
}
}
// right
if (idx0 != inMax0)
{
temp = inPtr0[inInc0] - *inPtr0;
if (fabs(temp) < th0)
{
*outPtr0 += temp * df0;
}
}
// up
if (idx1 != inMin1)
{
temp = inPtr0[-inInc1] - *inPtr0;
if (fabs(temp) < th1)
{
*outPtr0 += temp * df1;
}
}
// down
if (idx1 != inMax1)
{
temp = inPtr0[inInc1] - *inPtr0;
if (fabs(temp) < th1)
{
*outPtr0 += temp * df1;
}
}
}
if (this->Corners)
{
// left up
if (idx0 != inMin0 && idx1 != inMin1)
{
temp = inPtr0[-inInc0-inInc1] - *inPtr0;
if (fabs(temp) < th01)
{
*outPtr0 += temp * df01;
}
}
// right up
if (idx0 != inMax0 && idx1 != inMin1)
{
temp = inPtr0[inInc0-inInc1] - *inPtr0;
if (fabs(temp) < th01)
{
*outPtr0 += temp * df01;
}
}
// left down
if (idx0 != inMin0 && idx1 != inMax1)
{
temp = inPtr0[-inInc0+inInc1] - *inPtr0;
if (fabs(temp) < th01)
{
*outPtr0 += temp * df01;
}
}
// right down
if (idx0 != inMax0 && idx1 != inMax1)
{
temp = inPtr0[inInc0+inInc1] - *inPtr0;
if (fabs(temp) < th01)
{
*outPtr0 += temp * df01;
}
}
}
}
}
}
}
}
|