1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! This module contains traits and structs for feature descriptors that can be extracted from images.
//!
//! # Examples
//!
//! ```
//! use object_detector_rust::feature::{DummyFeature, Feature};
//! use image::{DynamicImage, RgbImage};
//!
//! let image = DynamicImage::ImageRgb8(RgbImage::new(16, 16));
//! let feature = DummyFeature::new(123.0);
//! let descriptor = feature.extract(&image).unwrap();
//!
//! assert_eq!(descriptor.len(), 1);
//! assert_eq!(descriptor[0], 123.0);
//! ```
//!
//! This example shows how to use the `DummyFeature` struct, which is a simple implementation of the `Feature` trait, to extract a feature descriptor from an image.

use image::DynamicImage;
use std::{fmt::Debug, result::Result};

#[cfg(feature = "hog")]
mod hog_feature;
#[cfg(feature = "hog")]
pub use hog_feature::HOGFeature;

#[cfg(feature = "brief")]
mod brief_feature;
#[cfg(feature = "brief")]
pub use brief_feature::BriefFeature;

/// Trait for feature descriptors that can be extracted from images
pub trait Feature: Debug {
    /// Extracts the feature descriptor from the given image
    fn extract(&self, image: &DynamicImage) -> Result<Vec<f32>, String>;
}

/// Dummy feature descriptor that always returns the same value
#[derive(Debug)]
pub struct DummyFeature {
    value: f32,
}

impl DummyFeature {
    /// Creates a new dummy feature descriptor with the given value
    pub fn new(value: f32) -> Self {
        Self { value }
    }
}

impl Feature for DummyFeature {
    fn extract(&self, _image: &DynamicImage) -> Result<Vec<f32>, String> {
        Ok(vec![self.value])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use image::RgbImage;

    #[test]
    fn test_dummy_feature() {
        let image = DynamicImage::ImageRgb8(RgbImage::new(16, 16));
        let feature = DummyFeature::new(123.0);
        let descriptor = feature.extract(&image).unwrap();
        assert_eq!(descriptor.len(), 1);
        assert_eq!(descriptor[0], 123.0);
    }
}