@@ -25,36 +25,35 @@ void Optimalization::read_user_mask(std::string path_to_mask){
25
25
26
26
}
27
27
28
- void Optimalization::single_option_optimization (cv::Mat &img, AlgorithmOptions &options, std::string option_class, size_t option_index,bool uneven,int max_iterations, double resolution){
28
+ void Optimalization::single_option_optimization (cv::Mat &img, AlgorithmOptions &options, std::string option_class, size_t option_index,bool uneven,int max_iterations, int min_start, double resolution){
29
29
// Rescale the image to given ratio
30
30
const int opt_size = int (img.cols * resolution); // Material should be a square
31
31
cv::Mat image, result;
32
32
img.copyTo (image);
33
33
Transformations::square_and_resize (image,opt_size);
34
- double precision = 1 ;
35
- int best_val = 0 ;
34
+ double precision = 0.0 , new_precision = 0.0 ;
36
35
// get the value of the option, and define max and minimum values.
37
36
if (options.get_db_var (option_index,option_class) == AlgorithmOptions::MAX_DB){
38
37
// Then we work with integers
39
38
// With integers it is easier, just iterate over every value from 0 to 10
40
39
simulate_watershed (image,result,options);
40
+ int best_val = options.get_int_var (option_index,option_class);
41
41
precision = calculate_intersection_over_union (result);
42
- std::cout << " PREC: " << precision << std::endl;
43
- best_val = options.get_int_var (option_index, option_class);
44
- for (int x = 1 ; x < max_iterations; x++){
42
+ for (int x = min_start; x < max_iterations; x++){
45
43
if (uneven && x !=1 ){
46
44
x+=1 ;
47
45
}
48
- // options.set_int_value(option_index, option_class, x);
46
+ options.set_int_value (option_index, option_class, x);
49
47
simulate_watershed (image,result,options);
50
- double new_precision = calculate_intersection_over_union (result);
48
+ new_precision = calculate_intersection_over_union (result);
49
+ // std::cout << "PRECISION: " << new_precision << std::endl;
51
50
if (new_precision > precision){
52
51
precision = new_precision;
53
52
std:: cout << " Found new best value for " << option_index << " : " << x << " || Precision: " << precision << std::endl;
54
53
best_val = x;
55
54
}
56
55
}
57
- // options.set_int_value(option_index, option_class, best_val);
56
+ options.set_int_value (option_index, option_class, best_val);
58
57
}
59
58
else {
60
59
@@ -76,9 +75,9 @@ double Optimalization::calculate_intersection_over_union(cv::Mat &image){
76
75
this ->find_unique_values (this ->user_mask_matrix ,unique_in_user_matrix);
77
76
// Then, calculate the intersection per object, including it's size
78
77
double val_sum = 0 ;
79
- int pixel_sum = 0 ;
78
+ long int pixel_sum = 0 ;
80
79
for (size_t x = 0 ; x < unique_in_user_matrix.size (); x++){
81
- std::pair<double ,int > result = this ->calc_intersection_over_union_per_object (unique_in_user_matrix[x],image);
80
+ std::pair<double ,long int > result = this ->calc_intersection_over_union_per_object (unique_in_user_matrix[x],image);
82
81
val_sum += result.first * result.second ;
83
82
pixel_sum += result.second ;
84
83
}
@@ -101,7 +100,7 @@ void Optimalization::find_unique_values(cv::Mat &matrix, std::vector<int> &stora
101
100
}
102
101
}
103
102
104
- void Optimalization::sum_up_storage (std::map<int ,int > &storage, int object_value){
103
+ void Optimalization::sum_up_storage (std::map<long int ,long int > &storage, int object_value){
105
104
// Find if key already exists
106
105
auto it = storage.find (object_value);
107
106
if (it == storage.end ()){
@@ -114,20 +113,21 @@ void Optimalization::sum_up_storage(std::map<int,int> &storage, int object_value
114
113
115
114
// I KNOW THIS ALGORITHM CAN BE MORE OPTIMAL, BUT I DID IT THEY WAY I DID IT, SO SCREW IT
116
115
// I mean, it could be reduced from in final version O(p(2n^2)), where p=nr of objects in user_mask, to just O(2n^2) ...
117
- std::pair<double ,int > Optimalization::calc_intersection_over_union_per_object (int object_id, cv::Mat &matrix){
116
+ std::pair<double ,long int > Optimalization::calc_intersection_over_union_per_object (int object_id, cv::Mat &matrix){
118
117
// Working in O(2(n^2)) time, where n is width=height of matrix
119
- cv::Mat mask = this ->user_mask_matrix == object_id;
118
+ cv::Mat mask;
119
+ cv::compare (this ->user_mask_matrix , object_id, mask, cv::CMP_EQ);
120
120
if (mask.size () != matrix.size ()){
121
121
throw std::invalid_argument (" Error, trying to compare user mask, and argument mask, they are different in size" );
122
122
}
123
123
// Creating a storage for calculating intersection over union
124
- std::map<int ,int > intersection_storage;
125
- std::map<int ,int > union_storage;
126
- int overall_matrix = 0 ;
127
- for (int x=0 ; x < mask.cols ; x++){
128
- for (int y=0 ;y<mask.rows ;y++){
129
- int val_mask = mask.at <int >(x,y);
130
- int val_matrix = matrix.at <int >(x,y);
124
+ std::map<long int ,long int > intersection_storage;
125
+ std::map<long int ,long int > union_storage;
126
+ long int overall_matrix = 0 ;
127
+ for (int x=0 ; x < mask.rows ; x++){
128
+ for (int y=0 ;y<mask.cols ;y++){
129
+ int val_mask = mask.at <uchar >(x,y);
130
+ int val_matrix = matrix.at <uchar >(x,y);
131
131
if (val_mask != 0 ){
132
132
sum_up_storage (intersection_storage,val_matrix);
133
133
overall_matrix += 1 ;
@@ -137,9 +137,9 @@ std::pair<double,int> Optimalization::calc_intersection_over_union_per_object(in
137
137
}
138
138
}
139
139
// Now, find the maximum value of intersection over union, here only the maximum value is in interest
140
- double max_val = 0 ;
140
+ double max_val = 0.0 ;
141
141
for (const auto &iter : intersection_storage){
142
- int key = iter.first ;
142
+ long int key = iter.first ;
143
143
if (key == 1 )continue ; // Exclude background
144
144
int to_add = -1 ;
145
145
// Find the same key in union_storage
@@ -150,14 +150,14 @@ std::pair<double,int> Optimalization::calc_intersection_over_union_per_object(in
150
150
if (to_add == -1 ){
151
151
to_add = union_val->second ;
152
152
}
153
- int intersec_value = iter.second ;
154
- int union_value = overall_matrix - intersec_value + to_add;
153
+ long int intersec_value = iter.second ;
154
+ long int union_value = overall_matrix - intersec_value + to_add;
155
155
double IoU = (double )intersec_value / union_value;
156
156
if (IoU > max_val){
157
157
max_val = IoU;
158
158
}
159
159
}
160
- return std::pair<double ,int >(max_val,overall_matrix);
160
+ return std::pair<double ,long int >(max_val,overall_matrix);
161
161
}
162
162
163
163
void Optimalization::simulate_watershed (cv::Mat &img, cv::Mat &out, AlgorithmOptions &options){
0 commit comments