Skip to content

Commit

Permalink
Color IFT working
Browse files Browse the repository at this point in the history
  • Loading branch information
anderflash committed Oct 5, 2015
1 parent be2af12 commit 499e1a4
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 46 deletions.
Binary file added data/target.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/target.xcf
Binary file not shown.
Binary file added data/target_labels.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions data/target_seeds_indices.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
50880;50660
1 change: 1 addition & 0 deletions data/target_seeds_labels.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1;0
Binary file added data/targetbw.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/targetbw_labels.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions data/targetbw_seeds_indices.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
50880;50660
1 change: 1 addition & 0 deletions data/targetbw_seeds_labels.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0;1
9 changes: 9 additions & 0 deletions include/grafeo/ift.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,17 @@ typedef double
* @return the instance
*/
IFT* ift_new();
/**
* @brief ift_new_from_array
* @param array
* @param map_dimension
* @return
*/
IFT* ift_new_from_array(Array* array, uint8_t map_dimension);
/**
* @brief Run IFT in an array
* @param array
* @param map_dimension
* @param adjacency
* @param optimization_type
* @param weight_function
Expand All @@ -94,6 +102,7 @@ IFT* ift_new();
* @return
*/
IFT* ift_apply_array(Array* array,
uint16_t map_dimension,
Adjacency adjacency,
IFTOptimization optimization_type,
WeightFunc weight_function,
Expand Down
53 changes: 32 additions & 21 deletions src/ift.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ IFT* ift_new(){
return malloc(sizeof(IFT));
}

IFT* ift_new_from_array(Array* array){
IFT* ift_new_from_array(Array* array, uint8_t map_dimension){
IFT* ift = ift_new();
ift->label = array_zeros_like_type(array, GRAFEO_UINT16);
ift->predecessors = array_zeros_like_type(array, GRAFEO_UINT64);
ift->connectivity = array_zeros_like_type(array, GRAFEO_INT64);
ift->root = array_zeros_like_type(array, GRAFEO_UINT64);
ift->label = array_zeros(map_dimension, array->size, GRAFEO_UINT16);
ift->predecessors = array_zeros(map_dimension, array->size, GRAFEO_UINT64);
ift->connectivity = array_zeros(map_dimension, array->size, GRAFEO_INT64);
ift->root = array_zeros(map_dimension, array->size, GRAFEO_UINT64);
ift->original = array;
return ift;
}

IFT* ift_apply_array(Array *array, Adjacency adjacency, IFTOptimization optimization_type, WeightFunc weight_function, PathConnectivityFunc path_connectivity, Array* seeds_indices, Array* seeds_labels){
IFT* ift_apply_array(Array *array, uint16_t map_dimension, Adjacency adjacency, IFTOptimization optimization_type, WeightFunc weight_function, PathConnectivityFunc path_connectivity, Array* seeds_indices, Array* seeds_labels){
// IFT structure
IFT* ift = ift_new_from_array(array);
IFT* ift = ift_new_from_array(array, map_dimension);

// Auxiliary structures
Array* visited = array_zeros_like_type(array, GRAFEO_UINT8);
Array* visited = array_zeros(map_dimension, array->size, GRAFEO_UINT8);
Queue* queue = queue_new();


Expand Down Expand Up @@ -54,15 +54,15 @@ IFT* ift_apply_array(Array *array, Adjacency adjacency, IFTOptimization optimi

// Process all nodes
uint64_t index_s; // indices for nodes s and t
int64_t index_t;
int64_t index_t;
uint8_t i,num_neighbors; // for iterating neighbors
int64_t connectivity; // connectivity for extended path <r...s,t>

if (adjacency == GRAFEO_NEIGHBOR_4) num_neighbors = 4;
else if(adjacency == GRAFEO_NEIGHBOR_8) num_neighbors = 8;

int32_t* index_t_nd = malloc(sizeof(int32_t)*array->dim);
uint32_t* index_t_nd_u = malloc(sizeof(uint32_t)*array->dim);
int32_t* index_t_nd = malloc(sizeof(int32_t)*ift->label->dim);
uint32_t* index_t_nd_u = malloc(sizeof(uint32_t)*ift->label->dim);

while(!queue_is_empty(queue)){

Expand All @@ -71,7 +71,7 @@ IFT* ift_apply_array(Array *array, Adjacency adjacency, IFTOptimization optimi
pqueue_remove_begin(queue);
pqueue_shrink(queue);

uint32_t* index_nd = (uint32_t*)array_index(array, (int64_t)index_s);
uint32_t* index_nd = (uint32_t*)array_index(ift->label, (int64_t)index_s);

// Do not process this node again
array_set_element(visited, index_nd, 1);
Expand All @@ -81,17 +81,17 @@ IFT* ift_apply_array(Array *array, Adjacency adjacency, IFTOptimization optimi

// Calculate neighbor index
uint8_t j;
for(j = 0; j < array->dim; j++)
for(j = 0; j < ift->label->dim; j++)
index_t_nd[j] = (int32_t)index_nd[j] + neighbors_relative[i][j];

// Verify if it's valid (inside array region)
if(array_index_is_valid(visited, index_t_nd)){


for(j = 0; j < array->dim; j++)
for(j = 0; j < ift->label->dim; j++)
index_t_nd_u[j] = (uint32_t)index_t_nd[j];

index_t = array_index_1D(array, index_t_nd);
index_t = array_index_1D(ift->label, index_t_nd);

// If it's not visited
uint8_t* element = (uint8_t*) array_get_element(visited, index_t_nd_u);
Expand Down Expand Up @@ -180,15 +180,24 @@ void ift_set_root(IFT* ift, Array* root){
}

double path_connectivity_sum(IFT* ift, uint64_t index_s, uint64_t index_t, WeightFunc weight_function){
return ift->connectivity->data_int64[index_s] + weight_function(ift->original, index_s, index_t);
return ift->connectivity->data_int64[index_s] +
weight_function(ift->original,
index_s*ift->original->step[ift->label->dim-1],
index_t*ift->original->step[ift->label->dim-1]);
}

double path_connectivity_max(IFT* ift, uint64_t index_s, uint64_t index_t, WeightFunc weight_function){
return max(ift->connectivity->data_int64[index_s], weight_function(ift->original, index_s, index_t));
return max(ift->connectivity->data_int64[index_s],
weight_function(ift->original,
index_s*ift->original->step[ift->label->dim-1],
index_t*ift->original->step[ift->label->dim-1]));
}

double path_connectivity_min(IFT* ift, uint64_t index_s, uint64_t index_t, WeightFunc weight_function){
return min(ift->connectivity->data_int64[index_s], weight_function(ift->original, index_s, index_t));
return min(ift->connectivity->data_int64[index_s],
weight_function(ift->original,
index_s*ift->original->step[ift->label->dim-1],
index_t*ift->original->step[ift->label->dim-1]));
}

double path_connectivity_euc(IFT* ift, uint64_t index_s, uint64_t index_t, WeightFunc weight_function){
Expand Down Expand Up @@ -222,7 +231,9 @@ double weight_diff(Array *array, uint64_t index1, uint64_t index2){
}

double weight_diff_3(Array* array, uint64_t index1, uint64_t index2){
return pow(array_get_long_double_1D(array, index1) - array_get_long_double_1D(array, index2),2) +
pow(array_get_long_double_1D(array, index1+1) - array_get_long_double_1D(array, index2+1),2) +
pow(array_get_long_double_1D(array, index1+2) - array_get_long_double_1D(array, index2+2),2);
long double value1 = array_get_long_double_1D(array, index1 ) - array_get_long_double_1D(array, index2),
value2 = array_get_long_double_1D(array, index1+1) - array_get_long_double_1D(array, index2+1),
value3 = array_get_long_double_1D(array, index1+2) - array_get_long_double_1D(array, index2+2);
double result = sqrt(value1*value1 + value2*value2 + value3*value3);
return result;
}
82 changes: 57 additions & 25 deletions tests/test_ift.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ static void assert_array_equal(Array* array1, Array* array2){
assert_true(array_get_long_double_1D(array1,i) == array_get_long_double_1D(array2,i));
}

static void helper_test_ift_img(const char* imagepath,
const char* labels_filename,
const char* seeds_indices_filename,
const char* seeds_labels_filename,
PathConnectivityFunc path_connectivity, IFTOptimization ift_optimization){
static void helper_test_ift_img(const char* imagepath,
const char* labels_filename,
const char* seeds_indices_filename,
const char* seeds_labels_filename,
uint16_t map_dimension,
PathConnectivityFunc path_connectivity,
WeightFunc weight_function,
IFTOptimization ift_optimization){
// Load images and seeds
Array* image = image_read(imagepath);
Array* labels = image_read(labels_filename);
Expand All @@ -63,7 +66,7 @@ static void helper_test_ift_img(const char* imagepath,
Array* seeds_labels = array_read_csv_type(seeds_labels_filename, GRAFEO_UINT16);
Array* seeds_indices = array_read_csv_type(seeds_indices_filename, GRAFEO_UINT64);

IFT* ift = ift_apply_array(image, GRAFEO_NEIGHBOR_4, ift_optimization, weight_diff, path_connectivity, seeds_indices, seeds_labels);
IFT* ift = ift_apply_array(image, map_dimension, GRAFEO_NEIGHBOR_4, ift_optimization, weight_function, path_connectivity, seeds_indices, seeds_labels);

Array* label = array_as_type(ift->label, GRAFEO_UINT8);
Array* output = array_mult_scalar(label, 255);
Expand Down Expand Up @@ -109,7 +112,7 @@ static void helper_test_ift(PathConnectivityFunc path_connectivity, IFTOptimizat
Array* image = array_from_data(data, 2, size, GRAFEO_UINT8);

// Run standard IFT
IFT* ift = ift_apply_array(image, GRAFEO_NEIGHBOR_4, ift_optimization, weight_diff, path_connectivity, seeds_indices, seeds_labels);
IFT* ift = ift_apply_array(image, image->dim, GRAFEO_NEIGHBOR_4, ift_optimization, weight_diff, path_connectivity, seeds_indices, seeds_labels);

assert_non_null(ift);
assert_non_null(ift->connectivity);
Expand Down Expand Up @@ -157,20 +160,34 @@ static void helper_test_ift(PathConnectivityFunc path_connectivity, IFTOptimizat
}

static void helper_test_ift_rgb(PathConnectivityFunc connectivity_function, IFTOptimization optimization){
uint8_t image_dim = 2;
uint32_t size[3] = {2,2,3}; // 2x2 RGB
uint32_t seeds_size[1] = {2};
uint8_t data[12] = { 0, 0, 0, 20, 12, 23,
128,200,50, 230,130,124};
uint64_t seeds_indices_data[2]= {};
uint16_t seeds_labels_data[2] = {};
uint8_t labels_data[4] = { 0, 0,
1, 1};
Array* image = array_from_data(data , 3, size , GRAFEO_UINT8);
Array* correct_labels = array_from_data(labels_data , 2, size , GRAFEO_UINT16);
Array* seeds_indices = array_from_data(seeds_indices_data, 1, seeds_size, GRAFEO_UINT64);
Array* seeds_labels = array_from_data(seeds_labels_data , 1, seeds_size, GRAFEO_UINT16);
IFT* ift = ift_apply_array(image,GRAFEO_NEIGHBOR_4,optimization, weight_diff_3, connectivity_function, seeds_indices, seeds_labels);
uint64_t seeds_indices_data[2]= {0,3};
uint16_t seeds_labels_data[2] = {0,1};
uint16_t labels_data[4] = { 0, 0,
1, 1};
Array* image = array_from_data(data , image_dim+1 , size , GRAFEO_UINT8);
Array* correct_labels = array_from_data(labels_data , image_dim , size , GRAFEO_UINT16);
Array* seeds_indices = array_from_data(seeds_indices_data, 1 , seeds_size, GRAFEO_UINT64);
Array* seeds_labels = array_from_data(seeds_labels_data , 1 , seeds_size, GRAFEO_UINT16);
IFT* ift = ift_apply_array(image, // Array
image_dim, // Dimension of IFT Maps
GRAFEO_NEIGHBOR_4, // Adjacency
optimization, // Maximization or Minimization
weight_diff_3, // Formula for edge weights
connectivity_function, // Path connectivity functions
seeds_indices, // Indices for seeds
seeds_labels); // Labels of seeds
assert_array_equal(ift_get_label(ift), correct_labels);

array_free(image);
array_free(correct_labels);
array_free(seeds_indices);
array_free(seeds_labels);
ift_free(ift);
}

static void test_ift_sum(void** state){
Expand All @@ -180,7 +197,10 @@ static void test_ift_sum(void** state){
"../data/starbw_labels.png",
"../data/starbw_seeds_indices.csv",
"../data/starbw_seeds_labels.csv",
path_connectivity_sum, GRAFEO_IFT_MIN);
2,
path_connectivity_sum,
weight_diff,
GRAFEO_IFT_MIN);
}
static void test_ift_max(void** state){
(void) state;
Expand All @@ -189,7 +209,10 @@ static void test_ift_max(void** state){
"../data/starbw_labels.png",
"../data/starbw_seeds_indices.csv",
"../data/starbw_seeds_labels.csv",
path_connectivity_max, GRAFEO_IFT_MIN);
2,
path_connectivity_max,
weight_diff,
GRAFEO_IFT_MIN);
}
static void test_ift_min(void** state){
(void) state;
Expand All @@ -198,7 +221,10 @@ static void test_ift_min(void** state){
"../data/starbw_labels.png",
"../data/starbw_seeds_indices.csv",
"../data/starbw_seeds_labels.csv",
path_connectivity_min, GRAFEO_IFT_MAX);
2,
path_connectivity_min,
weight_diff,
GRAFEO_IFT_MAX);
}
static void test_ift_euc(void** state){
(void) state;
Expand All @@ -207,17 +233,23 @@ static void test_ift_euc(void** state){
"../data/starbw_labels.png",
"../data/starbw_seeds_indices.csv",
"../data/starbw_seeds_labels.csv",
path_connectivity_euc, GRAFEO_IFT_MIN);
2,
path_connectivity_euc,
weight_diff,
GRAFEO_IFT_MIN);
}

static void test_ift_max_rgb(void** state){
(void) state;
helper_test_ift_rgb(path_connectivity_max, GRAFEO_IFT_MIN);
helper_test_ift_img("../data/starrgb.png",
"../data/starrgb_labels.png",
"../data/starrgb_seeds_indices.csv",
"../data/starrgb_seeds_labels.csv",
path_connectivity_euc, GRAFEO_IFT_MIN);
helper_test_ift_img("../data/target.png",
"../data/target_labels.png",
"../data/target_seeds_indices.csv",
"../data/target_seeds_labels.csv",
2,
path_connectivity_max,
weight_diff_3,
GRAFEO_IFT_MIN);
}

int main(int argc, char** argv){
Expand Down

0 comments on commit 499e1a4

Please sign in to comment.