Commit acd9e113 authored by Guillaume Lazzara's avatar Guillaume Lazzara
Browse files

Add demos source code.

parent fab432e2
Compile
- scribo/src/content_in_doc
- scribo/demos/xml2doc/scribo-xml2doc
Les données situées dans scribo/demos/templates sont également à
copier à un emplacement et à pointer au programme scribo-xml2doc.
\ No newline at end of file
This diff is collapsed.
#ifndef CURVATURE_HH
# define CURVATURE_HH
# include <mln/core/image/image2d.hh>
# include <mln/data/fill.hh>
# include <mln/extension/fill.hh>
//# include <fstream>
//# include <iostream>
namespace mln
{
template <typename T>
image2d<double>
K2_compute_curvature(const image2d<T>& f)
{
box2d b = f.domain();
b.pmax() *= 4;
image2d<double> curv(b);
data::fill(curv, 0);
extension::fill(curv, 0);
//std::ofstream file("new.txt");
int nrows = f.nrows();
int ncols = f.ncols();
double fx, fy, fxx, fyy, fxy;
double curv_v;
for (int i = 1; i < nrows - 2; i++)
for (int j = 1; j < ncols - 2; j++)
{
curv.at_(4*i, 4*j) = f.at_(i, j);
//| edge
if(f.at_(i,j) != f.at_(i,j+1))
{
fx = ( (double)f.at_(i+1, j) + f.at_(i+1, j+1) - f.at_(i-1, j) - f.at_(i-1, j+1) )/4;
fxx= ( (double)f.at_(i+1, j) + f.at_(i+1, j+1) + f.at_(i-1, j) + f.at_(i-1, j+1)
- 2*f.at_(i, j) - 2*f.at_(i, j+1) ) /2;
fy = (double)f.at_(i, j+1) - f.at_(i, j);
fyy = ( (double)f.at_(i, j+2) + f.at_(i, j-1) - f.at_(i, j) - f.at_(i, j+1) )/2;
fxy = ( (double)f.at_(i+1, j+1) - f.at_(i+1, j) - f.at_(i-1, j+1) + f.at_(i-1, j) )/2;
curv_v = (double)(fxx*fy*fy -2*fxy*fx*fy + fyy*fx*fx)/(std::pow(fx*fx+fy*fy ,1.5));
curv.at_(4*i, 4*j+1) = curv.at_(4*i, 4*j+3) = std::abs(curv_v);
}
//edge -
if(f.at_(i,j) != f.at_(i+1,j))
{
fx = (double)f.at_(i+1, j) - f.at_(i, j);
fxx= ( (double)f.at_(i+2, j) + f.at_(i-1, j) - f.at_(i, j) - f.at_(i+1, j) )/2;
fy = ( (double)f.at_(i+1, j+1) + f.at_(i, j+1) - f.at_(i, j-1) - f.at_(i+1, j-1) )/4;
fyy = ( (double)f.at_(i, j+1) + f.at_(i+1, j+1) + f.at_(i, j-1) + f.at_(i+1, j-1)
- 2*f.at_(i, j) - 2*f.at_(i+1, j) )/2;
fxy = ( (double)f.at_(i+1, j+1) - f.at_(i, j+1) - f.at_(i+1, j-1) + f.at_(i, j-1) )/2;
curv_v = (double)(fxx*fy*fy -2*fxy*fx*fy + fyy*fx*fx)/(std::pow(fx*fx+fy*fy ,1.5));
curv.at_(4*i+1, 4*j) = curv.at_(4*i+3, 4*j) = std::abs(curv_v);
}
}
for(int i = nrows-2, j = 1; j < ncols - 2; j++)
{
if(f.at_(i,j) != f.at_(i,j+1))
{
fx = ( (double)f.at_(i+1, j) + f.at_(i+1, j+1) - f.at_(i-1, j) - f.at_(i-1, j+1) )/4;
fxx= ( (double)f.at_(i+1, j) + f.at_(i+1, j+1) + f.at_(i-1, j) + f.at_(i-1, j+1)
- 2*f.at_(i, j) - 2*f.at_(i, j+1) )/2;
fy = (double)f.at_(i, j+1) - f.at_(i, j);
fyy = ( (double)f.at_(i, j+2) + f.at_(i, j-1) - f.at_(i, j) - f.at_(i, j+1) )/2;
fxy = ( (double)f.at_(i+1, j+1) - f.at_(i+1, j) - f.at_(i-1, j+1) + f.at_(i-1, j) )/2;
curv_v = (double)(fxx*fy*fy -2*fxy*fx*fy + fyy*fx*fx)/(std::pow(fx*fx+fy*fy ,1.5));
curv.at_(4*i, 4*j+1) = curv.at_(4*i, 4*j+3) = std::abs(curv_v);
}
}
for( int j = ncols-2, i = 1; i < nrows-2; i++)
{
if(f.at_(i,j) != f.at_(i+1, j))
{
fx = (double)f.at_(i+1, j) - f.at_(i, j);
fxx= ( (double)f.at_(i+2, j) + f.at_(i-1, j) - f.at_(i, j) - f.at_(i+1, j) )/2;
fy = ( (double)f.at_(i+1, j+1) + f.at_(i, j+1) - f.at_(i+1, j-1) - f.at_(i, j-1) )/4;
fyy = ( (double)f.at_(i, j+1) + f.at_(i+1, j+1) + f.at_(i, j-1) + f.at_(i+1, j-1)
- 2*f.at_(i, j) - 2*f.at_(i+1, j) )/2;
fxy = ( (double)f.at_(i+1, j+1) - f.at_(i, j+1) - f.at_(i+1, j-1) + f.at_(i, j-1) )/2;
curv_v = (double)(fxx*fy*fy -2*fxy*fx*fy + fyy*fx*fx)/(std::pow(fx*fx+fy*fy ,1.5));
curv.at_(4*i+1, 4*j) = curv.at_(4*i+3, 4*j) = std::abs(curv_v);
}
}
//handling the edge close to image contour
//get the value of the closist edge with the same type
for(int i = 1; i < nrows-2; i++)
{
curv.at_(4*i+1, 0) = curv.at_(4*i+3,0) = curv.at_(4*i+1, 4);
curv.at_(4*i, 1) = curv.at_(4*i, 3) = curv.at_(4*i,5);
curv.at_(4*i+1, 4*(ncols-1)) = curv.at_(4*i+3, 4*(ncols-1)) = curv.at_(4*i+1,4*(ncols-2));
curv.at_(4*i, 4*(ncols-2)+1) = curv.at_(4*i, 4*(ncols-2)+3) = curv.at_(4*i,4*(ncols-3) +3);
}
for(int j = 1; j < ncols-2; j++)
{
curv.at_(0, 4*j+1) = curv.at_(0,4*j+3) = curv.at_(4, 4*j+1);
curv.at_(1,4*j) = curv.at_(3, 4*j) = curv.at_(5, 4*j);
curv.at_(4*(nrows-1), 4*j+1) = curv.at_(4*(nrows-1), 4*j+3) = curv.at_(4*(nrows-2), 4*j+1);
curv.at_(4*(nrows-2)+1, 4*j) = curv.at_(4*(nrows-2)+3, 4*j) = curv.at_(4*(nrows-3)+3, 4*j);
}
curv.at_(0,1) = curv.at_(0,3) = curv.at_(0,5);
curv.at_(1,0) = curv.at_(3,0) = curv.at_(5,0);
curv.at_(0,4*(nrows-2)+1) = curv.at_(0,4*(nrows-2)+3) = curv.at_(0,4*(nrows-3)+3);
curv.at_(1,4*(ncols-2)) = curv.at_(3,4*(ncols-2)) = curv.at_(1, 4*(ncols-1)) = curv.at_(3,4*(ncols-1))
= curv.at_(5,4*(ncols-1));
curv.at_(4*(nrows-2),1) = curv.at_(4*(nrows-2),3) = curv.at_(4*(nrows-1),1) = curv.at_(4*(nrows-1),3)
= curv.at_(4*(nrows-2), 5);
curv.at_(4*(nrows-2)+1,0) =curv.at_(4*(nrows-2)+3, 0) = curv.at_(4*(nrows-2)+1,4);
curv.at_(4*(nrows-2),4*(ncols-2)+1) = curv.at_(4*(nrows-2),4*(ncols-2)+3)
= curv.at_(4*(nrows-1),4*(ncols-2)+1) = curv.at_(4*(nrows-1),4*(ncols-2)+3)
= curv.at_(4*(nrows-3), 4*(ncols-2)+1);
curv.at_(4*(nrows-2)+1, 4*(ncols-2)) = curv.at_(4*(nrows-2)+3, 4*(ncols-2))
=curv.at_(4*(nrows-2)+1, 4*(ncols-1)) = curv.at_(4*(nrows-2)+3, 4*(ncols-1))
= curv.at_(4*(nrows-2)+1, 4*(ncols-3));
//debug::println(g);
return curv;
}
}
#endif // CURVATURE_HH
// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of Olena.
//
// Olena 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, version 2 of the License.
//
// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project without restriction. Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License. This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
#ifndef GRADIENT_HH
# define GRADIENT_HH
# include <mln/core/image/image2d.hh>
# include <mln/data/fill.hh>
# include <mln/extension/fill.hh>
namespace mln
{
template <typename T>
image2d<T>
K2_compute_gradient(const image2d<T>& f)
{
box2d b = f.domain();
b.pmax() *= 4;
image2d<T> g(b);
data::fill(g, 0);
extension::fill(g, 0);
int nrows = f.nrows();
int ncols = f.ncols();
for (int i = 0; i < nrows - 1; i++)
for (int j = 0; j < ncols - 1; j++)
{
T g0 = std::abs(f.at_(i, j) - f.at_(i, j + 1));
T g1 = std::abs(f.at_(i, j) - f.at_(i+1, j));
g.at_(i * 4, j * 4) = f.at_(i, j);
g.at_(i * 4, j * 4 + 1) = g.at_(i * 4, j * 4 + 3) = g0;
g.at_(i * 4 + 1, j * 4) = g.at_(i * 4 + 3, j * 4) = g1;
}
for (int i = 0, j = ncols - 1; i < nrows - 1; i++)
{
g.at_(i * 4, j * 4) = f.at_(i, j);
g.at_(i * 4 + 1, j * 4) = g.at_(i * 4 + 3, j * 4) =
std::abs(f.at_(i, j) - f.at_(i+1, j));
}
for (int i = nrows - 1, j = 0; j < ncols - 1; j++)
{
g.at_(i * 4, j * 4) = f.at_(i, j);
g.at_(i * 4, j * 4 + 1) = g.at_(i * 4, j * 4 + 3) =
std::abs(f.at_(i, j) - f.at_(i, j + 1));
}
g.at_((nrows-1) * 4, (ncols-1) * 4) = f.at_(nrows-1, ncols-1);
return g;
}
}
# ifndef MLN_INCLUDE_ONLY
# endif // ! MLN_INCLUDE_ONLY
#endif // !GRADIENT_HH
#ifndef UTILS_H_MEDIAN_HH
# define UTILS_H_MEDIAN_HH
# include <mln/trait/value_.hh>
namespace mln
{
namespace utils
{
template <typename T>
struct h_median
{
enum {
nbits_ = mln_nbits(T),
sup = mlc_pow_int(2, nbits_)
};
h_median()
{
clear();
}
void clear()
{
for (unsigned v = 0; v < sup; ++v)
h_[v] = 0;
sum_ = 0;
sum_minus_ = 0;
sum_plus_ = 0;
t_ = sup / 2;
valid_ = true;
}
void take(const T& t)
{
++h_[t];
++sum_;
if (t < t_)
++sum_minus_;
else if (t > t_)
++sum_plus_;
if (valid_)
valid_ = false;
}
const T& value() const
{
if (! valid_)
update_();
return t_;
}
void println() const
{
if (! valid_)
update_();
std::cout << " h = ";
for (unsigned v = 0; v < sup; ++v)
if (h_[v])
std::cout << v << ':' << h_[v] << ' ';
std::cout << std::endl;
std::cout << " v = " << t_ << std::endl;
std::cout << " sum = " << sum_minus_ << " (-) " << h_[t_] << " (+) " << sum_plus_ << std::endl;
std::cout << " bal = " << (2 * sum_minus_) << " (-) " << sum_ << " (+) " << (2 * sum_plus_) << std::endl;
}
unsigned sum() const { return sum_; }
unsigned sum_plus() const { return sum_plus_; }
unsigned sum_minus() const { return sum_minus_; }
const unsigned* histo() const { return h_; }
bool goes_up() const
{
return sum_plus_ > sum_minus_;
}
private:
mutable unsigned h_[sup];
mutable unsigned sum_;
mutable unsigned sum_minus_, sum_plus_;
mutable bool valid_;
mutable T t_; // the median current value
void update_() const
{
valid_ = true;
if (sum_ == 0)
return;
if (2 * sum_minus_ > sum_)
go_minus_();
else
if (2 * sum_plus_ > sum_)
go_plus_();
else
if (h_[t_] == 0)
{
// go to the heaviest side
if (sum_plus_ > sum_minus_)
go_plus_();
else
go_minus_(); // default when both sides are balanced
}
}
void go_minus_() const
{
do
{
sum_plus_ += h_[t_];
do
--t_;
while (h_[t_] == 0);
sum_minus_ -= h_[t_];
}
while (2 * sum_minus_ > sum_);
}
void go_plus_() const
{
do
{
sum_minus_ += h_[t_];
do
++t_;
while (h_[t_] == 0);
sum_plus_ -= h_[t_];
}
while (2 * sum_plus_ > sum_);
}
};
} // end of namespace mln::utils
} // end of namespace mln
#endif // ! UTILS_H_MEDIAN_HH
// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of Olena.
//
// Olena 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, version 2 of the License.
//
// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project without restriction. Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License. This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
#ifndef HQUEUE_HH
# define HQUEUE_HH
# include <vector>
# include "range.hh"
# include "topology.hh"
namespace mln
{
namespace utils
{
template <typename V, typename T>
class hqueue
{
public:
enum {
nvalues = mln_card(V)
};
void init(V level);
/// insertion
/// \{
void push(const T& value, const V& level);
void push(const T& value, const range<V>& level);
/// \}
/// retrieval
/// \{
T pop();
T top() const;
/// \}
bool empty() const;
const V& current_level() const { return level_; }
const std::vector<T>& operator [] (int i) const { return h_[i]; }
std::vector<T>& operator [] (int i) { return h_[i]; }
private:
void search_up_n_down() const;
mutable V level_;
std::vector<T> h_[nvalues];
};
# ifndef MLN_INCLUDE_ONLY
template <typename V, typename T>
void hqueue<V, T>::init(V level)
{
level_ = level;
}
template <typename V, typename T>
void hqueue<V, T>::push(const T& value, const V& level)
{
h_[level].push_back(value);
}
template <typename V, typename T>
void hqueue<V, T>::push(const T& value, const range<V>& level)
{
if (level.lower > level_)
h_[level.lower].push_back(value);
else if (level.upper < level_)
h_[level.upper].push_back(value);
else
h_[level_].push_back(value);
}
template <typename V, typename T>
bool hqueue<V, T>::empty() const
{
search_up_n_down();
return h_[level_].empty();
}
template <typename V, typename T>
T hqueue<V, T>::top() const
{
assert(!empty());
search_up_n_down();
return h_[level_].back();
}
template <typename V, typename T>
T hqueue<V, T>::pop()
{
assert(!empty());
search_up_n_down();
T x;
x = h_[level_].back();
h_[level_].pop_back();
return x;
}
template <typename V, typename T>
void hqueue<V, T>::search_up_n_down() const
{
while (h_[level_].empty() && level_ < (int)(nvalues - 1))
level_++;
while (h_[level_].empty() && level_ > 0)
level_--;
}
# endif
}
}
#endif // !HQUEUE_HH
// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of Olena.
//
// Olena 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, version 2 of the License.
//
// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project without restriction. Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License. This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
#ifndef INTERPOLATE_HH
# define INTERPOLATE_HH
# include <mln/core/concept/image.hh>
# include <mln/value/int_u.hh>
# include <mln/accu/stat/min_max.hh>
# include "h_median.hh"
# include "range.hh"
# include "topology.hh"