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
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* 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
*
* https://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 itkPolylineMaskImageFilter_hxx
#define itkPolylineMaskImageFilter_hxx
#include "itkRigid3DPerspectiveTransform.h"
#include "itkImageRegionConstIterator.h"
#include "itkProgressReporter.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkResampleImageFilter.h"
#include "itkLineIterator.h"
#include "itkImageLinearIteratorWithIndex.h"
#include "itkPathIterator.h"
#include "itkVector.h"
#include "itkBoundingBox.h"
namespace itk
{
template <typename TInputImage, typename TPolyline, typename TVector, typename TOutputImage>
PolylineMaskImageFilter<TInputImage, TPolyline, TVector, TOutputImage>::PolylineMaskImageFilter()
: m_ViewVector(MakeFilled<VectorType>(1))
, m_UpVector(MakeFilled<VectorType>(1))
, m_CameraCenterPoint(0)
, m_FocalPoint()
{
this->SetNumberOfRequiredInputs(2);
// This filter is meant only for 3D input and output images. We must
// throw an exception otherwise.
if ((TInputImage::ImageDimension != 3) || (TOutputImage::ImageDimension != 3))
{
itkExceptionMacro("PolylineMaskImageFilter must be templated over "
<< "input and output images of dimension 3");
}
// View vectors must be of dimension 3
if (TVector::Length != 3)
{
itkExceptionMacro("PolylineMaskImageFilter must be templated over "
<< "a view vector of length 3");
}
}
template <typename TInputImage, typename TPolyline, typename TVector, typename TOutputImage>
void
PolylineMaskImageFilter<TInputImage, TPolyline, TVector, TOutputImage>::SetInput1(const InputImageType * input)
{
// Process object is not const-correct so the const_cast is required here
this->ProcessObject::SetNthInput(0, const_cast<InputImageType *>(input));
}
template <typename TInputImage, typename TPolyline, typename TVector, typename TOutputImage>
void
PolylineMaskImageFilter<TInputImage, TPolyline, TVector, TOutputImage>::SetInput2(const PolylineType * input)
{
// Process object is not const-correct so the const_cast is required here
this->ProcessObject::SetNthInput(1, const_cast<PolylineType *>(input));
}
template <typename TInputImage, typename TPolyline, typename TVector, typename TOutputImage>
void
PolylineMaskImageFilter<TInputImage, TPolyline, TVector, TOutputImage>::GenerateRotationMatrix()
{
// Normalize view and up vectors
TVector nUpVector;
TVector nViewVector;
nUpVector = m_UpVector;
nUpVector.Normalize();
nViewVector = m_ViewVector;
nViewVector.Normalize();
itkDebugMacro("Normalized Up vector" << nUpVector);
itkDebugMacro("Normalized View vector" << nViewVector);
// Orthogonalize nUpVector and nViewVector
TVector nOrthogonalVector;
nOrthogonalVector = nUpVector - (nViewVector * (nUpVector * nViewVector));
itkDebugMacro("Up vector component orthogonal to View vector " << nOrthogonalVector);
// Perform the cross product and determine a third coordinate axis
// orthogonal to both nOrthogonalVector and nViewVector.
TVector nThirdAxis;
nThirdAxis = itk::CrossProduct(nOrthogonalVector, nViewVector);
itkDebugMacro("Third basis vector" << nThirdAxis);
// Populate the rotation matrix using the unit vectors of the
// camera reference coordinate system.
m_RotationMatrix[0][0] = nThirdAxis[0];
m_RotationMatrix[0][1] = nThirdAxis[1];
m_RotationMatrix[0][2] = nThirdAxis[2];
m_RotationMatrix[1][0] = nOrthogonalVector[0];
m_RotationMatrix[1][1] = nOrthogonalVector[1];
m_RotationMatrix[1][2] = nOrthogonalVector[2];
m_RotationMatrix[2][0] = nViewVector[0];
m_RotationMatrix[2][1] = nViewVector[1];
m_RotationMatrix[2][2] = nViewVector[2];
}
template <typename TInputImage, typename TPolyline, typename TVector, typename TOutputImage>
auto
PolylineMaskImageFilter<TInputImage, TPolyline, TVector, TOutputImage>::TransformProjectPoint(PointType inputPoint)
-> ProjPlanePointType
{
PointType centered;
for (unsigned int i = 0; i < 3; ++i)
{
centered[i] = inputPoint[i] - m_CameraCenterPoint[i];
}
PointType rotated = m_RotationMatrix * centered;
ProjPlanePointType result;
double factor = m_FocalDistance / (rotated[2]);
result[0] = m_FocalPoint[0] + (rotated[0] * factor);
result[1] = m_FocalPoint[1] + (rotated[1] * factor);
return result;
}
template <typename TInputImage, typename TPolyline, typename TVector, typename TOutputImage>
void
PolylineMaskImageFilter<TInputImage, TPolyline, TVector, TOutputImage>::GenerateData()
{
using InputImageSizeType = typename TInputImage::SizeType;
using InputImagePointType = typename TInputImage::PointType;
using InputImageSpacingType = typename TInputImage::SpacingType;
using InputImageConstIteratorType = ImageRegionConstIterator<TInputImage>;
using PixelType = typename TOutputImage::PixelType;
using OutputImageIteratorType = ImageRegionIterator<TOutputImage>;
using VertexType = typename TPolyline::VertexType;
using VertexListType = typename TPolyline::VertexListType;
using OriginType = Point<double, 3>;
typename TInputImage::ConstPointer inputImagePtr(dynamic_cast<const TInputImage *>(this->ProcessObject::GetInput(0)));
typename TPolyline::ConstPointer polylinePtr(dynamic_cast<const TPolyline *>(this->ProcessObject::GetInput(1)));
typename TOutputImage::Pointer outputImagePtr(dynamic_cast<TOutputImage *>(this->ProcessObject::GetOutput(0)));
OriginType originInput;
originInput.Fill(0.0);
// outputImagePtr->SetOrigin(inputImagePtr->GetOrigin());
outputImagePtr->SetOrigin(originInput);
outputImagePtr->SetSpacing(inputImagePtr->GetSpacing());
outputImagePtr->SetDirection(inputImagePtr->GetDirection());
outputImagePtr->SetRequestedRegion(inputImagePtr->GetRequestedRegion());
outputImagePtr->SetBufferedRegion(inputImagePtr->GetBufferedRegion());
outputImagePtr->SetLargestPossibleRegion(inputImagePtr->GetLargestPossibleRegion());
outputImagePtr->AllocateInitialized();
InputImageConstIteratorType inputIt(inputImagePtr, inputImagePtr->GetLargestPossibleRegion());
OutputImageIteratorType outputIt(outputImagePtr, outputImagePtr->GetLargestPossibleRegion());
using InterpolatorType = NearestNeighborInterpolateImageFunction<TInputImage, double>;
using InterpolatorPointType = typename InterpolatorType::PointType;
// Generate the transformation matrix
this->GenerateRotationMatrix();
// Generate input and output points
InterpolatorPointType inputPoint;
ProjPlanePointType outputPoint;
// Generate a 2D image with the viewing polygon as a mask
using ProjectionImageType = Image<PixelType, 2>;
using ProjectionImageIndexType = typename ProjectionImageType::IndexType;
using ProjectionImagePointType = typename ProjectionImageType::PointType;
using ProjectionImageSpacingType = typename ProjectionImageType::SpacingType;
using ProjectionImagePixelType = typename ProjectionImageType::PixelType;
using ProjectionImageRegionType = typename ProjectionImageType::RegionType;
using ProjectionImageSizeType = typename ProjectionImageType::SizeType;
// Determine the projection image size by transforming the eight corners
// of the 3D input image
InputImageSizeType inputImageSize;
using CornerPointType = Point<double, 3>;
using CornerPointProjectionType = Point<double, 2>;
using BoundingBoxType = BoundingBox<unsigned long, 2, double>;
using CornerPointProjectionContainer = BoundingBoxType::PointsContainer;
auto cornerPointProjectionlist = CornerPointProjectionContainer::New();
CornerPointType cornerPoint;
CornerPointType originPoint;
CornerPointProjectionType cornerProjectionPoint;
originPoint[0] = 0.0;
originPoint[1] = 0.0;
originPoint[2] = 0.0;
originPoint = inputImagePtr->GetOrigin();
inputImageSize = inputImagePtr->GetLargestPossibleRegion().GetSize();
// 1st corner (xmin,ymin,zmin)
cornerPoint = originPoint;
cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
cornerPointProjectionlist->push_back(cornerProjectionPoint);
// 2nd corner (xmin,ymin,zmax)
cornerPoint[0] = originPoint[0];
cornerPoint[1] = originPoint[1];
cornerPoint[2] = originPoint[2] + inputImageSize[2];
cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
cornerPointProjectionlist->push_back(cornerProjectionPoint);
// 3rd corner (xmin,ymax,zmin)
cornerPoint[0] = originPoint[0];
cornerPoint[1] = originPoint[1] + inputImageSize[1];
cornerPoint[2] = originPoint[2];
cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
cornerPointProjectionlist->push_back(cornerProjectionPoint);
// 4th corner (xmin,ymax,zmax)
cornerPoint[0] = originPoint[0];
cornerPoint[1] = originPoint[1] + inputImageSize[1];
cornerPoint[2] = originPoint[2] + inputImageSize[2];
cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
cornerPointProjectionlist->push_back(cornerProjectionPoint);
// 5th corner (xmax,ymin,zmin)
cornerPoint[0] = originPoint[0] + inputImageSize[0];
cornerPoint[1] = originPoint[1];
cornerPoint[2] = originPoint[2];
cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
cornerPointProjectionlist->push_back(cornerProjectionPoint);
// 6th corner (xmax,ymin,zmax)
cornerPoint[0] = originPoint[0] + inputImageSize[0];
cornerPoint[1] = originPoint[1];
cornerPoint[2] = originPoint[2] + inputImageSize[2];
cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
cornerPointProjectionlist->push_back(cornerProjectionPoint);
// 7th corner (xmax,ymax,zmin)
cornerPoint[0] = originPoint[0] + inputImageSize[0];
cornerPoint[1] = originPoint[1] + inputImageSize[1];
cornerPoint[2] = originPoint[2];
cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
cornerPointProjectionlist->push_back(cornerProjectionPoint);
// 8th corner (xmax,ymax,zmax)
cornerPoint[0] = originPoint[0] + inputImageSize[0];
cornerPoint[1] = originPoint[1] + inputImageSize[1];
cornerPoint[2] = originPoint[2] + inputImageSize[2];
cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
cornerPointProjectionlist->push_back(cornerProjectionPoint);
// Compute the bounding box of the projected points
auto boundingBox = BoundingBoxType::New();
boundingBox->SetPoints(cornerPointProjectionlist);
if (!boundingBox->ComputeBoundingBox())
{
itkExceptionMacro("Bounding box computation error");
}
const BoundingBoxType::BoundsArrayType & bounds = boundingBox->GetBounds();
itkDebugMacro("Projection image bounding box=" << bounds);
ProjectionImageIndexType projectionStart;
projectionStart[0] = 0;
projectionStart[1] = 0;
ProjectionImageSizeType projectionSize;
IndexValueType pad;
pad = 5;
projectionSize[0] = (IndexValueType)(bounds[1] - bounds[0]) + pad;
projectionSize[1] = (IndexValueType)(bounds[3] - bounds[2]) + pad;
const ProjectionImageRegionType projectionRegion(projectionStart, projectionSize);
auto projectionImagePtr = ProjectionImageType::New();
ProjectionImagePointType origin;
origin[0] = bounds[0];
origin[1] = bounds[2];
projectionImagePtr->SetOrigin(origin);
ProjectionImageSpacingType spacing;
spacing[0] = 1.0;
spacing[1] = 1.0;
projectionImagePtr->SetSpacing(spacing);
itkDebugMacro("Projection image size:" << projectionSize);
itkDebugMacro("Projection image start index:" << projectionStart);
itkDebugMacro("Projection image origin:" << origin);
projectionImagePtr->SetRegions(projectionRegion);
projectionImagePtr->AllocateInitialized();
using ProjectionImageIteratorType = ImageRegionIterator<ProjectionImageType>;
ProjectionImageIteratorType projectionIt(projectionImagePtr, projectionImagePtr->GetLargestPossibleRegion());
itkDebugMacro("Rotation matrix" << m_RotationMatrix);
const VertexListType * container = polylinePtr->GetVertexList();
typename VertexListType::ConstIterator piter = container->Begin();
// Rasterize each polyline segment using Bresenham line iterator
VertexType startIndex;
VertexType endIndex;
VertexType projectionIndex;
VertexType pstartIndex;
// Define a flag to indicate the line segment slope
bool pflag;
// Define background, foreground, and unlabeled pixel values
auto u_val = static_cast<ProjectionImagePixelType>(0);
auto b_val = static_cast<ProjectionImagePixelType>(2);
auto f_val = static_cast<ProjectionImagePixelType>(255);
projectionImagePtr->FillBuffer(u_val);
// Polygon start index
pstartIndex = piter.Value();
projectionIndex = pstartIndex;
++piter;
ProjectionImageIndexType startImageIndex;
ProjectionImageIndexType endImageIndex;
ProjectionImageIndexType projectionImageIndex;
using LineIteratorType = LineIterator<ProjectionImageType>;
using ImageLineIteratorType = ImageLinearIteratorWithIndex<ProjectionImageType>;
ImageLineIteratorType imit(projectionImagePtr, projectionImagePtr->GetLargestPossibleRegion());
imit.SetDirection(0);
while (piter != container->End())
{
pflag = false;
startIndex = projectionIndex;
endIndex = piter.Value();
for (unsigned int i = 0; i < ProjectionImageType::ImageDimension; ++i)
{
startImageIndex[i] = static_cast<IndexValueType>(startIndex[i]);
endImageIndex[i] = static_cast<IndexValueType>(endIndex[i]);
}
if (endImageIndex[1] > startImageIndex[1])
{
pflag = true;
}
itkDebugMacro("Polyline:" << startImageIndex << ',' << endImageIndex);
LineIteratorType it(projectionImagePtr, startImageIndex, endImageIndex);
it.GoToBegin();
while (!it.IsAtEnd())
{
projectionImageIndex[0] = it.GetIndex()[0];
projectionImageIndex[1] = it.GetIndex()[1];
// Initialize the image line iterator
imit.SetIndex(projectionImageIndex);
while (!imit.IsAtEndOfLine())
{
if (pflag)
{
if (imit.Get() == u_val)
{
imit.Set(f_val);
}
}
else
{
imit.Set(b_val);
}
++imit;
}
++it;
}
projectionIndex = endIndex;
++piter;
}
// Close the polygon
pflag = false;
startIndex = projectionIndex;
endIndex = pstartIndex;
for (unsigned int i = 0; i < ProjectionImageType::ImageDimension; ++i)
{
startImageIndex[i] = static_cast<IndexValueType>(startIndex[i]);
endImageIndex[i] = static_cast<IndexValueType>(endIndex[i]);
}
if (endImageIndex[1] > startImageIndex[1])
{
pflag = true;
}
LineIteratorType it(projectionImagePtr, startImageIndex, endImageIndex);
while (!it.IsAtEnd())
{
projectionImageIndex[0] = it.GetIndex()[0];
projectionImageIndex[1] = it.GetIndex()[1];
// Initialize the image line iterator
imit.SetIndex(projectionImageIndex);
while (!imit.IsAtEndOfLine())
{
if (pflag)
{
if (imit.Get() == u_val)
{
imit.Set(f_val);
}
}
else
{
imit.Set(b_val);
}
++imit;
}
++it;
}
// Mask the input image using the binary image defined by the region
// demarcated by the polyline contour
InputImageSpacingType inputImageSpacing;
InputImagePointType inputImageOrigin;
inputImageSpacing = inputImagePtr->GetSpacing();
inputImageOrigin = inputImagePtr->GetOrigin();
inputImageSize = inputImagePtr->GetLargestPossibleRegion().GetSize();
while (!inputIt.IsAtEnd())
{
outputImagePtr->TransformIndexToPhysicalPoint(outputIt.GetIndex(), inputPoint);
outputPoint = this->TransformProjectPoint(inputPoint);
projectionImageIndex = projectionImagePtr->TransformPhysicalPointToIndex(outputPoint);
if (!projectionImagePtr->GetBufferedRegion().IsInside(projectionImageIndex))
{
itkExceptionMacro("Projection Image index out of bound:" << projectionImageIndex);
}
if (projectionImagePtr->GetPixel(projectionImageIndex) == f_val)
{
outputIt.Set(inputIt.Get());
}
else
{
outputIt.Set(u_val);
}
++inputIt;
++outputIt;
}
}
template <typename TInputImage, typename TPolyline, typename TVector, typename TOutputImage>
void
PolylineMaskImageFilter<TInputImage, TPolyline, TVector, TOutputImage>::PrintSelf(std::ostream & os,
Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Viewing vector: " << static_cast<typename NumericTraits<VectorType>::PrintType>(m_ViewVector)
<< std::endl;
os << indent << "Up Vector: " << static_cast<typename NumericTraits<VectorType>::PrintType>(m_UpVector) << std::endl;
os << indent << "Camera Center Point: " << m_CameraCenterPoint << std::endl;
os << indent << "Focal Point : " << m_FocalPoint << std::endl;
os << indent << "Focal Distance : " << m_FocalDistance << std::endl;
os << indent << "Rotation matrix : " << m_RotationMatrix << std::endl;
}
} // end namespace itk
#endif
|