Skip to content

implemented gftt_response #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/kornia-imgproc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ version.workspace = true
[dependencies]
fast_image_resize = "5.1.0"
kornia-tensor = { workspace = true }
kornia-tensor-ops = { workspace = true }
kornia-image = { workspace = true }
num-traits = { workspace = true }
rayon = "1.10"
Expand Down
111 changes: 111 additions & 0 deletions crates/kornia-imgproc/src/features.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use kornia_image::{Image, ImageError};
use kornia_tensor_ops::TensorOps;
use rayon::prelude::*;

use crate::filter::gaussian_blur;
use crate::filter::spatial_gradient_float;

fn _get_kernel_size(sigma: f32) -> usize {
let mut ksize = (2.0 * 4.0 * sigma + 1.0) as usize;
Expand All @@ -15,6 +17,77 @@ fn _get_kernel_size(sigma: f32) -> usize {

ksize
}
///Compute the Shi-Tomasi cornerness function.
///
/// The Shi-Tomasi cornerness function is computed as the minimum eigenvalue of the gradient matrix.
///
/// Args:
/// src: The source image.
/// dst: The destination image.
pub fn gftt_response(src: &Image<f32, 1>, dst: &mut Image<f32, 1>) -> Result<(), ImageError> {
if src.size() != dst.size() {
return Err(ImageError::InvalidImageSize(
src.cols(),
src.rows(),
dst.cols(),
dst.rows(),
));
}
let mut dx = Image::from_size_val(src.size(), 0.0)?;
let mut dy = Image::from_size_val(src.size(), 0.0)?;
let _ = spatial_gradient_float(src, &mut dx, &mut dy);
let dx2: Image<f32, 1> = Image(dx.mul(&dx).unwrap());
let dy2: Image<f32, 1> = Image(dy.mul(&dy).unwrap());
let dxy: Image<f32, 1> = Image(dx.mul(&dy).unwrap());

let mut dx2_g = Image::from_size_val(src.size(), 0.0)?;
let mut dy2_g = Image::from_size_val(src.size(), 0.0)?;
let mut dxy_g = Image::from_size_val(src.size(), 0.0)?;
let _ = gaussian_blur(&dx2, &mut dx2_g, (7usize, 7usize), (1.0, 1.0));
let _ = gaussian_blur(&dy2, &mut dy2_g, (7usize, 7usize), (1.0, 1.0));
let _ = gaussian_blur(&dxy, &mut dxy_g, (7usize, 7usize), (1.0, 1.0));

let det_m = dx2_g
.mul(&dy2_g)
.unwrap()
.sub(&dxy_g.mul(&dxy_g).unwrap())
.unwrap();
let trace_m = dx2_g.add(&dy2_g).unwrap();

let e1 = trace_m
.add(
&(trace_m
.mul(&trace_m)
.unwrap()
.sub(&det_m.mul_scalar(4.0))
.unwrap()
.abs())
.powf(0.5),
)
.unwrap()
.mul_scalar(0.5);
let e2 = trace_m
.sub(
&(trace_m
.mul(&trace_m)
.unwrap()
.sub(&det_m.mul_scalar(4.0))
.unwrap()
.abs())
.powf(0.5),
)
.unwrap()
.mul_scalar(0.5);

let score = e1.min(&e2).unwrap();
dst.as_slice_mut()
.iter_mut()
.zip(score.as_slice().iter())
.for_each(|(dst_pixel, score_pixel)| {
*dst_pixel = *score_pixel;
});
Ok(())
}

/// Compute the Hessian response of an image.
///
Expand Down Expand Up @@ -132,6 +205,44 @@ pub fn dog_response(
mod tests {
use super::*;

#[test]
fn test_gftt_response() -> Result<(), ImageError> {
#[rustfmt::skip]
let src = Image::from_size_slice(
[9, 9].into(),
&[
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0,
0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0,
0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0,
0.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0,
0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0,
0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
],
)?;

let mut dst = Image::from_size_val([9, 9].into(), 0.0)?;
gftt_response(&src, &mut dst)?;

#[rustfmt::skip]
let expected_center_value = 0.1274;
assert!(
(dst.as_slice()[4 * 9 + 4] - expected_center_value).abs() < 1e-4,
"Center value should be close to expected value"
);
let max = dst
.as_slice()
.iter()
.max_by(|a, b| a.partial_cmp(b).unwrap())
.unwrap();
assert!(
(*max - expected_center_value).abs() < 1e-4,
"Max value should be close to centre value"
);
Ok(())
}
#[test]
fn test_hessian_response() -> Result<(), ImageError> {
#[rustfmt::skip]
Expand Down
7 changes: 4 additions & 3 deletions crates/kornia-imgproc/src/filter/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,12 @@ mod tests {
#[rustfmt::skip]
let img = Image::<f32, 2>::new(
size,
(0..25).into_iter().flat_map(|x| [x as f32, x as f32 + 25.0]).collect(),
(0..25).flat_map(|x| [x as f32, x as f32 + 25.0]).collect(),
)?;
static TEST_FUNCTIONS: &'static [(
#[allow(clippy::type_complexity)]
static TEST_FUNCTIONS: &[(
fn(&Image<f32, 2>, &mut Image<f32, 2>, &mut Image<f32, 2>) -> Result<(), ImageError>,
&'static str,
&str,
)] = &[
(spatial_gradient_float, "spatial_gradient_float"),
(
Expand Down
4 changes: 2 additions & 2 deletions crates/kornia-imgproc/src/pyramid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ fn get_pyramid_gaussian_kernel() -> (Vec<f32>, Vec<f32>) {
// [1.0, 4.0, 6.0, 4.0, 1.0],
// ] / 256.0

let kernel_x = vec![1.0, 4.0, 6.0, 4.0, 1.0]
let kernel_x = [1.0, 4.0, 6.0, 4.0, 1.0]
.iter()
.map(|&x| x / 16.0)
.collect();
let kernel_y = vec![1.0, 4.0, 6.0, 4.0, 1.0]
let kernel_y = [1.0, 4.0, 6.0, 4.0, 1.0]
.iter()
.map(|&x| x / 16.0)
.collect();
Expand Down
2 changes: 1 addition & 1 deletion crates/kornia-io/src/stream/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl CameraCapture {
/// # Arguments
///
/// * `config` - A trait object implementing `CameraCaptureConfig` that specifies
/// the camera configuration.
/// the camera configuration.
///
/// # Returns
///
Expand Down
2 changes: 1 addition & 1 deletion examples/dora/image-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
arrow = "54.2.1"
arrow = "54.3.0"
dora-node-api = { version = "0.3", features = ["tracing"] }
eyre = "0.6"
kornia = { version = "0.1.8"}
Loading