Skip to content

Commit bbac595

Browse files
committed
Add minor fix, nuke old code, add positivity check to tests
1 parent 4a58c61 commit bbac595

File tree

2 files changed

+16
-95
lines changed

2 files changed

+16
-95
lines changed

src/algorithm/APRConverter.hpp

Lines changed: 6 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -594,95 +594,6 @@ inline bool APRConverter<ImageType>::get_apr(APR &aAPR, PixelData<T>& input_imag
594594
return true;
595595
}
596596

597-
template<typename ImageType>
598-
template<typename T,typename S>
599-
void APRConverter<ImageType>::autoParameters(const PixelData<T> &localIntensityScale,const PixelData<S> &grad){
600-
/*
601-
* Assumes a dark background. Please use the Python interactive parameter selection for more detailed approaches.
602-
*
603-
* Finds the flatest (1% of the image) and estimates the noise level in the gradient and sets the grad threshold from that.
604-
*/
605-
606-
607-
//need to select some pixels. we need some buffer room in the image.
608-
const size_t total_required_pixels = std::min((size_t) 10*512*512,localIntensityScale.mesh.size()/2);
609-
const size_t delta = localIntensityScale.mesh.size()/total_required_pixels - 1;
610-
611-
std::vector<T> lis_buffer(total_required_pixels);
612-
std::vector<S> grad_buffer(total_required_pixels);
613-
614-
uint64_t counter = 0;
615-
uint64_t counter_sampled = 0;
616-
617-
while((counter < localIntensityScale.mesh.size()) && (counter_sampled < total_required_pixels)){
618-
619-
lis_buffer[counter_sampled] = localIntensityScale.mesh[counter];
620-
grad_buffer[counter_sampled] = grad.mesh[counter];
621-
622-
counter+=delta;
623-
counter_sampled++;
624-
625-
}
626-
627-
628-
//float min_lis = *std::min_element(lis_buffer.begin(),lis_buffer.end());
629-
float max_lis = *std::max_element(lis_buffer.begin(),lis_buffer.end());
630-
631-
std::vector<uint64_t> hist_lis;
632-
hist_lis.resize(std::ceil(max_lis)+1,0);
633-
634-
for (uint64_t i = 0; i < total_required_pixels; ++i) {
635-
auto lis_val = std::floor(lis_buffer[i]);
636-
hist_lis[lis_val]++;
637-
}
638-
639-
//Then find 5% therhold, and take the grad values from that.
640-
uint64_t prop_values = 0.05*total_required_pixels;
641-
642-
uint64_t cumsum = 0;
643-
uint64_t freq_val=0;
644-
while (cumsum < prop_values){
645-
cumsum+= hist_lis[freq_val];
646-
freq_val++;
647-
648-
}
649-
650-
std::vector<S> grad_hist;
651-
float grad_max = *std::max_element(grad_buffer.begin(),grad_buffer.end());
652-
//float grad_min = *std::max_element(grad_buffer.begin(),grad_buffer.end());
653-
654-
grad_hist.resize(std::ceil(grad_max));
655-
uint64_t grad_counter = 0;
656-
657-
for (uint64_t i = 0; i < total_required_pixels; ++i) {
658-
auto val = lis_buffer[i];
659-
660-
if(val <= freq_val){
661-
auto grad_val = grad_buffer[i];
662-
grad_hist[std::floor(grad_val)]++;
663-
grad_counter++;
664-
}
665-
}
666-
667-
auto max_it = std::max_element(grad_hist.begin(),grad_hist.end());
668-
uint64_t mode = std::distance(grad_hist.begin(),max_it);
669-
670-
float grad_th = std::round(4*mode); //magic numbers.
671-
672-
par.grad_th = grad_th;
673-
par.sigma_th = freq_val;
674-
par.sigma_th_max = 1;
675-
676-
std::cout << "Used parameters: " << std::endl;
677-
std::cout << "I_th: " << par.Ip_th << std::endl;
678-
std::cout << "sigma_th: " << par.sigma_th << std::endl;
679-
std::cout << "grad_th: " << par.grad_th << std::endl;
680-
std::cout << "relative error (E): " << par.rel_error << std::endl;
681-
std::cout << "lambda: " << par.lambda << std::endl;
682-
683-
}
684-
685-
686597
template<typename T>
687598
void compute_means(const std::vector<T>& data, float threshold, float& mean_back, float& mean_fore) {
688599
float sum_fore=0.f, sum_back=0.f;
@@ -796,12 +707,14 @@ void APRConverter<ImageType>::autoParametersLiEntropy(const PixelData<T> &image,
796707
}
797708
}
798709

799-
grad_foreground.resize(counter);
800-
lis_foreground.resize(counter);
710+
const size_t num_foreground_pixels = counter;
711+
712+
grad_foreground.resize(num_foreground_pixels); //setting size to non-zero elements.
713+
lis_foreground.resize(num_foreground_pixels);
801714

802715
/// Then we uniformly subsample these signals, as we typically don't need all elements to compute the thresholds
803-
const size_t num_elements = std::min((size_t)32*512*512, counter);
804-
const size_t delta = counter / num_elements;
716+
const size_t num_elements = std::min((size_t)32*512*512, num_foreground_pixels); //arbitrary number.
717+
const size_t delta = num_foreground_pixels / num_elements;
805718
grad_subsampled.resize(num_elements);
806719
lis_subsampled.resize(num_elements);
807720

test/APRTest.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,18 @@ bool test_auto_parameters(TestData& test_data){
287287

288288
aprConverter.get_apr(apr, test_data.img_original);
289289

290-
//This test doesn't do anything except check if it runs.
290+
auto par = apr.get_apr_parameters();
291291

292-
return success;
292+
//checks if run, and the values are positive
293+
if(par.sigma_th <= 0){
294+
success = false;
295+
}
293296

297+
if(par.grad_th <= 0){
298+
success = false;
299+
}
300+
301+
return success;
294302

295303
}
296304

0 commit comments

Comments
 (0)