_reg_tools.h 15.8 KB
Newer Older
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
/**
 * @file _reg_tools.h
 * @author Marc Modat
 * @date 25/03/2009
 * @brief Set of useful functions
 *
 *  Created by Marc Modat on 25/03/2009.
 *  Copyright (c) 2009, University College London. All rights reserved.
 *  Centre for Medical Image Computing (CMIC)
 *  See the LICENSE.txt file in the nifty_reg root folder
 *
 */

#ifndef _REG_TOOLS_H
#define _REG_TOOLS_H

#include <fstream>
#include <limits>
#include "_reg_maths.h"


/* *************************************************************** */
/** @brief This function check some header parameters and correct them in
 * case of error. For example no dimension is lower than one. The scl_sclope
 * can not be equal to zero. The qto_xyz and qto_ijk are populated if
 * both qform_code and sform_code are set to zero.
 * @param image Input image to check and correct if necessary
 */
extern "C++"
void reg_checkAndCorrectDimension(nifti_image *image);

/* *************************************************************** */
/** @brief Check if the specified filename corresponds to an image.
 * @param name Input filename
 * @return True is the specified filename corresponds to an image,
 * false otherwise.
 */
extern "C++"
bool reg_isAnImageFileName(char *name);

/* *************************************************************** */
/** @brief Rescale an input image between two user-defined values.
 * Some threshold can also be applied concurrenlty
 * @param image Image to be rescaled
 * @param newMin Intensity lower bound after rescaling
 * @param newMax Intensity higher bound after rescaling
 * @param lowThr Intensity to use as lower threshold
 * @param upThr Intensity to use as higher threshold
 */
extern "C++"
void reg_intensityRescale(nifti_image *image,
                          int timepoint,
                          float newMin,
                          float newMax
                         );
56 57 58 59 60 61 62 63 64 65


/* *************************************************************** */
/** @brief Set the scl_slope to 1 and the scl_inter to 0 and rescale
 * the intensity values
 * @param image Image to be updated
 */
extern "C++"
void reg_tools_removeSCLInfo(nifti_image *img);

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
/* *************************************************************** */
/** @brief reg_getRealImageSpacing
 * @param image image
 * @param spacingValues spacingValues
 */
extern "C++" template <class DTYPE>
void reg_getRealImageSpacing(nifti_image *image,
                             DTYPE *spacingValues);
/* *************************************************************** */
/** @brief Smooth an image using a Gaussian kernel
 * @param image Image to be smoothed
 * @param sigma Standard deviation of the Gaussian kernel
 * to use. The kernel is bounded between +/- 3 sigma.
 * @param axis Boolean array to specify which axis have to be
 * smoothed. The array follow the dim array of the nifti header.
 */
extern "C++"
void reg_tools_kernelConvolution(nifti_image *image,
                                 float *sigma,
                                 int kernelType,
                                 int *mask = NULL,
                                 bool *timePoints = NULL,
                                 bool *axis = NULL);
/* *************************************************************** */
/** @brief Downsample an image by a ratio of two
 * @param image Image to be downsampled
 * @param type The image is first smoothed  using a Gaussian
 * kernel of 0.7 voxel standard deviation before being downsample
 * if type is set to true.
 * @param axis Boolean array to specify which axis have to be
 * downsampled. The array follow the dim array of the nifti header.
 */
extern "C++" template <class PrecisionTYPE>
void reg_downsampleImage(nifti_image *image,
                         int type,
                         bool *axis
                        );
/* *************************************************************** */
/** @brief Returns the maximal euclidean distance from a
 * deformation field image
 * @param image Vector image to be considered
 * @return Scalar value that corresponds to the longest
 * euclidean distance
 */
extern "C++" template <class PrecisionTYPE>
PrecisionTYPE reg_getMaximalLength(nifti_image *image);
/* *************************************************************** */
/** @brief Change the datatype of a nifti image
 * @param image Image to be updated.
 */
extern "C++" template <class NewTYPE>
Marc Modat's avatar
Marc Modat committed
117 118
void reg_tools_changeDatatype(nifti_image *image,
                              int type=-1);
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
/* *************************************************************** */
/** @brief Add two images.
 * @param img1 First image to consider
 * @param img2 Second image to consider
 * @param out Result image that contains the result of the operation
 * between the first and second image.
 */
extern "C++"
void reg_tools_addImageToImage(nifti_image *img1,
                               nifti_image *img2,
                               nifti_image *out);
