pub mod glcm;
pub mod features;
#[cfg(test)]
mod tests {
use core::f32::consts::PI;
use crate::{glcm::*, features::{calculate_feature, FeatureType}};
use ndarray::prelude::*;
#[test]
fn glcm_test_from_skimage() {
let result = calculate_glcm_matrix(
array![
[0, 0, 1, 1],
[0, 0, 1, 1],
[0, 2, 2, 2],
[2, 2, 3, 3]
],
&[1.0],
&[0.0, PI / 4.0, PI / 2.0, 3.0 * (PI / 4.0)],
4,
false
);
assert_eq!(
result.slice(s![.., .., 0, 0]),
array![
[2.0, 2.0, 1.0, 0.0],
[0.0, 2.0, 0.0, 0.0],
[0.0, 0.0, 3.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
]
);
assert_eq!(
result.slice(s![.., .., 0, 1]),
array![
[1.0, 1.0, 3.0, 0.0],
[0.0, 1.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 2.0],
[0.0, 0.0, 0.0, 0.0],
]
);
assert_eq!(
result.slice(s![.., .., 0, 2]),
array![
[3.0, 0.0, 2.0, 0.0],
[0.0, 2.0, 2.0, 0.0],
[0.0, 0.0, 1.0, 2.0],
[0.0, 0.0, 0.0, 0.0],
]
);
assert_eq!(
result.slice(s![.., .., 0, 3]),
array![
[2.0, 0.0, 0.0, 0.0],
[1.0, 1.0, 2.0, 0.0],
[0.0, 0.0, 2.0, 1.0],
[0.0, 0.0, 0.0, 0.0],
]
);
}
fn round(x: f32, decimals: u32) -> f32 {
let y = 10i32.pow(decimals) as f32;
(x * y).round() / y
}
fn assert_eq_round(left: Array2<f32>, right: Array2<f32>, message: String) {
assert_eq!(
left.mapv(|x| round(x, 7)),
right.mapv(|x| round(x, 7)),
"{}",
message
)
}
#[test]
#[allow(clippy::excessive_precision)]
fn contrast_feature() {
let p = calculate_glcm_matrix(
array![
[0, 0, 1, 1],
[0, 0, 1, 1],
[0, 2, 2, 2],
[2, 2, 3, 3]
],
&[1.0, 2.0],
&[0.0, PI / 2.0],
4,
true
);
assert_eq_round(
calculate_feature(&p, FeatureType::Contrast),
array![
[0.583333_3, 1.0],
[1.25, 2.75]
],
format!("Feature type: {:?}", FeatureType::Contrast)
);
assert_eq_round(
calculate_feature(&p, FeatureType::Dissimilarity),
array![
[0.41666666, 0.6666667],
[1.0, 1.5]
],
format!("Feature type: {:?}", FeatureType::Dissimilarity)
);
assert_eq_round(
calculate_feature(&p, FeatureType::Homogeneity),
array![
[0.80833333, 0.7],
[0.525, 0.375]
],
format!("Feature type: {:?}", FeatureType::Homogeneity)
);
assert_eq_round(
calculate_feature(&p, FeatureType::Energy),
array![
[0.40824829, 0.42491829],
[0.58630197, 0.53033009],
],
format!("Feature type: {:?}", FeatureType::Energy)
);
assert_eq_round(
calculate_feature(&p, FeatureType::ASM),
array![
[0.16666667, 0.18055556],
[0.34375, 0.28125],
],
format!("Feature type: {:?}", FeatureType::ASM)
);
assert_eq_round(
calculate_feature(&p, FeatureType::Correlation),
array![
[0.79698847, 0.70116959],
[0.85634884, 0.57735027],
],
format!("Feature type: {:?}", FeatureType::Correlation)
);
}
}