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
|
/* 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 "header.h"
#include "image.h"
#include "algo/histogram.h"
using namespace MR;
using namespace App;
void usage ()
{
AUTHOR = "Robert E. Smith (robert.smith@florey.edu.au)";
SYNOPSIS = "Generate a histogram of image intensities";
ARGUMENTS
+ Argument ("image", "the input image from which the histogram will be computed").type_image_in()
+ Argument ("hist", "the output histogram file").type_file_out();
OPTIONS
+ Algo::Histogram::Options
+ OptionGroup ("Additional options for mrhistogram")
+ Option ("allvolumes", "generate one histogram across all image volumes, rather than one per image volume");
}
class Volume_loop { MEMALIGN(Volume_loop)
public:
Volume_loop (Image<float>& in) :
image (in),
is_4D (in.ndim() == 4),
status (true)
{
if (is_4D)
image.index(3) = 0;
}
void operator++ ()
{
if (is_4D) {
image.index(3)++;
} else {
assert (status);
status = false;
}
}
operator bool() const
{
if (is_4D)
return (image.index(3) >= 0 && image.index(3) < image.size(3));
else
return status;
}
private:
Image<float>& image;
const bool is_4D;
bool status;
};
template <class Functor>
void run_volume (Functor& functor, Image<float>& data, Image<bool>& mask)
{
if (mask.valid()) {
for (auto l = Loop(0,3) (data, mask); l; ++l) {
if (mask.value())
functor (float(data.value()));
}
} else {
for (auto l = Loop(0,3) (data); l; ++l)
functor (float(data.value()));
}
}
void run ()
{
auto header = Header::open (argument[0]);
if (header.ndim() > 4)
throw Exception ("mrhistogram is not designed to handle images greater than 4D");
if (header.datatype().is_complex())
throw Exception ("histogram generation not supported for complex data types");
auto data = header.get_image<float>();
const bool allvolumes = get_options ("allvolumes").size();
size_t nbins = get_option_value ("bins", 0);
auto opt = get_options ("mask");
Image<bool> mask;
if (opt.size()) {
mask = Image<bool>::open (opt[0][0]);
check_dimensions (mask, header, 0, 3);
}
File::OFStream output (argument[1]);
output << "# " << App::command_history_string << "\n";
Algo::Histogram::Calibrator calibrator (nbins, get_options ("ignorezero").size());
opt = get_options ("template");
if (opt.size()) {
calibrator.from_file (opt[0][0]);
} else {
for (auto v = Volume_loop(data); v; ++v)
run_volume (calibrator, data, mask);
// If getting min/max using all volumes, but generating a single histogram per volume,
// then want the automatic calculation of bin width to be based on the number of
// voxels per volume, rather than the total number of values sent to the calibrator
calibrator.finalize (header.ndim() > 3 && !allvolumes ? header.size(3) : 1,
header.datatype().is_integer() && header.intensity_offset() == 0.0 && header.intensity_scale() == 1.0);
}
nbins = calibrator.get_num_bins();
if (!nbins)
throw Exception (std::string("No histogram bins constructed") +
((get_options ("ignorezero").size() || get_options ("bins").size()) ?
"." :
"; you might want to use the -ignorezero or -bins option."));
for (size_t i = 0; i != nbins; ++i)
output << (calibrator.get_min() + ((i+0.5) * calibrator.get_bin_width())) << ",";
output << "\n";
if (allvolumes) {
Algo::Histogram::Data histogram (calibrator);
for (auto v = Volume_loop(data); v; ++v)
run_volume (histogram, data, mask);
for (size_t i = 0; i != nbins; ++i)
output << histogram[i] << ",";
output << "\n";
} else {
for (auto v = Volume_loop(data); v; ++v) {
Algo::Histogram::Data histogram (calibrator);
run_volume (histogram, data, mask);
for (size_t i = 0; i != nbins; ++i)
output << histogram[i] << ",";
output << "\n";
}
}
}
|