solvastro 0.1.1

A Rust blind plate solver
Documentation
use crate::create_query::{PointDetector, QueryPointCandidate};
use image::GrayImage;

const POINT_SKIP_STEP: u32 = 3;
const POINT_EXCLUSION_RADIUS: f64 = 20.0;
const POINT_THRESHOLD: u8 = 45;
const STEP_X: u32 = 1;
const STEP_Y: u32 = 1;
pub const PATCH_SIZE: u32 = 20;

pub struct PointDetectPeak {}

impl PointDetector for PointDetectPeak {
    fn extract_from_img(&self, img: &GrayImage) -> Vec<QueryPointCandidate> {
        let mut points: Vec<QueryPointCandidate> = vec![];

        let (img_w, img_h) = img.dimensions();
        let mut x: u32 = 0;
        let mut y: u32 = 0;

        while y < img_h {
            while x < img_w {
                if inside_existing_point(x, y, &points) {
                    x += POINT_SKIP_STEP;
                    continue;
                }

                let val: u8 = img.get_pixel(x, y)[0];

                if val > POINT_THRESHOLD {
                    let mut curr_val: u8 = val;
                    let mut point_x = x;
                    while point_x + 1 < img_w && img.get_pixel(point_x + 1, y)[0] > curr_val {
                        point_x += 1;
                        curr_val = img.get_pixel(point_x, y)[0];
                    }

                    let mut point_y = y;
                    while point_y + 1 < img_h && img.get_pixel(point_x, point_y + 1)[0] > curr_val {
                        point_y += 1;
                        curr_val = img.get_pixel(point_x, point_y)[0];
                    }

                    points.push(QueryPointCandidate {
                        x: point_x as f64,
                        y: point_y as f64,
                    });
                }

                x += STEP_X;
            }
            x = 0;
            y += STEP_Y;
        }

        points
    }
}

pub fn inside_existing_point(x: u32, y: u32, points: &[QueryPointCandidate]) -> bool {
    points.iter().any(|point| {
        let xd = (point.x - (x as f64)).abs();
        let yd = (point.y - (y as f64)).abs();
        let radius = POINT_EXCLUSION_RADIUS;
        xd < radius && yd < radius && ((xd * xd) + (yd * yd)).sqrt() < radius
    })
}