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
|
/* Copyright (c) 2008-2025 the MRtrix3 contributors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Covered Software is provided under this License on an "as is"
* basis, without warranty of any kind, either expressed, implied, or
* statutory, including, without limitation, warranties that the
* Covered Software is free of defects, merchantable, fit for a
* particular purpose or non-infringing.
* See the Mozilla Public License v. 2.0 for more details.
*
* For more details, see http://www.mrtrix.org/.
*/
#include "command.h"
#include "image.h"
#include "image_helpers.h"
#include "transform.h"
#include "types.h"
using namespace MR;
using namespace App;
void usage ()
{
AUTHOR = "Robert E. Smith (robert.smith@florey.edu.au)";
SYNOPSIS = "Determine the centre of mass / centre of gravity of an image";
ARGUMENTS
+ Argument ("input", "the input image").type_image_in();
OPTIONS
+ Option ("mask", "only include voxels within a mask in the calculation")
+ Argument ("image").type_image_in()
+ Option ("voxelspace", "report image centre of mass in voxel space rather than scanner space");
}
typedef float value_type;
void run ()
{
Image<value_type> image = Image<value_type>::open (argument[0]);
if (image.ndim() > 3)
throw Exception ("Command does not accept images with more than 3 dimensions");
Image<bool> mask;
auto opt = get_options ("mask");
if (opt.size()) {
mask = Image<bool>::open (opt[0][0]);
check_dimensions (image, mask);
}
Eigen::Vector3d com (0.0, 0.0, 0.0);
default_type mass = 0.0;
if (mask.valid()) {
for (auto l = Loop(image) (image, mask); l; ++l) {
if (mask.value()) {
com += Eigen::Vector3d (image.index(0), image.index(1), image.index(2)) * image.value();
mass += image.value();
}
}
} else {
for (auto l = Loop(image) (image); l; ++l) {
com += Eigen::Vector3d (image.index(0), image.index(1), image.index(2)) * image.value();
mass += image.value();
}
}
com /= mass;
if (!get_options ("voxelspace").size())
com = image.transform() * com;
std::cout << com.transpose();
}
|