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
|
/*LICENSE_START*/
/*
* Copyright 1995-2002 Washington University School of Medicine
*
* http://brainmap.wustl.edu
*
* This file is part of CARET.
*
* CARET is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CARET is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CARET; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*LICENSE_END*/
#include <QDir>
#include <QFileInfo>
#include "BrainModelVolumeSegmentationStereotaxic.h"
#include "BrainSet.h"
#include "CommaSeparatedValueFile.h"
#include "FileUtilities.h"
#include "SegmentationMaskListFile.h"
#include "SpecFile.h"
#include "StatisticHistogram.h"
#include "StringTable.h"
#include "Structure.h"
#include "VolumeFile.h"
/**
* constructor.
*/
BrainModelVolumeSegmentationStereotaxic::BrainModelVolumeSegmentationStereotaxic(
BrainSet* brainSetIn,
const VolumeFile* anatomicalVolumeFileIn,
const int uniformityIterationsIn,
const bool disconnectEyeFlagIn,
const BrainModelVolumeSureFitSegmentation::ERROR_CORRECTION_METHOD errorCorrectVolumeMethodIn,
const bool errorCorrectSurfaceFlagIn,
const bool maxPolygonsFlagIn,
const bool flatteningFilesFlagIn)
: BrainModelAlgorithm(brainSetIn),
anatomicalVolumeFile(anatomicalVolumeFileIn)
{
disconnectEyeFlag = disconnectEyeFlagIn;
uniformityIterations = uniformityIterationsIn;
errorCorrectVolumeMethod = errorCorrectVolumeMethodIn;
errorCorrectSurfaceFlag = errorCorrectSurfaceFlagIn;
maxPolygonsFlag = maxPolygonsFlagIn;
flatteningFilesFlag = flatteningFilesFlagIn;
}
/**
* destructor.
*/
BrainModelVolumeSegmentationStereotaxic::~BrainModelVolumeSegmentationStereotaxic()
{
}
/**
* execute the algorithm.
*/
void
BrainModelVolumeSegmentationStereotaxic::execute() throw (BrainModelAlgorithmException)
{
//
// Check inputs
//
if (brainSet == NULL) {
throw BrainModelAlgorithmException("BrainSet is NULL.");
}
if (anatomicalVolumeFile == NULL) {
throw BrainModelAlgorithmException("Anatomical volume file is NULL.");
}
const QString& stereotaxicSpaceName = brainSet->getStereotaxicSpace().getName();
if (stereotaxicSpaceName.isEmpty()) {
throw BrainModelAlgorithmException("SpecFile has no stereotaxic space.");
}
//
// Only do left and right structures
//
const Structure::STRUCTURE_TYPE structure = brainSet->getStructure().getType();
QString structureName;
switch (structure) {
case Structure::STRUCTURE_TYPE_CORTEX_LEFT:
structureName = "LEFT";
break;
case Structure::STRUCTURE_TYPE_CORTEX_RIGHT:
structureName = "RIGHT";
break;
case Structure::STRUCTURE_TYPE_CORTEX_BOTH:
throw BrainModelAlgorithmException("Structure BOTH not allowed, must be RIGHT or LEFT");
break;
case Structure::STRUCTURE_TYPE_CEREBELLUM:
throw BrainModelAlgorithmException("Structure CEREBELLUM not allowed, must be RIGHT or LEFT");
break;
case Structure::STRUCTURE_TYPE_CEREBELLUM_OR_CORTEX_LEFT:
throw BrainModelAlgorithmException("Structure CEREBELLUM OR LEFT not allowed, must be RIGHT or LEFT");
break;
case Structure::STRUCTURE_TYPE_CEREBELLUM_OR_CORTEX_RIGHT:
throw BrainModelAlgorithmException("Structure CEREBELLUM OR RIGHT not allowed, must be RIGHT or LEFT");
break;
case Structure::STRUCTURE_TYPE_CORTEX_LEFT_OR_CEREBELLUM:
throw BrainModelAlgorithmException("Structure LEFT CEREBELLUM not allowed, must be RIGHT or LEFT");
break;
case Structure::STRUCTURE_TYPE_CORTEX_RIGHT_OR_CEREBELLUM:
throw BrainModelAlgorithmException("Structure RIGHT CEREBELLUM not allowed, must be RIGHT or LEFT");
break;
case Structure::STRUCTURE_TYPE_CEREBRUM_CEREBELLUM:
throw BrainModelAlgorithmException("Structure CEREBRUM CEREBELLUM not allowed, must be RIGHT or LEFT");
break;
case Structure::STRUCTURE_TYPE_SUBCORTICAL:
throw BrainModelAlgorithmException("Structure SUBCORTICAL not allowed, must be RIGHT or LEFT");
break;
case Structure::STRUCTURE_TYPE_ALL:
throw BrainModelAlgorithmException("Structure ALL not allowed, must be RIGHT or LEFT");
break;
case Structure::STRUCTURE_TYPE_INVALID:
throw BrainModelAlgorithmException("Structure in SpecFile is invalid");
break;
}
//
// Find the names of the mask volumes
//
QString maskVolumeName;
getSegmentationMaskVolumeFileNames(stereotaxicSpaceName,
structureName,
maskVolumeName);
//
// Generate segmentation
//
generateSegmentation(maskVolumeName);
}
/**
* generate the segmentation.
*/
void
BrainModelVolumeSegmentationStereotaxic::generateSegmentation(
const QString& maskVolumeFileName) throw (BrainModelAlgorithmException)
{
//
// Read the mask volume
//
VolumeFile maskVolume;
try {
maskVolume.readFile(maskVolumeFileName);
}
catch (FileException& e) {
throw BrainModelAlgorithmException(e);
}
//
// Get extent of the non-zero voxels in the max volume
//
int maskExtent[6];
float coordExtent[6];
maskVolume.getNonZeroVoxelExtent(maskExtent, coordExtent);
const int padExtent = 1;
maskExtent[0] -= padExtent;
maskExtent[1] += padExtent;
maskExtent[2] -= padExtent;
maskExtent[3] += padExtent;
maskExtent[4] -= padExtent;
maskExtent[5] += padExtent;
//
// Convert mask extent to stereotaxic coordinates
//
const int minMaskExtent[3] = { maskExtent[0], maskExtent[2], maskExtent[4] };
const int maxMaskExtent[3] = { maskExtent[1], maskExtent[3], maskExtent[5] };
float minExtentXYZ[3], maxExtentXYZ[3];
maskVolume.getVoxelCoordinate(minMaskExtent, minExtentXYZ);
maskVolume.getVoxelCoordinate(maxMaskExtent, maxExtentXYZ);
//
// Add anatomical volume name to spec file
//
brainSet->addToSpecFile(SpecFile::getVolumeAnatomyFileTag(),
anatomicalVolumeFile->getFileName());
//
// Copy anatomical volume
//
VolumeFile volume(*anatomicalVolumeFile);
//
// Convert mask extent stereotaxic coordinates into input volume slices
//
int minCropping[3], maxCropping[3];
volume.convertCoordinatesToVoxelIJK(minExtentXYZ,
minCropping);
volume.convertCoordinatesToVoxelIJK(maxExtentXYZ,
maxCropping);
const int cropping[6] = {
minCropping[0],
maxCropping[0],
minCropping[1],
maxCropping[1],
minCropping[2],
maxCropping[2]
};
//
// Crop the input volume
//
volume.resize(cropping);
writeDebugVolume(volume, "anatomy_cropped");
//
// Mask the volume to extract the hemisphere
// DO NOT DO THIS FOR NOW as it causes a problem with cutting the corpus callosum
//
//volume.maskWithVolume(&maskVolume);
//
// Stretch the voxels to 0 to 255
//
volume.stretchVoxelValues();
writeDebugVolume(volume, "anatomy_0_255");
//
// Get a histogram for the volume
//
StatisticHistogram uniformHistogram = *(volume.getHistogram());
//
// get histogram measurements
//
int csfPeakBucketNumber;
int grayPeakBucketNumber;
int whitePeakBucketNumber;
int grayMinimumBucketNumber;
int whiteMaximumBucketNumber;
int grayWhiteBoundaryBucketNumber;
uniformHistogram.getGrayWhitePeakEstimates(grayPeakBucketNumber,
whitePeakBucketNumber,
grayMinimumBucketNumber,
whiteMaximumBucketNumber,
grayWhiteBoundaryBucketNumber,
csfPeakBucketNumber);
//
// Perform non-uniformity correction on the volume
//
if (uniformityIterations > 0) {
const float uniformGrayMin = uniformHistogram.getDataValueForBucket(grayMinimumBucketNumber);
const float uniformWhiteMax = uniformHistogram.getDataValueForBucket(whiteMaximumBucketNumber);
std::cout << "Bias correction Gray Min: " << uniformGrayMin
<< " White Max: " << uniformWhiteMax << std::endl;
if ((uniformGrayMin > 0) &&
(uniformWhiteMax > 0)) {
volume.biasCorrectionWithAFNI(static_cast<int>(uniformGrayMin),
static_cast<int>(uniformWhiteMax),
uniformityIterations);
writeDebugVolume(volume, "bias_corrected");
}
else {
std::cout << "Uniformity correction skipped due to invalid gray/white." << std::endl;
}
}
//
// Get histogram for gray/white peaks
//
StatisticHistogram grayWhiteHistogram = *(volume.getHistogram());
grayWhiteHistogram.getGrayWhitePeakEstimates(grayPeakBucketNumber,
whitePeakBucketNumber,
grayMinimumBucketNumber,
whiteMaximumBucketNumber,
grayWhiteBoundaryBucketNumber,
csfPeakBucketNumber);
const float whiteMatterMaximum = grayWhiteHistogram.getDataValueForBucket(whiteMaximumBucketNumber);
std::cout << "White matter maximum for segmentation: "
<< whiteMatterMaximum << std::endl;
//
// Get the peaks and adjust the gray peak a little bit low
//
const int grayOffset = 0; //-3;
const float grayPeak = grayWhiteHistogram.getDataValueForBucket(grayPeakBucketNumber
+ grayOffset);
const float whitePeak = grayWhiteHistogram.getDataValueForBucket(whitePeakBucketNumber);
std::cout << "Gray Peak: " << grayPeak
<< " White Peak: " << whitePeak << std::endl;
if ((grayPeak <= 0) ||
(whitePeak <= 0)) {
throw BrainModelAlgorithmException("ERROR: Invalid gray or white peak.");
}
//
// Get anterior commissure position
//
const float zeros[3] = { 0.0, 0.0, 0.0 };
int acIJK[3];
volume.convertCoordinatesToVoxelIJK(zeros, acIJK);
//
// Segment the volume
//
const int padding[6] = { 0, 0, 0, 0, 0, 0 };
BrainModelVolumeSureFitSegmentation sureFit(brainSet,
&volume,
NULL,
VolumeFile::FILE_READ_WRITE_TYPE_NIFTI_GZIP,
acIJK,
padding,
whitePeak,
grayPeak,
0.0,
brainSet->getStructure().getType(),
disconnectEyeFlag, // eye
true, // hindbrain
true, // high thresh
true, // cut corpus callosum
true, // segment anatomy
true, // fill ventricles
errorCorrectVolumeMethod, // correct volume
true, // generate surfaces
maxPolygonsFlag, // max polygons
errorCorrectSurfaceFlag, // correct surface
true, // inflated
flatteningFilesFlag, // very inflated
flatteningFilesFlag, // ellipsoid
flatteningFilesFlag, // sphere
flatteningFilesFlag, // CMW
flatteningFilesFlag, // hull,
flatteningFilesFlag, // Depth, Curve, Geography
false, // Landmarks
true); // auto save
sureFit.setVolumeMask(&maskVolume);
//sureFit.setWhiteMatterMaximum(whiteMatterMaximum);
sureFit.execute();
}
/**
* get the segmentation mask volume file names.
*/
void
BrainModelVolumeSegmentationStereotaxic::getSegmentationMaskVolumeFileNames(
const QString& spaceNameIn,
const QString& structureNameIn,
QString& maskNameOut) throw (BrainModelAlgorithmException)
{
maskNameOut = "";
//
// Read in segmentation masks list file
//
const QString maskVolumesDirectory(BrainSet::getCaretHomeDirectory()
+ "/data_files/segmentation_masks/");
const QString maskVolumeListFileName(maskVolumesDirectory
+ "mask_list.txt.csv");
//CommaSeparatedValueFile maskVolumeListFile;
SegmentationMaskListFile maskVolumeListFile;
try {
maskVolumeListFile.readFile(maskVolumeListFileName);
}
catch (FileException& e) {
throw BrainModelAlgorithmException(e);
}
/*
//
// Get the Mask Volumes String Table
//
const QString maskTableName("MaskVolumes");
const StringTable* maskVolumesTable = maskVolumeListFile.getDataSectionByName(maskTableName);
if (maskVolumesTable == NULL) {
throw BrainModelAlgorithmException("Unable to find table named "
+ maskTableName
+ " in "
+ maskVolumeListFileName);
}
//
// Find columns numbers of data
//
const int spaceCol = maskVolumesTable->getColumnIndexFromName("Space");
const int structureCol = maskVolumesTable->getColumnIndexFromName("Structure");
const int volumeCol = maskVolumesTable->getColumnIndexFromName("MaskVolume");
if ((spaceCol < 0) ||
(structureCol < 0) ||
(volumeCol < 0)) {
throw BrainModelAlgorithmException("Missing required columns in " +
maskVolumeListFileName);
}
//
// find the mask volume names
//
const QString& spaceName(spaceNameIn.toLower());
const QString& structureName(structureNameIn.toLower());
const int numRows = maskVolumesTable->getNumberOfRows();
for (int i = 0; i < numRows; i++) {
if (spaceName == maskVolumesTable->getElement(i, spaceCol).toLower()) {
if (structureName == maskVolumesTable->getElement(i, structureCol).toLower()) {
maskNameOut = maskVolumesDirectory
+ maskVolumesTable->getElement(i, volumeCol);
}
}
}
*/
maskNameOut = maskVolumeListFile.getSegmentationMaskFileName(spaceNameIn,
structureNameIn);
if (maskNameOut.isEmpty()) {
QString msg("Unable to find mask volume for space \""
+ spaceNameIn
+ "\" and structure \""
+ structureNameIn
+ "\".\n"
+ "Available masks are: \n"
+ maskVolumeListFile.getAvailableMasks(" "));
/* for (int i = 0; i < numRows; i++) {
msg += (maskVolumesTable->getElement(i, spaceCol).toLower()
+ " "
+ maskVolumesTable->getElement(i, structureCol).toLower()
+ "\n");
}
*/
throw BrainModelAlgorithmException(msg);
}
if (QFile::exists(maskNameOut) == false) {
throw BrainModelAlgorithmException("Mask Volume "
+ maskNameOut
+ " is missing.");
}
}
/**
* write debugging volume.
*/
void
BrainModelVolumeSegmentationStereotaxic::writeDebugVolume(VolumeFile& vf,
const QString& fileNameIn)
{
//if (DebugControl::getDebugOn()) {
const QString name("seg_debug_"
+ fileNameIn
+ SpecFile::getNiftiGzipVolumeFileExtension());
try {
vf.writeFile(name);
}
catch (FileException& e) {
std::cout << "Writing debug volume: " << e.whatQString().toAscii().constData() << std::endl;
}
//}
}
|