File: label2colour.cpp

package info (click to toggle)
mrtrix3 3.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,300 kB
  • sloc: cpp: 130,470; python: 9,603; sh: 597; makefile: 62; xml: 47
file content (124 lines) | stat: -rw-r--r-- 3,552 bytes parent folder | download
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
/* 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 <map>

#include "command.h"
#include "image.h"
#include "mrtrix.h"

#include "algo/loop.h"
#include "math/rng.h"

#include "connectome/connectome.h"
#include "connectome/lut.h"



using namespace MR;
using namespace App;
using namespace MR::Connectome;


void usage ()
{

  AUTHOR = "Robert E. Smith (robert.smith@florey.edu.au)";

  SYNOPSIS = "Convert a parcellated image (where values are node indices) into a colour image";

  DESCRIPTION
  + "Many software packages handle this colouring internally within their viewer program; this binary "
    "explicitly converts a parcellation image into a colour image that should be viewable in any software.";

  ARGUMENTS
  + Argument ("nodes_in",   "the input node parcellation image").type_image_in()
  + Argument ("colour_out", "the output colour image").type_image_out();

  OPTIONS
  + Option ("lut", "Provide the relevant colour lookup table "
                   "(if not provided, nodes will be coloured randomly)")
    + Argument ("file").type_file_in();

}





void run ()
{

  auto H = Header::open (argument[0]);
  Connectome::check (H);
  auto nodes = H.get_image<node_t>();

  const std::string lut_path = get_option_value<std::string> ("lut", "");
  LUT lut;
  if (lut_path.size()) {
    lut.load (lut_path);
  } else {
    INFO ("No lookup table provided; colouring nodes randomly");

    node_t max_index = 0;
    for (auto l = Loop (nodes) (nodes); l; ++l) {
      const node_t index = nodes.value();
      if (index > max_index)
        max_index = index;
    }

    lut.insert (std::make_pair (0, LUT_node ("None", 0, 0, 0, 0)));
    Math::RNG rng;
    std::uniform_int_distribution<uint8_t> dist;

    for (node_t i = 1; i <= max_index; ++i) {
      LUT_node::RGB colour;
      do {
        colour[0] = dist (rng);
        colour[1] = dist (rng);
        colour[2] = dist (rng);
      } while (int(colour[0]) + int(colour[1]) + int(colour[2]) < 100);
      lut.insert (std::make_pair (i, LUT_node (str(i), colour)));
    }
  }

  H.ndim() = 4;
  H.size (3) = 3;
  H.datatype() = DataType::UInt8;
  add_line (H.keyval()["comments"], "Coloured parcellation image generated by label2colour");
  if (lut_path.size())
    H.keyval()["LUT"] = Path::basename (lut_path);
  auto out = Image<uint8_t>::create (argument[1], H);

  for (auto l = Loop ("Colourizing parcellated node image", nodes) (nodes, out); l; ++l) {
    const node_t index = nodes.value();
    const LUT::const_iterator i = lut.find (index);
    if (i == lut.end()) {
      out.index (3) = 0; out.value() = 0;
      out.index (3) = 1; out.value() = 0;
      out.index (3) = 2; out.value() = 0;
    } else {
      const LUT_node::RGB& colour (i->second.get_colour());
      out.index (3) = 0; out.value() = colour[0];
      out.index (3) = 1; out.value() = colour[1];
      out.index (3) = 2; out.value() = colour[2];
    }
  }

}