Skip to content

Commit

Permalink
moves from integers to doubles for centers calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Nowosad committed Mar 28, 2024
1 parent 8c7b013 commit eb02c98
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/create_connectivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ void Slic::create_connectivity(doubles_matrix<> vals, cpp11::function avg_fun_fu

clusters = new_clusters;

vector<vector<int> > new_centers;
vector<vector<double> > new_centers;
vector<vector<double> > new_centers_vals;
vector<int> new_center_counts(label);

/* Clear the center values. */
/* Clear the center _vals values. */
for (int m = 0; m < (int) label; m++) {

vector<int> new_center(2);
vector<double> new_center(2);
new_center[0] = new_center[1] = 0;
new_centers.push_back(new_center);

Expand Down
12 changes: 6 additions & 6 deletions src/private_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ void Slic::create_centers(vector<int> mat_dims, doubles_matrix<> vals,
std::string& dist_name, cpp11::function dist_fun, double step) {
for (int ncolcenter = step/2; ncolcenter < mat_dims[1]; ncolcenter += step){
for (int nrowcenter = step/2; nrowcenter < mat_dims[0]; nrowcenter += step){
vector<int> center; center.reserve(2);
vector<double> center; center.reserve(2);
int ncell = ncolcenter + (nrowcenter * mat_dims[1]);
vector<double> colour; colour.reserve(mat_dims[2]);
for (int nval = 0; nval < mat_dims[2]; nval++){
double val = vals(ncell, nval);
colour.push_back(val);
}

vector<int> lm = find_local_minimum(vals, nrowcenter, ncolcenter, dist_name, dist_fun);
vector<double> lm = find_local_minimum(vals, nrowcenter, ncolcenter, dist_name, dist_fun);

/* Generate the center vector. */
center.push_back(lm[0]);
Expand All @@ -53,15 +53,15 @@ void Slic::create_centers2(vector<int> mat_dims,
int ncell = 0;
for (int i = 0; i < input_centers.nrow(); i++){
int nrowcenter = input_centers(i, 1); int ncolcenter = input_centers(i, 0);
vector<int> center; center.reserve(2);
vector<double> center; center.reserve(2);
vector<double> colour; colour.reserve(mat_dims[2]);
for (int nval = 0; nval < mat_dims[2]; nval++){
double val = vals(ncell, nval);
colour.push_back(val);
}
ncell++;

vector<int> lm = find_local_minimum(vals, nrowcenter, ncolcenter, dist_name, dist_fun);
vector<double> lm = find_local_minimum(vals, nrowcenter, ncolcenter, dist_name, dist_fun);

/* Generate the center vector. */
center.push_back(lm[0]);
Expand Down Expand Up @@ -120,11 +120,11 @@ double Slic::compute_dist(int& ci, int& y, int& x, vector<double>& values,
return sqrt((dist1 * dist1) + (dist2 * dist2));
}

vector<int> Slic::find_local_minimum(doubles_matrix<> vals, int& y, int& x,
vector<double> Slic::find_local_minimum(doubles_matrix<> vals, int& y, int& x,
std::string& dist_name, cpp11::function dist_fun) {
double min_grad = FLT_MAX;

vector<int> loc_min(2);
vector<double> loc_min(2);
loc_min.at(0) = y;
loc_min.at(1) = x;

Expand Down
6 changes: 3 additions & 3 deletions src/slic.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Slic {
vector<int> mat_dims;

/* The values of the centers. */
vector<vector<int> > centers;
vector<vector<double> > centers;
vector<vector<double> > centers_vals;

/* The number of occurrences of each center. */
Expand Down Expand Up @@ -59,7 +59,7 @@ class Slic {
std::string& dist_name, cpp11::function dist_fun);

/* Find the pixel with the lowest gradient in a 3x3 surrounding. */
vector<int> find_local_minimum(doubles_matrix<> vals, int& y, int& x,
vector<double> find_local_minimum(doubles_matrix<> vals, int& y, int& x,
std::string& dist_name, cpp11::function dist_fun);

/* Remove and initialize the 2d vectors. */
Expand All @@ -86,7 +86,7 @@ class Slic {

/* Writes the results to a format usable by R/cpp11. */
/* Returns final pixel assignments */
writable::integers_matrix<> return_centers();
writable::doubles_matrix<> return_centers();
/* Returns supercells centers*/
writable::integers_matrix<> return_clusters();
/* Returns supercells centers' values*/
Expand Down
4 changes: 2 additions & 2 deletions src/writers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ writable::integers_matrix<> Slic::return_clusters(){
return result;
}

writable::integers_matrix<> Slic::return_centers(){
writable::integers_matrix<> result(centers.size(), 2);
writable::doubles_matrix<> Slic::return_centers(){
writable::doubles_matrix<> result(centers.size(), 2);
for (int i = 0; i < (int) centers.size(); i++){
result(i, 1) = centers[i][0]; /*y*/
result(i, 0) = centers[i][1]; /*x*/
Expand Down

0 comments on commit eb02c98

Please sign in to comment.