Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Add well centering tests
Browse files Browse the repository at this point in the history
  • Loading branch information
garryod committed Jul 18, 2023
1 parent bc0ec06 commit 3694493
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions chimp_chomp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ ort = { version = "1.14.8", default-features = false, features = [
tokio = { workspace = true, features = ["sync"] }
url = { workspace = true }
uuid = { workspace = true }

[dev-dependencies]
approx = { version = "0.5.1" }
2 changes: 1 addition & 1 deletion chimp_chomp/src/image_loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use opencv::{
use std::path::Path;

#[derive(Debug, Deref)]
pub struct WellImage(Mat);
pub struct WellImage(pub Mat);

#[derive(Debug, Deref)]
pub struct ChimpImage(Array<f32, Ix3>);
Expand Down
61 changes: 59 additions & 2 deletions chimp_chomp/src/well_centering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use opencv::{
use std::ops::Deref;
use tokio::sync::mpsc::UnboundedSender;

fn find_well_center(image: WellImage) -> Result<Circle, anyhow::Error> {
fn find_well_location(image: WellImage) -> Result<Circle, anyhow::Error> {
let min_side = *image.deref().mat_size().iter().min().unwrap();
let mut circles = Vector::<Vec4f>::new();
hough_circles(
Expand Down Expand Up @@ -44,8 +44,65 @@ pub async fn well_centering(
error_tx: UnboundedSender<(anyhow::Error, Job)>,
) {
println!("Finding Well Center for {job:?}");
match find_well_center(image) {
match find_well_location(image) {
Ok(well_center) => well_location_tx.send((well_center, job)).unwrap(),
Err(err) => error_tx.send((err, job)).unwrap(),
}
}

#[cfg(test)]
mod tests {
use crate::{image_loading::WellImage, well_centering::find_well_location};
use approx::assert_relative_eq;
use opencv::{
core::{Mat, Point_, Scalar, CV_8UC1},
imgproc::{circle, LINE_8},
};

#[test]
fn well_center_found() {
const CENTER_X: usize = 654;
const CENTER_Y: usize = 321;
const RADIUS: f32 = 480.0;
const THICKNESS: i32 = 196;

let mut test_image = Mat::new_nd_with_default(
&[1024, 1224],
CV_8UC1,
Scalar::new(
std::u8::MAX as f64,
std::u8::MAX as f64,
std::u8::MAX as f64,
std::u8::MAX as f64,
),
)
.unwrap();
circle(
&mut test_image,
Point_ {
x: CENTER_X as i32,
y: CENTER_Y as i32,
},
RADIUS as i32 + THICKNESS / 2,
Scalar::new(0_f64, 0_f64, 0_f64, std::u8::MAX as f64),
THICKNESS,
LINE_8,
0,
)
.unwrap();

let location = find_well_location(WellImage(test_image)).unwrap();

assert_relative_eq!(
CENTER_X as f64,
location.center.x as f64,
max_relative = 8.0
);
assert_relative_eq!(
CENTER_Y as f64,
location.center.y as f64,
max_relative = 8.0
);
assert_relative_eq!(RADIUS, location.radius, max_relative = 8.0)
}
}

0 comments on commit 3694493

Please sign in to comment.