/usr/include/mia-2.4/mia/template/seededwatershed.hh is in libmia-2.4-dev 2.4.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
*
* MIA 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 MIA; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef mia_internal_seededwatershed_hh
#define mia_internal_seededwatershed_hh
#include <queue>
#include <mia/template/dimtrait.hh>
NS_MIA_BEGIN
/// @cond DOC_PLUGINS
template <int dim>
class TSeededWS : public watershed_traits<dim>::Handler::Product {
public:
typedef typename watershed_traits<dim>::PNeighbourhood PNeighbourhood;
typedef typename PNeighbourhood::element_type::value_type MPosition;
typedef typename watershed_traits<dim>::Handler Handler;
typedef typename watershed_traits<dim>::FileHandler FileHandler;
typedef typename Handler::Product CFilter;
typedef typename FileHandler::Instance::DataKey DataKey;
typedef typename CFilter::Pointer PFilter;
typedef typename CFilter::Image CImage;
typedef typename CImage::Pointer PImage;
typedef typename CImage::dimsize_type Position;
TSeededWS(const DataKey& mask_image, PNeighbourhood neighborhood,
bool with_borders, bool input_is_gradient);
template <template <typename> class Image, typename T>
typename TSeededWS<dim>::result_type operator () (const Image<T>& data) const;
private:
virtual PImage do_filter(const CImage& image) const;
DataKey m_label_image_key;
PNeighbourhood m_neighborhood;
PFilter m_togradnorm;
bool m_with_borders;
};
template <int dim>
class TSeededWSFilterPlugin: public watershed_traits<dim>::Handler::Interface {
public:
typedef typename watershed_traits<dim>::PNeighbourhood PNeighbourhood;
typedef typename watershed_traits<dim>::Handler Handler;
typedef typename watershed_traits<dim>::FileHandler FileHandler;
typedef typename Handler::Product CFilter;
TSeededWSFilterPlugin();
private:
virtual CFilter *do_create()const;
virtual const std::string do_get_descr()const;
std::string m_seed_image_file;
PNeighbourhood m_neighborhood;
bool m_with_borders;
bool m_input_is_gradient;
};
template <template <typename> class Image, typename T, typename S, typename N, typename R, int dim, bool supported>
struct seeded_ws {
static R apply(const Image<T>& image, const Image<S>& seed, N n, bool with_borders);
};
template <template <typename> class Image, typename T, typename S, typename N, typename R, int dim>
struct seeded_ws<Image, T, S, N, R, dim, false> {
static R apply(const Image<T>& /*image*/, const Image<S>& /*seed*/, N /*n*/, bool /*with_borders*/) {
throw create_exception<std::invalid_argument>("C2DRunSeededWS: seed data type '", __type_descr<S>::value, "' not supported");
}
};
// helper to dispatch based on the pixel type of the seed image
template <template <typename> class Image, typename T, typename N, typename R, int dim>
struct dispatch_RunSeededWS : public TFilter<R> {
dispatch_RunSeededWS(N neighborhood, const Image<T>& image, bool with_borders):
m_neighborhood(neighborhood),
m_image(image),
m_with_borders(with_borders)
{}
template <typename S>
R operator () (const Image<S>& seed) const {
const bool supported = std::is_integral<S>::value && !std::is_same<S, bool>::value;
return seeded_ws<Image, T, S, N, R, dim, supported>::apply(m_image, seed, m_neighborhood, m_with_borders);
}
N m_neighborhood;
const Image<T>& m_image;
bool m_with_borders;
};
template <typename L, typename Position>
struct PixelWithLocation {
Position pos;
float value;
L label;
};
template <typename L, typename Position>
bool operator < (const PixelWithLocation<L,Position>& lhs, const PixelWithLocation<L,Position>& rhs)
{
mia::less_then<Position> l;
return lhs.value > rhs.value||
( lhs.value == rhs.value && l(rhs.pos, lhs.pos));
}
template <template <typename> class Image, typename T, typename S, int dim>
class TRunSeededWatershed {
public:
typedef typename watershed_traits<dim>::PNeighbourhood PNeighbourhood;
typedef typename PNeighbourhood::element_type::value_type MPosition;
typedef typename watershed_traits<dim>::Handler Handler;
typedef typename Handler::Product CFilter;
typedef typename CFilter::Pointer PFilter;
typedef typename CFilter::Image CImage;
typedef typename CImage::Pointer PImage;
typedef typename CImage::dimsize_type Position;
TRunSeededWatershed(const Image<T>& image, const Image<S>& seed, PNeighbourhood neighborhood, bool with_borders);
PImage run();
private:
void add_neighborhood(const PixelWithLocation<S, Position>& pixel);
const Image<T>& m_image;
const Image<S>& m_seed;
PNeighbourhood m_neighborhood;
Image<unsigned char> m_visited;
Image<unsigned char> m_stored;
Image<S> *m_result;
PImage m_presult;
std::priority_queue<PixelWithLocation<S, Position> > m_seeds;
S m_watershed;
bool m_with_borders;
};
template <template <typename> class Image, typename T, typename S, int dim>
TRunSeededWatershed<Image, T, S, dim>::TRunSeededWatershed(const Image<T>& image, const Image<S>& seed,
PNeighbourhood neighborhood, bool with_borders):
m_image(image),
m_seed(seed),
m_neighborhood(neighborhood),
m_visited(seed.get_size()),
m_stored(seed.get_size()),
m_watershed(std::numeric_limits<S>::max()),
m_with_borders(with_borders)
{
m_result = new Image<S>(seed.get_size(), image);
m_presult.reset(m_result);
}
template <template <typename> class Image, typename T, typename S, int dim>
void TRunSeededWatershed<Image, T, S, dim>::add_neighborhood(const PixelWithLocation<S, Position>& pixel)
{
PixelWithLocation<S, Position> new_pixel;
new_pixel.label = pixel.label;
bool hit_boundary = false;
for (auto i = m_neighborhood->begin(); i != m_neighborhood->end(); ++i) {
Position new_pos( pixel.pos + *i);
if (new_pos != pixel.pos && new_pos < m_seed.get_size()) {
if (!m_visited(new_pos)) {
if (!m_stored(new_pos) ) {
new_pixel.value = m_image(new_pos);
new_pixel.pos = new_pos;
m_seeds.push(new_pixel);
m_stored(new_pos) = 1;
}
}else{
hit_boundary |= (*m_result)(new_pos) != pixel.label &&
(*m_result)(new_pos) != m_watershed;
}
}
}
// set pixel to new label
if (!m_visited(pixel.pos)) {
m_visited(pixel.pos) = true;
(*m_result)(pixel.pos) = (m_with_borders && hit_boundary) ? m_watershed : pixel.label;
}
}
template <template <typename> class Image, typename T, typename S, int dim>
typename TRunSeededWatershed<Image,T,S,dim>::PImage
TRunSeededWatershed<Image,T,S,dim>::run()
{
// copy seed and read initial pixels
auto iv = m_visited.begin();
auto is = m_seed.begin_range(Position::_0, m_seed.get_size());
auto es = m_seed.end_range(Position::_0, m_seed.get_size());
auto ir = m_result->begin();
auto ii = m_image.begin();
auto ist = m_stored.begin();
PixelWithLocation<S, Position> pixel;
while (is != es) {
*ir = *is;
if (*is) {
*iv = 1;
*ist;
pixel.pos = is.pos();
pixel.value = *ii;
pixel.label = *is;
m_seeds.push(pixel);
}
++iv; ++is; ++ir; ++ist; ++ii;
}
while (!m_seeds.empty()) {
auto p = m_seeds.top();
m_seeds.pop();
add_neighborhood(p);
}
return m_presult;
}
template <template <typename> class Image, typename T, typename S, typename N, typename R, int dim, bool supported>
R seeded_ws<Image,T,S,N,R,dim,supported>::apply(const Image<T>& image,
const Image<S>& seed, N neighborhood, bool with_borders)
{
TRunSeededWatershed<Image, T, S, dim> ws(image, seed, neighborhood, with_borders);
return ws.run();
}
template <int dim>
TSeededWS<dim>::TSeededWS(const DataKey& label_image_key, PNeighbourhood neighborhood, bool with_borders, bool input_is_gradient):
m_label_image_key(label_image_key),
m_neighborhood(neighborhood),
m_with_borders(with_borders)
{
if (!input_is_gradient)
m_togradnorm = Handler::instance().produce("gradnorm");
}
template <int dim>
template <template <typename> class Image, typename T>
typename TSeededWS<dim>::result_type TSeededWS<dim>::operator () (const Image<T>& data) const
{
// read start label image
auto in_image_list = m_label_image_key.get();
if (!in_image_list || in_image_list->empty())
throw create_exception<std::runtime_error>( "C2DSeededWS: no seed image could be loaded");
if (in_image_list->size() > 1)
cvwarn() << "C2DSeededWS:got more than one seed image. Ignoring all but first";
auto seed = (*in_image_list)[0];
if (seed->get_size() != data.get_size()) {
throw create_exception<std::invalid_argument>( "C2DSeededWS: seed and input differ in size: seed " , seed->get_size()
, ", input " , data.get_size());
}
dispatch_RunSeededWS<Image, T, PNeighbourhood, PImage, dim> ws(m_neighborhood, data, m_with_borders);
return mia::filter(ws, *seed);
}
template <int dim>
typename TSeededWS<dim>::PImage TSeededWS<dim>::do_filter(const CImage& image) const
{
PImage result;
if (m_togradnorm) {
auto grad = m_togradnorm->filter(image);
result = mia::filter(*this, *grad);
}
else
result = mia::filter(*this, image);
return result;
}
template <int dim>
TSeededWSFilterPlugin<dim>::TSeededWSFilterPlugin():
Handler::Interface("sws"),
m_with_borders(false),
m_input_is_gradient(false)
{
this->add_parameter("seed", new CStringParameter(m_seed_image_file, CCmdOptionFlags::required_input,
"seed input image containing the lables for the initial regions"));
this->add_parameter("n", make_param(m_neighborhood, "sphere:r=1", false, "Neighborhood for watershead region growing"));
this->add_parameter("mark", new CBoolParameter(m_with_borders, false, "Mark the segmented watersheds with a special gray scale value"));
this->add_parameter("grad", new CBoolParameter(m_input_is_gradient, false, "Interpret the input image as gradient. "));
}
template <int dim>
typename TSeededWSFilterPlugin<dim>::CFilter *TSeededWSFilterPlugin<dim>::do_create()const
{
auto seed = FileHandler::instance().load_to_pool(m_seed_image_file);
return new TSeededWS<dim>(seed, m_neighborhood, m_with_borders, m_input_is_gradient);
}
template <int dim>
const std::string TSeededWSFilterPlugin<dim>::do_get_descr()const
{
return "seeded watershead. The algorithm extracts exactly so "
"many reagions as initial labels are given in the seed image.";
}
NS_MIA_END
/// @endcond
#endif
|