Fix inner/outer detection in contour tracing

This commit is contained in:
cdo committed 2026-11-02 11:15:27 +00:00
1 parent f25e9a00a8
commit f68fa6f0b7
2 files changed
+187 -36

No files matched your search

+21 -13
View File
@@ -1,30 +1,38 @@
mod common;
use ccvl::kernel::Kernel;
use ccvl::*;
fn draw(contour: &contour::Contour, img: &mut image::Image<image::SRGBA>) {
let n = contour.vertices.len();
fn draw(
img: &mut image::Image<image::SRGBA>,
path: &Vec<(usize, usize)>,
colour: image::SRGBA,
) {
let n = path.len();
for i in 0..n {
let (x0, y0) = contour.vertices[i];
let (x1, y1) = contour.vertices[(i + 1) % n];
let colour = match contour.kind {
contour::Kind::Outer => colours::GREEN,
contour::Kind::Inner => colours::BLUE,
};
let (x0, y0) = path[i];
let (x1, y1) = path[(i + 1) % n];
draw::line(x0, y0, x1, y1, img, colour);
}
}
fn approx(a: f32, b: f32, epsilon: f32) -> bool {
(a - b).abs() <= epsilon
}
fn main() -> std::process::ExitCode {
common::process_image(|mut img| {
let greyscale = img.map(|p| image::Luminance::from(p));
let thresholded = filter::threshold_auto(greyscale);
let contours = contour::find(thresholded);
let blurred = kernel::gaussian::<5>(1.2).convolve_sat(&greyscale);
let thresholded = filter::threshold_auto(blurred);
let contours =
contour::find(&thresholded, image::Luminance { y: 0.0 });
for contour in contours {
draw(&contour, &mut img);
draw(&mut img, &contour.vertices, match contour.kind {
contour::Kind::Outer => colours::GREEN,
contour::Kind::Inner => colours::BLUE,
});
}
img
})
}