Skip to content

Commit f77d4ae

Browse files
committed
Add Image Moments functions
1 parent 8564b2d commit f77d4ae

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/defines.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,14 @@ pub enum MarkerType {
333333
PLUS = 6,
334334
STAR = 7
335335
}
336+
337+
/// Image moment types
338+
#[repr(C)]
339+
#[derive(Clone, Copy, Debug, PartialEq)]
340+
pub enum MomentType {
341+
M00 = 1, // 1<<0
342+
M01 = 2, // 1<<1
343+
M10 = 4, // 1<<2
344+
M11 = 8, // 1<<3
345+
FIRST_ORDER = 1<<0 | 1<<1 | 1<<2 | 1<<3
346+
}

src/image/mod.rs

Lines changed: 48 additions & 1 deletion
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};
4+
use defines::{AfError, BorderType, ColorSpace, Connectivity, InterpType, YCCStd, MomentType};
55
use error::HANDLE_ERROR;
66
use util::HasAfEnum;
77
use self::libc::{uint8_t, c_uint, c_int, c_float, c_double};
@@ -93,6 +93,9 @@ extern {
9393
fn af_rgb2ycbcr(out: MutAfArray, input: AfArray, stnd: c_int) -> c_int;
9494
fn af_is_image_io_available(out: *mut c_int) -> c_int;
9595
fn af_transform_coordinates(out: MutAfArray, tf: AfArray, d0: c_float, d1: c_float) -> c_int;
96+
97+
fn af_moments(out: MutAfArray, input: AfArray, moment: c_int) ->c_int;
98+
fn af_moments_all(out: *mut c_double, input: AfArray, moment: c_int) ->c_int;
9699
}
97100

98101
/// Calculate the gradients
@@ -1136,3 +1139,47 @@ pub fn transform_coords(tf: &Array, d0: f32, d1: f32) -> Array {
11361139
Array::from(temp)
11371140
}
11381141
}
1142+
1143+
/// Find Image moments
1144+
///
1145+
/// # Parameters
1146+
///
1147+
/// - `input` is the input image
1148+
/// - `moment` is the type of moment to be computed, takes a value of
1149+
/// [enum](./enum.MomentType.html)
1150+
///
1151+
/// # Return Values
1152+
///
1153+
/// Moments Array
1154+
pub fn moments(input: &Array, moment: MomentType) -> Array {
1155+
unsafe {
1156+
let mut temp: i64 = 0;
1157+
let err_val = af_moments(&mut temp as MutAfArray,
1158+
input.get() as AfArray,
1159+
moment as c_int);
1160+
HANDLE_ERROR(AfError::from(err_val));
1161+
Array::from(temp)
1162+
}
1163+
}
1164+
1165+
/// Find Image moment for whole image
1166+
///
1167+
/// # Parameters
1168+
///
1169+
/// - `input` is the input image
1170+
/// - `moment` is the type of moment to be computed, takes a value of
1171+
/// [enum](./enum.MomentType.html)
1172+
///
1173+
/// # Return Values
1174+
///
1175+
/// Moment value of the whole image
1176+
pub fn moments_all(input: &Array, moment: MomentType) -> f64 {
1177+
unsafe {
1178+
let mut temp: f64 = 0.0;
1179+
let err_val = af_moments_all(&mut temp as *mut c_double,
1180+
input.get() as AfArray,
1181+
moment as c_int);
1182+
HANDLE_ERROR(AfError::from(err_val));
1183+
temp
1184+
}
1185+
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mod device;
4747
pub use defines::{DType, AfError, Backend, ColorMap, YCCStd, HomographyType};
4848
pub use defines::{InterpType, BorderType, MatchType, NormType};
4949
pub use defines::{Connectivity, ConvMode, ConvDomain, ColorSpace, MatProp};
50-
pub use defines::{MarkerType};
50+
pub use defines::{MarkerType, MomentType};
5151
mod defines;
5252

5353
pub use dim4::Dim4;
@@ -74,6 +74,7 @@ pub use image::{gradient, histogram, hist_equal, regions};
7474
pub use image::{gray2rgb, rgb2gray, hsv2rgb, rgb2hsv, color_space};
7575
pub use image::{bilateral, mean_shift, medfilt, sobel};
7676
pub use image::{unwrap, wrap, sat, rgb2ycbcr, ycbcr2rgb, is_imageio_available, transform_coords};
77+
pub use image::{moments, moments_all};
7778
mod image;
7879

7980
pub use lapack::{svd, lu, qr, cholesky, solve, solve_lu, inverse, det, rank, norm};

0 commit comments

Comments
 (0)