/* *************************************************************** */
/** @brief Substract two images.
 * @param img1 First image to consider
 * @param img2 Second image to consider
 * @param out Result image that contains the result of the operation
 * between the first and second image.
 */
extern "C++"
void reg_tools_substractImageToImage(nifti_image *img1,
                                     nifti_image *img2,
                                     nifti_image *out);
/* *************************************************************** */
/** @brief Multiply two images.
 * @param img1 First image to consider
 * @param img2 Second image to consider
 * @param out Result image that contains the result of the operation
 * between the first and second image.
 */
extern "C++"
void reg_tools_multiplyImageToImage(nifti_image *img1,
                                    nifti_image *img2,
                                    nifti_image *out);
/* *************************************************************** */
/** @brief Divide two images.
 * @param img1 First image to consider
 * @param img2 Second image to consider
 * @param out Result image that contains the result of the operation
 * between the first and second image.
 */
extern "C++"
void reg_tools_divideImageToImage(nifti_image *img1,
                                  nifti_image *img2,
                                  nifti_image *out);

/* *************************************************************** */
/** @brief Add a scalar to all image intensity
 * @param img1 Input image
 * @param out Result image that contains the result of the operation.
 * @param val Value to be added to input image
 */
extern "C++"
void reg_tools_addValueToImage(nifti_image *img1,
                               nifti_image *out,
                               float val);
/* *************************************************************** */
/** @brief Substract a scalar to all image intensity
 * @param img1 Input image
 * @param out Result image that contains the result of the operation.
 * @param val Value to be substracted to input image
 */
extern "C++"
void reg_tools_substractValueToImage(nifti_image *img1,
                                     nifti_image *out,
                                     float val);
/* *************************************************************** */
/** @brief Multiply a scalar to all image intensity
 * @param img1 Input image
 * @param out Result image that contains the result of the operation.
 * @param val Value to be multiplied to input image
 */
extern "C++"
void reg_tools_multiplyValueToImage(nifti_image *img1,
                                    nifti_image *out,
                                    float val);
/* *************************************************************** */
/** @brief Mivide a scalar to all image intensity
 * @param img1 Input image
 * @param out Result image that contains the result of the operation.
 * @param val Value to be divided to input image
 */
extern "C++"
void reg_tools_divideValueToImage(nifti_image *img1,
                                  nifti_image *out,
                                  float val);

/* *************************************************************** */
/** @brief Binarise an input image. All values different
 * from 0 are set to 1, 0 otherwise.
 * @param img Image that will be binarise inline
 */
extern "C++"
void reg_tools_binarise_image(nifti_image *img);

/* *************************************************************** */
/** @brief Binarise an input image. The binarisation is
 * performed according to a threshold value that is
 * user-defined.
 * @param img Image that will be binarise inline
 * @param thr Threshold value used for binarisation.
 * All values bellow thr are set to 0. All values equal
 * or bellow thr are set to 1
 */
extern "C++"
void reg_tools_binarise_image(nifti_image *img,
                              float thr);

/* *************************************************************** */
/** @brief Convert a binary image into an array of int.
 * This is used to define a mask within the registration
 * function.
 * @param img Input image
 * @param array The data array from the input nifti image
 * is binarised and stored in this array.
 * @param activeVoxelNumber This reference is updated
 * with the number of voxel that are included into the
 * mask
 */
extern "C++"
void reg_tools_binaryImage2int(nifti_image *img,
                               int *array,
                               int &activeVoxelNumber);

/* *************************************************************** */
/** @brief Compute the mean root mean squared error between
 * two vector images
 * @param imgA Input vector image
 * @param imgB Input vector image
 * @return Mean rsoot mean squared error valueis returned
 */
extern "C++"
double reg_tools_getMeanRMS(nifti_image *imgA,
                            nifti_image *imgB);
/* *************************************************************** */
/** @brief Set all voxels from an image to NaN if the voxel
 * bellong to the mask
 * @param img Input image to be masked with NaN value
 * @param mask Input mask that defines which voxels
 * have to be set to NaN
 * @param res Output image
 */
extern "C++"
int reg_tools_nanMask_image(nifti_image *img,
                            nifti_image *mask,
                            nifti_image *res);
/* *************************************************************** */
/** @brief Get the minimal value of an image
 * @param img Input image
 * @return min value
 */
