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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
//! Struct for extracting HOG features from images.
//!
//! # Examples
//!
//! ```
//! use image::DynamicImage;
//! use object_detector_rust::feature::{Feature, HOGFeature};
//!
//! let image = DynamicImage::new_luma8(128, 128);
//! let hog_feature = HOGFeature::default();
//! let features = hog_feature.extract(&image).unwrap();
//! ```
use image::DynamicImage;
use imageproc::hog::{hog, HogOptions};
use super::Feature;
/// Struct for extracting HOG features from images.
#[derive(Debug, PartialEq)]
pub struct HOGFeature {
options: HogOptions,
}
impl HOGFeature {
/// Creates a new HOG feature extractor
pub fn new() -> Self {
Self::default()
}
/// Creates a new `HogFeature` with the given `HogOptions`.
///
/// # Example
///
/// ```
/// use imageproc::hog::HogOptions;
/// use object_detector_rust::feature::{Feature, HOGFeature};
///
/// let hog_options = HogOptions {
/// orientations: 9,
/// signed: true,
/// cell_side: 8,
/// block_side: 2,
/// block_stride: 1,
/// };
/// let hog_feature = HOGFeature::new_with_options(hog_options);
/// ```
///
/// In this example, a `HogFeature` is created with 9 gradient orientation bins,
/// treating gradients in opposite directions as equal,
/// and using 8x8 pixel cells and 2x2 cell blocks with a stride of 1 cell.
///
pub fn new_with_options(options: HogOptions) -> Self {
Self { options }
}
}
impl Default for HOGFeature {
/// Creates a new HOG feature extractor with default options
fn default() -> Self {
let default_options = HogOptions {
orientations: 9,
signed: false,
cell_side: 8,
block_side: 2,
block_stride: 1,
};
Self {
options: default_options,
}
}
}
impl Feature for HOGFeature {
fn extract(&self, image: &DynamicImage) -> Result<Vec<f32>, String> {
match image.as_luma8() {
Some(gray_image) => hog(gray_image, self.options),
None => hog(&image.to_luma8(), self.options),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hog_feature() {
// Define the input image for the test
let image = image::DynamicImage::new_luma8(64, 64);
// Create an instance of the HOG feature with default options
let hog_feature = HOGFeature::new();
// Call the extract method on the HOG feature
let hog_descriptor = hog_feature.extract(&image).unwrap();
// Assert that the output of the extract method is correct
assert_eq!(hog_descriptor.iter().sum::<f32>(), 0.0);
}
#[test]
fn test_hog_feature_with_options() {
// Define the input image for the test
let image = image::DynamicImage::new_luma8(64, 64);
let hog_options = HogOptions {
orientations: 9,
signed: true,
cell_side: 8,
block_side: 2,
block_stride: 1,
};
let hog_feature = HOGFeature::new_with_options(hog_options);
// Call the extract method on the HOG feature
let hog_descriptor = hog_feature.extract(&image).unwrap();
// Assert that the output of the extract method is correct
assert_eq!(hog_descriptor.iter().sum::<f32>(), 0.0);
}
}