Skip to content

Commit

Permalink
Fix bogus error message when estimating floor/ceiling from bracketed …
Browse files Browse the repository at this point in the history
…images.

Also add improve the estimation itself a bit.
  • Loading branch information
cessen committed Feb 14, 2024
1 parent b09ec31 commit d5552e0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/bin/lut_maker/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ impl AppMain {
let histogram_sets =
bracket_images_to_histogram_sets(&*bracket_image_sets.lock());

if histogram_sets.len() < 2 {
if histogram_sets.iter().all(|hs| hs[0].len() < 2 || hs[1].len() < 2 || hs[2].len() < 2) {
status.lock_mut().log_warning(format!(
"Not enough valid images to estimate floor. Check that you have at least one dark image or at least two bracketed exposure images with exposure Exif data.",
));
Expand All @@ -396,6 +396,9 @@ impl AppMain {
return;
}
for i in 0..3 {
if histograms[i].len() < 2 {
continue;
}
let norm = 1.0 / (histograms[i][0].0.buckets.len() - 1) as f32;
if let Some((f, _)) = estimate_sensor_floor_ceiling(&histograms[i]) {
if let Some(ref mut floor) = floor[i] {
Expand Down Expand Up @@ -444,7 +447,7 @@ impl AppMain {
.set_progress(format!("Estimating sensor ceiling"), 0.0);

let histogram_sets = bracket_images_to_histogram_sets(&*bracket_image_sets.lock());
if histogram_sets.len() < 2 {
if histogram_sets.iter().all(|hs| hs[0].len() < 2 || hs[1].len() < 2 || hs[2].len() < 2) {
status.lock_mut().log_warning(format!(
"Not enough valid images to estimate ceiling. Check that you have at least two bracketed exposure images with exposure Exif data.",
));
Expand All @@ -458,6 +461,9 @@ impl AppMain {
return;
}
for i in 0..3 {
if histograms[i].len() < 2 {
continue;
}
let norm = 1.0 / (histograms[i][0].0.buckets.len() - 1) as f32;
if let Some((_, c)) = estimate_sensor_floor_ceiling(&histograms[i]) {
if let Some(ref mut ceiling) = ceiling[i] {
Expand Down
23 changes: 20 additions & 3 deletions sub_crates/sensor_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ pub fn estimate_sensor_floor_ceiling(histograms: &[(Histogram, f32)]) -> Option<
.iter()
.fold(&histograms[0], |a, b| if a.1 > b.1 { a } else { b });

let darkest_pixel = lowest_exposed
.0
.buckets
.iter()
.position(|c| *c > 0)
.unwrap_or(0) as f32;
let brightest_pixel = highest_exposed
.0
.buckets
.iter()
.rposition(|c| *c > 0)
.unwrap_or(highest_exposed.0.buckets.len() - 1) as f32;

let mut sensor_floor = 0.0f32;
let mut sensor_ceiling = (bucket_count - 1) as f32;
for i in 0..histograms.len() {
Expand All @@ -116,7 +129,9 @@ pub fn estimate_sensor_floor_ceiling(histograms: &[(Histogram, f32)]) -> Option<
let tmp_i = ((ratio * LOOSENESS) as usize).min(bucket_count * 3 / 4);
if ratio >= 8.0 && tmp_i > 0 {
let target_sum = histograms[i].0.sum_under(tmp_i);
sensor_floor = sensor_floor.max(lowest_exposed.0.find_sum_lerp(target_sum));
sensor_floor = sensor_floor
.max(lowest_exposed.0.find_sum_lerp(target_sum))
.max(darkest_pixel);
}

// Ceiling.
Expand All @@ -125,7 +140,9 @@ pub fn estimate_sensor_floor_ceiling(histograms: &[(Histogram, f32)]) -> Option<
if ratio <= 0.125 && tmp_i < (bucket_count - 1) {
let target_sum = histograms[i].0.sum_under(tmp_i);
if target_sum > (total_samples / 2) {
sensor_ceiling = sensor_ceiling.min(highest_exposed.0.find_sum_lerp(target_sum));
sensor_ceiling = sensor_ceiling
.min(highest_exposed.0.find_sum_lerp(target_sum))
.min(brightest_pixel);
}
}
}
Expand All @@ -134,7 +151,7 @@ pub fn estimate_sensor_floor_ceiling(histograms: &[(Histogram, f32)]) -> Option<
// Otherwise return the result, appropriately snapped to image
// quantization.
if ((sensor_ceiling - sensor_floor) / bucket_count as f32) >= 0.8 {
Some((sensor_floor.floor(), sensor_ceiling.ceil()))
Some((sensor_floor, sensor_ceiling.ceil()))
} else {
// Failed basic sanity check.
None
Expand Down

0 comments on commit d5552e0

Please sign in to comment.