extern "C++"
float reg_tools_getMinValue(nifti_image *img);
/* *************************************************************** */
/** @brief Get the maximal value of an image
 * @param img Input image
 * @return max value
 */
extern "C++"
float reg_tools_getMaxValue(nifti_image *img);
/* *************************************************************** */
/** @brief Generate a pyramid from an input image.
 * @param input Input image to be downsampled to create the pyramid
 * @param pyramid Output array of images that will contains the
 * different resolution images of the pyramid
 * @param levelNumber Number of level to use to create the pyramid.
 * 1 level corresponds to the original image resolution.
 * @param levelToPerform Number to level that will be perform during
 * the registration.
 */
extern "C++" template<class DTYPE>
int reg_createImagePyramid(nifti_image * input,
                           nifti_image **pyramid,
                           unsigned int levelNumber,
                           unsigned int levelToPerform);
/* *************************************************************** */
/** @brief Generate a pyramid from an input mask image.
 * @param input Input image to be downsampled to create the pyramid
 * @param pyramid Output array of mask images that will contains the
 * different resolution images of the pyramid
 * @param levelNumber Number of level to use to create the pyramid.
 * 1 level corresponds to the original image resolution.
 * @param levelToPerform Number to level that will be perform during
 * the registration.
 * @param activeVoxelNumber Array that contains the number of active
 * voxel for each level of the pyramid
 */
extern "C++" template<class DTYPE>
int reg_createMaskPyramid(nifti_image *input,
                          int **pyramid,
                          unsigned int levelNumber,
                          unsigned int levelToPerform,
                          int *activeVoxelNumber);
/* *************************************************************** */
/** @brief this function will threshold an image to the values provided,
 * set the scl_slope and sct_inter of the image to 1 and 0
 * (SSD uses actual image data values),
 * and sets cal_min and cal_max to have the min/max image data values.
 * @param image Input image to be thresholded.
 * @param lowThr Lower threshold value. All Value bellow the threshold
 * are set to the threshold value.
 * @param upThr Upper threshold value. All Value above the threshold
 * are set to the threshold value.
 */
extern "C++" template<class T>
void reg_thresholdImage(nifti_image *image,
                        T lowThr,
                        T upThr
                       );
/* *************************************************************** */
/** @brief This function flipp the specified axis
 * @param image Input image to be flipped
 * @param array Array that will contain the flipped
 * input image->data array
 * @param cmd String that contains the letter(s) of the axis
 * to flip (xyztuvw)
 */
extern "C++"
void reg_flippAxis(nifti_image *image,
                   void *array,
                   std::string cmd
                  );
/* *************************************************************** */
/** @brief This function converts an image containing deformation
 * field into a displacement field
 * The conversion is done using the appropriate qform/sform
 * @param image Image that contains a deformation field and will be
 * converted into a displacement field
 */
extern "C++"
int reg_getDisplacementFromDeformation(nifti_image *image);
/* *************************************************************** */
/** @brief This function converts an image containing a displacement field
 * into a displacement field.
 * The conversion is done using the appropriate qform/sform
 * @param image Image that contains a deformation field and will be
 * converted into a displacement field
 */
extern "C++"
int reg_getDeformationFromDisplacement(nifti_image *image);
/* *************************************************************** */
/** @brief The functions returns the largest ratio between two arrays
 * The returned value is the largest value computed as ((A/B)-1)
 * If A or B are zeros then the (A-B) value is returned.
 */
extern "C++" template<class DTYPE>
float reg_test_compare_arrays(DTYPE *ptrA,
                              DTYPE *ptrB,
                              size_t nvox);
/* *************************************************************** */
/** @brief The functions returns the largest ratio between input image intensities
 * The returned value is the largest value computed as ((A/B)-1)
 * If A or B are zeros then the (A-B) value is returned.
 */
extern "C++"
float reg_test_compare_images(nifti_image *imgA,
                              nifti_image *imgB);
/* *************************************************************** */
/** @brief The absolute operator is applied to the input image
 */
extern "C++"
void reg_tools_abs_image(nifti_image *img);
/* *************************************************************** */
/** @brief This function tells the progress to the CLI */
extern "C++"
void progressXML(unsigned long p, std::string text);
/* *************************************************************** */
/** @brief This function initiates progress updates through the CLI */
extern "C++"
void startProgress(std::string name);
/* *************************************************************** */
/** @brief This function closes progress updates through the CLI */
extern "C++"
void closeProgress(std::string name, std::string status);
/* *************************************************************** */
#endif