Skip to content

Commit d0c75c8

Browse files
committed
canny edge detection function
1 parent b4de986 commit d0c75c8

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/defines.rs

+10
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,13 @@ pub enum Scalar {
450450
/// 16 bit unsigned integer
451451
U16(u16),
452452
}
453+
454+
/// Canny edge detector threshold operations types
455+
#[repr(C)]
456+
#[derive(Clone, Copy, Debug, PartialEq)]
457+
pub enum CannyThresholdType {
458+
/// User has to define canny thresholds manually
459+
MANUAL = 0,
460+
/// Determine canny algorithm high threshold using Otsu algorithm automatically
461+
OTSU = 1,
462+
}

src/image/mod.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate libc;
22

33
use array::Array;
4-
use defines::{AfError, BorderType, ColorSpace, Connectivity, InterpType, YCCStd, MomentType};
4+
use defines::{AfError, BorderType, CannyThresholdType, ColorSpace, Connectivity, InterpType, YCCStd, MomentType};
55
use error::HANDLE_ERROR;
66
use util::{AfArray, DimT, HasAfEnum, MutAfArray};
77
use self::libc::{uint8_t, c_uint, c_int, c_float, c_double, c_char};
@@ -95,6 +95,9 @@ extern {
9595

9696
fn af_moments(out: MutAfArray, input: AfArray, moment: c_int) ->c_int;
9797
fn af_moments_all(out: *mut c_double, input: AfArray, moment: c_int) ->c_int;
98+
99+
fn af_canny(out: MutAfArray, input: AfArray, thres_type: c_int, low: c_float, high: c_float,
100+
swindow: c_uint, is_fast: c_int) -> c_int;
98101
}
99102

100103
/// Calculate the gradients
@@ -1216,3 +1219,32 @@ pub fn medfilt1(input: &Array, wlen: u64, etype: BorderType) -> Array {
12161219
Array::from(temp)
12171220
}
12181221
}
1222+
1223+
/// Canny edge detection operator
1224+
///
1225+
/// The Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images. A more in depth discussion on it can be found [here](https://en.wikipedia.org/wiki/Canny_edge_detector).
1226+
///
1227+
/// # Parameters
1228+
///
1229+
/// - `input` is the input image
1230+
/// - `threshold_type` helps determine if user set high threshold is to be used or not. It can take values defined by the enum [CannyThresholdType](./enum.CannyThresholdType.html)
1231+
/// - `low` is the lower threshold % of the maximum or auto-derived high
1232+
/// - `hight` is the higher threshold % of maximum value in gradient image used in hysteresis procedure. This value is ignored if [CannyThresholdType::OTSU](./enum.CannyThresholdType.html) is chosen.
1233+
/// - `sobel_window` is the window size of sobel kernel for computing gradient direction and magnitude.
1234+
/// - `is_fast` indicates if L<SUB>1</SUB> norm(faster but less accurate) is used to compute image gradient magnitude instead of L<SUB>2</SUB> norm.
1235+
///
1236+
/// # Return Values
1237+
///
1238+
/// An Array of binary type [DType::B8](./enum.DType.html) indicating edges(All pixels with
1239+
/// non-zero values are edges).
1240+
pub fn canny(input: &Array, threshold_type: CannyThresholdType, low: f32, hight: f32,
1241+
sobel_window: u32, is_fast: bool) -> Array {
1242+
unsafe {
1243+
let mut temp: i64 = 0;
1244+
let err_val = af_canny(&mut temp as MutAfArray, input.get() as AfArray,
1245+
threshold_type as c_int, low as c_float, hight as c_float,
1246+
sobel_window as c_uint, is_fast as c_int);
1247+
HANDLE_ERROR(AfError::from(err_val));
1248+
Array::from(temp)
1249+
}
1250+
}

0 commit comments

Comments
 (0)