Bsoft  2.1.0
Bernard's software package
Bsuperpixel.h
Go to the documentation of this file.
1 
9 #include "utilities.h"
10 
11 #ifndef _Bsuperpixel_
12 
13 #define NNEIGHBOR 64
14 
15 class Bsuperpixel {
16 private:
17  long idx; // index
18  long cnt; // pixel count
19  vector<double> coor; // coordinate average
20  vector<double> chan; // channel averages
21  vector<double> var; // channel variances
22  double wt; // weight
23  vector<long> nb; // neigbor list
24 public:
25  Bsuperpixel() : idx(0), cnt(0), wt(0) {
26  nb.resize(NNEIGHBOR, -1);
27  for ( long i=0; i<3; ++i ) coor.push_back(0);
28  }
29  void index(long i) { idx = i; }
30  long index() { return idx; }
31  void count(long i) { cnt = i; }
32  long count() { return cnt; }
33  void coordinates(vector<double>& c) { coor = c; }
34  vector<double>& coordinates() { return coor; }
35  void channels(long nc) {
36  chan.resize(nc, 0);
37  var.resize(nc, 0);
38  }
39  void channels(vector<double>& v) {
40  if ( v.size() != chan.size() ) {
41  cerr << "Error: vector != channels" << endl;
42  return;
43  }
44  for ( size_t i=0; i<v.size(); ++i ) chan[i] = v[i];
45  }
46  void variances(vector<double>& v) {
47  if ( v.size() != var.size() ) {
48  cerr << "Error: vector != channels" << endl;
49  return;
50  }
51  for ( size_t i=0; i<v.size(); ++i ) var[i] = v[i];
52  }
53  vector<double>& channels() { return chan; }
54  vector<double>& variances() { return var; }
55  double channel(long i) { return chan[i]; }
56  double variance(long i) { return var[i]; }
57  void weight(double w) { wt = w; }
58  double weight() { return wt; }
59  void neighbor(long i, long j) { nb[i] = j; }
60  long neighbor(long i) { return nb[i]; }
61  void neighbors(vector<long>& v) {
62  for ( size_t i=0; i<NNEIGHBOR && i<v.size(); ++i ) nb[i] = v[i];
63  }
64  void add_neighbor(long i) {
65  long j;
66  for ( j=0; j<NNEIGHBOR && nb[j] >= 0; ++j ) ;
67  if ( j < NNEIGHBOR ) nb[j] = i;
68  else {
69  cerr << "Too many neighbors for segment " << idx << endl;
70  exit(-1);
71  }
72  }
73  void clear_neighbors() {
74 // for ( long i=0; i<NNEIGHBOR; i++ ) nb[i] = -1;
75  fill(nb.begin(), nb.end(), -1);
76  }
77  void clear() {
78  cnt = 0;
79  fill(coor.begin(), coor.end(), 0);
80  fill(chan.begin(), chan.end(), 0);
81  fill(var.begin(), var.end(), 0);
82  wt = 0;
83  }
84  void add(long cx, long cy, long cz, vector<double> v) {
85  cnt++;
86  coor[0] += cx;
87  coor[1] += cy;
88  coor[2] += cz;
89  for ( size_t i=0; i<chan.size() && i<v.size(); ++i ) {
90  chan[i] += v[i];
91  var[i] += v[i]*v[i];
92  }
93  }
94  void add(vector<long> c, vector<double> v) {
95  add(c[0], c[1], c[2], v);
96  }
97  void average() {
98  if ( cnt ) {
99  coor /= cnt;
100  for ( size_t i=0; i<chan.size(); ++i ) {
101  chan[i] /= cnt;
102  var[i] /= cnt;
103  var[i] -= chan[i] * chan[i];
104  }
105  }
106  }
107  double difference2(vector<double> v) {
108  double d(0);
109  for ( size_t i=0; i<chan.size() && i<v.size(); ++i )
110  d += (chan[i] - v[i])*(chan[i] - v[i]);
111  return d;
112  }
113  void scale(double s) {
114  coor *= s;
115  cnt *= s;
116  }
117  void show() {
118  cout << idx << tab << cnt;
119  for ( auto const& i : coor ) cout << tab << i;
120  for ( auto const& i : chan ) cout << tab << i;
121  for ( auto const& i : var ) cout << tab << i;
122  cout << endl;
123  }
124 };
125 
126 #define _Bsuperpixel_
127 #endif
Bsuperpixel::neighbor
long neighbor(long i)
Definition: Bsuperpixel.h:60
Bsuperpixel::weight
double weight()
Definition: Bsuperpixel.h:58
Bsuperpixel::add
void add(vector< long > c, vector< double > v)
Definition: Bsuperpixel.h:94
Bsuperpixel::neighbor
void neighbor(long i, long j)
Definition: Bsuperpixel.h:59
Bsuperpixel::index
void index(long i)
Definition: Bsuperpixel.h:29
Bsuperpixel::channels
vector< double > & channels()
Definition: Bsuperpixel.h:53
Bsuperpixel
Definition: Bsuperpixel.h:15
Bsuperpixel::channel
double channel(long i)
Definition: Bsuperpixel.h:55
Bsuperpixel::variances
void variances(vector< double > &v)
Definition: Bsuperpixel.h:46
Bsuperpixel::add
void add(long cx, long cy, long cz, vector< double > v)
Definition: Bsuperpixel.h:84
Bsuperpixel::count
void count(long i)
Definition: Bsuperpixel.h:31
Bsuperpixel::scale
void scale(double s)
Definition: Bsuperpixel.h:113
Bsuperpixel::average
void average()
Definition: Bsuperpixel.h:97
Bsuperpixel::weight
void weight(double w)
Definition: Bsuperpixel.h:57
Bsuperpixel::show
void show()
Definition: Bsuperpixel.h:117
Bsuperpixel::difference2
double difference2(vector< double > v)
Definition: Bsuperpixel.h:107
Bsuperpixel::add_neighbor
void add_neighbor(long i)
Definition: Bsuperpixel.h:64
Bsuperpixel::coordinates
void coordinates(vector< double > &c)
Definition: Bsuperpixel.h:33
Bsuperpixel::variance
double variance(long i)
Definition: Bsuperpixel.h:56
Bsuperpixel::coordinates
vector< double > & coordinates()
Definition: Bsuperpixel.h:34
NNEIGHBOR
#define NNEIGHBOR
Definition: Bsuperpixel.h:13
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bsuperpixel::index
long index()
Definition: Bsuperpixel.h:30
Bsuperpixel::variances
vector< double > & variances()
Definition: Bsuperpixel.h:54
Bsuperpixel::channels
void channels(long nc)
Definition: Bsuperpixel.h:35
Bsuperpixel::count
long count()
Definition: Bsuperpixel.h:32
Bsuperpixel::channels
void channels(vector< double > &v)
Definition: Bsuperpixel.h:39
Bsuperpixel::neighbors
void neighbors(vector< long > &v)
Definition: Bsuperpixel.h:61
Bsuperpixel::clear_neighbors
void clear_neighbors()
Definition: Bsuperpixel.h:73
Bsuperpixel::clear
void clear()
Definition: Bsuperpixel.h:77
Bsuperpixel::Bsuperpixel
Bsuperpixel()
Definition: Bsuperpixel.h:25